云计算百科
云计算领域专业知识百科平台

MyBatis-Plus Mapper 扫描配置问题

问题描述

Spring Boot 启动时报错:

A component required a bean of type
'…mapper.UserMapper' that could not be found

问题原因

Spring 容器没有成功注册 UserMapper 对应的 Mapper Bean,通常需要检查 Mapper 扫描配置及 MyBatis-Plus 依赖是否正确。

解决方案

在 Spring Boot 启动类上添加 @MapperScan,指定 Mapper 接口所在的包:

@SpringBootApplication
@MapperScan("com.hadoop.hdfs.psychologyhealthassistantbackend.mapper")
public class PsychologyhealthAssistantBackendApplication {

public static void main(String[] args) {
SpringApplication.run(
PsychologyhealthAssistantBackendApplication.class,
args
);
}
}

确认 Mapper 接口继承 BaseMapper:

public interface UserMapper extends BaseMapper<User> {
}

也可以在单个 Mapper 接口上添加 @Mapper 注解。

补充说明

  • @Mapper:标记单个 Mapper 接口。

  • @MapperScan:批量扫描指定包下的 Mapper 接口。

  • BaseMapper<T>:提供 MyBatis-Plus 封装的基础 CRUD 方法。

小 tips

仅仅 import MapperScan 并不会启用 Mapper 扫描,必须在启动类或其他配置类上实际使用 @MapperScan 注解。

赞(0)
未经允许不得转载:网硕互联帮助中心 » MyBatis-Plus Mapper 扫描配置问题
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!