问题描述:配置多数据源之后一直扫错包,一个数据源总是扫到另外的mapper里面
知识拓展: @Repository注解和@Mapper注解区别
@Reponsitory注解
@Reponsitory使用后,在启动类上需要添加@MapperScan("xxx.xxx.xxx.mapper")注解
@Mapper注解
@Mapper注解使用后相当于@Reponsitory加@MapperScan注解,会自动进行配置加载
1.解决方法一
多数据源不要在Application启动类写@MapperScan() <此处不扫描是因为要分主副连接扫描什么的太麻烦所以干脆此处不用>
在相应的DataScoureCofig 里面使用@MapperScan()
并且@Mapper不要使用 改用@Repository
2.解决方法二
接口不用写任何注解
在 DatasourceConfig 上写MapperScan(basePackages = ““com.swcp.xxx.xxx.xxx””)
在SqlSessionFactory工厂注入地址
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:com/swcp/xxx/xxx/xxx/*.xml"));
例
@Bean(name = "mysqlSqlSessionFactory")
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqldatasource") DataSource dataSource)
throws Exception {
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setCallSettersOnNulls(true);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
sessionFactory.setConfiguration(configuration);
// sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
sessionFactory.setPlugins(new Interceptor[]{new PaginationInterceptor().setLimit(-1)});
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:com/swcp/xxx/xxx/xxx/*.xml"));
return sessionFactory.getObject();
}