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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot @Autowired mapper 爆红解决方案,超详细,罗列各种情况相关的知识,希望对你有一定的参考价值。

参考技术A 解决@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 爆红解决方案,超详细,罗列各种情况的主要内容,如果未能解决你的问题,请参考以下文章

springboot component注入servecie

springboot Autowired BeanNotOfRequiredTypeException

SpringBoot@Autowired注入静态成员变量报空指针异常

SpringBoot常用注解:@Resource/@Component与@Autowired的使用

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

SpringBoot中的autowired注解可以修饰接口吗?