多数据源
数据源mysql配置
@Configuration
@MapperScan(basePackages="com.database.multidatademo.mapper.first",sqlSessionFactoryRef = "firstSqlSessionFactory")
public class DataSource1Config {
@Autowired
Datasource1Properties datasource1Properties;
@Bean(name = "firstDataSource")
@Primary
public DataSource getDataSource1(){
HikariConfig config = new HikariConfig();
config.setUsername(datasource1Properties.getUsername());
config.setPassword(datasource1Properties.getPassword());
config.setJdbcUrl(datasource1Properties.getUrl());
config.setDriverClassName(datasource1Properties.getDriverClassName());
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
@Bean(name = "firstSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource1) throws Exception {
//使用BaseMapper需要使用MybatisSqlSessionFactoryBean而不是SqlSessionFactoryBean,否则Mapper中继承的BaseMapper无效
MybatisSqlSessionFactoryBean mssfb = new MybatisSqlSessionFactoryBean();
mssfb.setDataSource(dataSource1);
//xml位置
mssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapping/*.xml"));
//别名
mssfb.setTypeAliasesPackage("com.database.multidatademo.domain");
//分页插件,需要放在驼峰配置前,否则分页不生效,total=0
mssfb.setPlugins(new Interceptor[] {new PaginationInterceptor()});
//驼峰
mssfb.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
mssfb.getObject().getConfiguration().setCallSettersOnNulls(true);
return mssfb.getObject();
}
@Bean(name = "firstSqlSessionTemplate")
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "firstDataSourceTransactionManager")
public DataSourceTransactionManager firstDataSourceTransactionManager(@Qualifier("firstDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
数据源oracle配置
@Configuration
@MapperScan(basePackages="com.database.multidatademo.mapper.sec",sqlSessionFactoryRef = "secSqlSessionFactory")
public class DataSource2Config {
@Autowired
Datasource2Properties datasource2Properties;
@Bean(name = "secDataSource")
public DataSource getDataSource2(){
HikariConfig config = new HikariConfig();
config.setUsername(datasource2Properties.getUsername());
config.setPassword(datasource2Properties.getPassword());
config.setJdbcUrl(datasource2Properties.getUrl());
config.setDriverClassName(datasource2Properties.getDriverClassName());
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
@Bean(name = "secSqlSessionFactory")
public SqlSessionFactory secSqlSessionFactory(@Qualifier("secDataSource") DataSource dataSource2) throws Exception {
//使用BaseMapper需要使用MybatisSqlSessionFactoryBean而不是SqlSessionFactoryBean,否则Mapper中继承的BaseMapper无效
MybatisSqlSessionFactoryBean mssfb = new MybatisSqlSessionFactoryBean();
mssfb.setDataSource(dataSource2);
//xml位置
mssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapping/*.xml"));
//别名
mssfb.setTypeAliasesPackage("com.database.multidatademo.domain");
//分页插件,需要放在驼峰配置前,否则分页不生效,total=0
mssfb.setPlugins(new Interceptor[] {new PaginationInterceptor()});
//驼峰
mssfb.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
mssfb.getObject().getConfiguration().setCallSettersOnNulls(true);
return mssfb.getObject();
}
@Bean(name = "secSqlSessionTemplate")
public SqlSessionTemplate secSqlSessionTemplate(@Qualifier("secSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "secDataSourceTransactionManager")
public DataSourceTransactionManager secDataSourceTransactionManager(@Qualifier("secDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
demo已经上传,使用swagger3可以调试,3和2的请求地址也有所不同,http://localhost:8888/swagger-ui/index.html
,表中的数据自己可以添加几条测试下https://gitee.com/cherrychao/case.git