原因:
@Autowired 为 Spring 的注解,而注入的 Mapper 对象使用的又是 @Mapper 的注解,然而 @Mapper 又为 MyBaits 的注解,IDEA 能很好的兼容并识别 Spring 的注解,但不能很好的识别 MyBatis 的注解,因此在使用 @Autowired 注解时,IDEA 并不能检测到 @Mapper 注解的对象不为 NULL,因此就会报错。
如下:
解决方法:
1、在 Mapper 的类上添加 Spring 的注解,也可以解决 IDEA 报错的问题,如 @Repository 或 @Component 这类注解; 2、设置允许注入的 Mapper 对象为 NULL,也可以避免 IDEA 报错,只需要设置 @Autowired(required=false)