SpringBoot+MyBatis-Plus项目启动报错:找不到UserMapper Bean依赖
报错信息
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.wms.wms.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}...
Process finished with exit code 1
问题根源及解决步骤
1. 补充MyBatis-Plus核心依赖
你的pom.xml仅引入了MyBatis-Plus代码生成器依赖,缺少核心运行依赖,导致Spring无法识别Mapper接口。在<dependencies>节点中添加:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> <!-- 与已引入的generator版本保持一致 --> </dependency>
2. 修正MySQL驱动类
MySQL 8.0+版本的驱动类为com.mysql.cj.jdbc.Driver,替换application.yml中的旧驱动类:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver
3. 修复实体类与数据库字段映射
数据库表字段role_id、isValid与实体类roleId、isvalid存在命名规则不匹配,可二选一处理:
- 方式一:开启自动驼峰映射,在application.yml中添加:
mybatis-plus: configuration: map-underscore-to-camel-case: true - 方式二:给实体类字段添加注解指定映射关系:
@TableField("role_id") private int roleId; @TableField("isValid") private String isvalid;
4. 确保Mapper接口被Spring扫描(可选但稳妥)
在启动类WmsApplication上添加@MapperScan注解,明确指定Mapper所在包,避免扫描遗漏:
@SpringBootApplication @MapperScan("com.wms.wms.mapper") public class WmsApplication { public static void main(String[] args) { SpringApplication.run(WmsApplication.class, args); } }
5. 刷新依赖并重启项目
执行Maven的clean+compile命令刷新依赖,再重启项目即可。
内容的提问来源于stack exchange,提问作者Cody Qi

