踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.

背景:

公司项目有个小伙子将项目的日志功能做了优化。今天在pom添加依赖,在启动类上加个注解,然后启动项目突然报了ModuleMapper 找不到。
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
项目一直是好的,又没有对这个mapper修改。进入ModuleMapper 中发现上面也是有@Mapper注解的踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
可是为什么容器找不到呢,分析一下刚刚修改的才做,引入依赖加注解,问题可能是注解上的问题,注释掉添加的注解,启动服务正常运行。罪魁祸首就是这个注解。
点解注解发现配置类上有个@MapperScan扫描注入。发现这伙计需要操作数据库。而我接收负责的项目用的是传统的@Mapper注入。
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.

思考:难道在一个项目中@MapperScan和@Mapper不能共存吗?

尝试解决:

自己创建一个springboot 项目,开始用@Mapper 注入容器 启动程序。程序正常启动。
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
停掉服务,将mapp文件上的@Mapper删除 ,将文件移动到mapper文件中,在启动类上添加@MapperScan 扫包注入。正常启动。
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
现在测试@MapperScan 和@Mapper 分别在不包中测试一下。现在将UserMapper 放在mapper包中, 将UserTokenMapper 放在mapper2包中并添加@Mapper 启动测试。

启动类:
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
UserMapper :
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
UserTokenMapper :
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
启动项目报错:Description:

Field userTokenMapper in com.wyz.yangyang.member.service.impl.MemberServiceImpl required a bean of type ‘com.wyz.yangyang.member.mapper2.UserTokenMapper’ that could not be found.

Action:

Consider defining a bean of type ‘com.wyz.yangyang.member.mapper2.UserTokenMapper’ in your configuration.

Disconnected from the target VM, address: ‘127.0.0.1:56527’, transport: ‘socket’

Process finished with exit code 1
踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.
然后我有测试可@MapperScan 扫描的包中不放mapper 文件 ,mapper2 中mapper文件都添加@Mapper,发现启动正常。
又测试了@MapperScan 和@Mapper 同在一个包中,启动正常。

在此可以看出 @MapperScan 和@Mapper在不同包中,@Mapper注解失效。
为了项目快速开发,为了以后更好的兼容,我将项目改为@MapperScan 模式,因为一个注解可以配置多个包路径。

踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found.踩 项目中@MapperScan和@Mapper共存 之坑XxxMapper that could not be found. wyz0923 发布了18 篇原创文章 · 获赞 2 · 访问量 2567 私信 关注
上一篇:springboot启动类注解说明


下一篇:Spring Boot MyBatis注解:@MapperScan和@Mapper