1. 当我们在使用springboot整合mybatis的时候,mapper接口常使用@Mapper注解,让mybatis自动取扫描这个类,并将该类添加到spring容器中
    在这里插入图片描述

  2. 如果我们的mapper接口,非常多,我们可以使用@MapperScan注解去定义某一个包,自动扫描这个报下所有的mapper接口,而不用每个mapper接口都加上@mapper注解

多数据源的异常情况

当我们使用多数据源的情况下,如使 用了一个 以上的 DataSource ,例如seata的分布式事务。那 么自动 装配可 能会失效 。此时你需要重新指定@MapperScan进行扫描。如果不定义则在启动时会抛出异常,提示找不到该mapper接口的bean。

  • 你可以自定义配置类进行扫描
@Configuration
@MapperScan({"com.aiit.springclould.dao"})
public class MyBatisConfig {

}
  • 你也可以在启动类上添加注解
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@MapperScan({"com.aiit.springclould.dao"})
public class SeataStorageMicroServiceApplication10010 {
    public static void main(String[] args) {
        SpringApplication.run
                (SeataStorageMicroServiceApplication10010.class,args);
    }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐