static final String MAPPER_LOCATION1 = "classpath:mybatis/dsno1/*/*.xml"; //扫描 目录1
static final String MAPPER_LOCATION2 = "classpath:mybatis/dsno2/*/*.xml"; //扫描 目录2
@Bean(name = "masterSqlSessionFactory")
@Primary //www.1b23.com
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(resolveMapperLocations());
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MasterDataSourceConfig.CONFIG_LOCATION));
sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);
return sessionFactory.getObject();
}
/**加载多个mapper路径
* @return
*/www.1b23.com
public Resource[] resolveMapperLocations() {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<String> mapperLocations = new ArrayList<>();
mapperLocations.add(MasterDataSourceConfig.MAPPER_LOCATION1);
mapperLocations.add(MasterDataSourceConfig.MAPPER_LOCATION2);
List<Resource> resources = new ArrayList<Resource>();
if (mapperLocations != null) {
for (String mapperLocation : mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
来源:oschina
链接:https://my.oschina.net/fhadmin/blog/4292312