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关键字,没找到疑似配置。研究下我自己工程的初始化过程,找到了解决办法,我觉得这个解决办法,可以是通用的(至少思路是通用的)。

扫描二维码关注公众号,回复: 1913932 查看本文章

解决办法:

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

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


关联姿势:

使用MapperScannerConfigurer简化MyBatis配置


猜你喜欢

转载自blog.csdn.net/libertine1993/article/details/80388592