SpringBoot 3.2.2 @Autowired注入DeptMapper失败,2.7.5版本正常
问题:SpringBoot 3.2.2启动时无法找到MyBatis Mapper Bean,回退到2.7.5正常
错误信息
*************************** APPLICATION FAILED TO START *************************** Description: Field deptMapper in com.itheima.service.impl.DeptServiceImpl required a bean of type 'com.itheima.mapper.DeptMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.itheima.mapper.DeptMapper' in your configuration.
问题背景
使用SpringBoot 3.2.2启动项目时出现上述错误,但将pom.xml中SpringBoot版本改回2.7.5后,无需修改任何代码即可正常启动。项目结构符合SpringBoot路径规范。
相关代码
Controller层
package com.itheima.controller; import com.itheima.pojo.Dept; import com.itheima.pojo.Result; import com.itheima.service.DeptService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Slf4j @RestController public class DeptController { @Autowired(required = true) private DeptService deptService; @GetMapping("/depts") public Result list() { log.info("search all departments"); List<Dept> deptList = deptService.list(); return Result.success(deptList); } }
Service接口
package com.itheima.service; import com.itheima.pojo.Dept; import java.util.List; public interface DeptService { /*search all departments*/ List<Dept> list(); }
Service实现类
package com.itheima.service.impl; import com.itheima.mapper.DeptMapper; import com.itheima.pojo.Dept; import com.itheima.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DeptServiceImpl implements DeptService { @Autowired private DeptMapper deptMapper; @Override public List<Dept> list() { return deptMapper.list(); } }
MyBatis Mapper接口
package com.itheima.mapper; import com.itheima.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface DeptMapper { // 查询全部的部门数据 @Select("select * from dept") List<Dept> list(); }
POJO类
package com.itheima.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDateTime; @Data @NoArgsConstructor @AllArgsConstructor public class Dept { private Integer id; private String name; private LocalDateTime createTime; private LocalDateTime updateTime; }
原因分析与解决方案
SpringBoot 3.x基于Spring Framework 6.x,对依赖版本有严格的兼容性要求:
- MyBatis Starter版本适配问题:SpringBoot 3.2.2需要搭配MyBatis Spring Boot Starter 3.0.x及以上版本,而SpringBoot 2.7.5对应的是MyBatis Starter 2.x版本。如果pom.xml中MyBatis Starter版本未同步升级,会导致MyBatis无法正常扫描
@Mapper注解生成Bean。 - 包扫描范围问题:虽然项目结构符合规范,但SpringBoot 3.x在自动配置的扫描逻辑上有细微调整,可尝试在启动类上添加
@MapperScan("com.itheima.mapper")注解,强制指定Mapper接口的扫描路径,确保MyBatis能识别并生成代理Bean。
具体解决步骤
步骤1:升级MyBatis Starter版本
在pom.xml中确保MyBatis Spring Boot Starter版本与SpringBoot 3.2.2兼容,例如:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> <!-- 适配SpringBoot 3.x的版本 --> </dependency>
步骤2:添加@MapperScan注解到启动类
在SpringBoot启动类上添加@MapperScan注解,指定Mapper所在的包:
package com.itheima; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.itheima.mapper") // 指定Mapper接口所在包 public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
步骤3:检查JDK版本
SpringBoot 3.x要求JDK 17及以上版本,确保项目运行环境是JDK 17+,否则也可能导致依赖加载异常。
内容的提问来源于stack exchange,提问作者lilpiggie0522
相关产品推荐
相关产品推荐

