自动注入失败。
方法一:
@Configuration
public class MyBatisConfig {
/**
* Mapper扫描配置. 自动扫描将Mapper接口生成代理注入到Spring.
*/
@Bean
public static MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
// 注意这里的扫描路径: 1.不要扫描到自定义的Mapper; 2.定义的路径不要扫描到tk.mybatis.mapper(如定义**.mapper).
// 两个做法都会导致扫描到tk.mybatis的Mapper,就会产生重复定义的报错.
mapperScannerConfigurer.setBasePackage("**.example.**.mapper");
return mapperScannerConfigurer;
}
}
方法二:
启动类上加MapperScan注解
@SpringBootApplication
@MapperScan("**.example.**.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}