springboot mybatis @MapperScan()注解作用

错误

在springboot+mybatis工程中,有时报错,如下:

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.lvyou.mapper.IAdminMapper' that could not be found.


Action:

Consider defining a bean of type 'com.lvyou.mapper.IAdminMapper' in your configuration.

找不到Mapper

在 mapper接口使用 @Repository 注解时,会报上面的错误

解决

方法一

mapper接口使用 @Mapper 注解 替代 @Repository 注解

方法二

mapper 接口仍然使用 @Repository 注解

在启动类增加 @MapperScan("top.malaoshi.mapper") 注解,
扫描 top.malaoshi.mapper 包下的类,使 mapper接口 生效

如下:

@SpringBootApplication
//mapper类的包路径
@MapperScan("top.malaoshi.mapper")
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}

原文出处:/show_1EF6RCe0gjUK.html