SpringBoot+mybatis Field xxMapper in xxx required a bean of type 'XXMapper' that could not be found

用Spring Boot + Mybatis 做项目。项目代码结构是RestController + Service + ServiceImpl + Mapper + xxMapper.xml。在启动的时候报错:

Field xxxMapper in XXX required a bean of type 'XXXMapper' that could not be found.

Consider defining a bean of type 'XXXMapper' in your configuration.


网上找到的两种方法,一个不起作用,一个不适用。简单的说下:

1)在 Mapper的java类上面加@Mapper注解。

@Mapper
public interface UserMapper {

}

2)直接在启动入口RunApplication那里指定要扫描mapper的包的位置。

@MapperScan(value = "com.demo.mapper")
public class App 
{
    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }
}


第二种做法的思路是对的,但是不适用我的情况。因为我的工程,是在别的工程上做开发的,我们共用一个run Application入口,这么指定可能会影响工程其他模块。按照第二种做法的思路,工程内肯定在某个地方指定了mapper的扫描位置,解决问题的思路就是找出在哪指定了mapper的扫描位置。


开始在配置文件中查找类似scan,resources关键字,没找到疑似配置。研究下我自己工程的初始化过程,找到了解决办法,我觉得这个解决办法,可以是通用的(至少思路是通用的)。

解决办法:

全工程检索“MapperScannerConfigurer”,可以找到一个配置类,在这里指定了扫描的mapper包的位置。增加多一个你需要扫描的包的项即可。

		//配置多个的时候,最后一个需要以分号结尾,一个的时候是不需要分号的
		mapperScannerConfigurer.setBasePackage(
				"com.xxx.xxx.xxx.mapper;"
				+ "com.xx.xxx.xxx.xxx.mapper;"
		);


关联姿势:

使用MapperScannerConfigurer简化MyBatis配置


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值