springboot @Autowired mapper 爆红解决方案,超详细,罗列各种情况

解决@Autowired爆红的方案有四个:
1.mapper加入@Mapper注解(启动类可省去@MapperScan),因为@Mapper的文件会被去自动扫描注入,但是需要全体Mapper文件都加
2.mapper加入@Repository注解(启动类必须加@MapperScan)
3.mapper加入@Component注解(启动类必须加@MapperScan)
4.改@Autowired为@Resource,但程序运行报不报错还取决于mapper注入正确与否(看mapper文件用了什么注解以及启动类是否配了@MapperScan)
简单来说就是两种情况:
- 启动类配置了@MapperScan且路径正确,mapper接口文件可以不配任何注解(当然你也可以随意写上@Mapper,@Respository,@Component注解,不会有任何影响)
- 启动类未配置了@MapperScan,则Mapper接口文件必须配置@Mapper注解

笔者推荐最简方案:
启动类加@MapperScan 配合使用 @Resource (替换@Autowired)

你可能感兴趣的:(springboot @Autowired mapper 爆红解决方案,超详细,罗列各种情况)