使用druid连接池
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
1、DataSourceSwitchAspect
@Component
@Order(value = -100)
@Aspect
public class DataSourceSwitchAspect {
@Pointcut("execution(* com.yxy.datacollectionetcgantry.mapper.db2..*.*(..))")
private void db1Aspect() {
}
@Pointcut("execution(* com.yxy.datacollectionetcgantry.mapper.postgre..*.*(..))")
private void db2Aspect() {
}
@Pointcut("execution(* com.yxy.datacollectionetcgantry.mapper.sqlserver..*.*(..))")
private void db3Aspect() {
}
@Before("db1Aspect()")
public void db1() {
DbContextHolder.setDbType(DBTypeEnum.db2);
}
@Before("db2Aspect()")
public void db2() {
DbContextHolder.setDbType(DBTypeEnum.postgre);
}
@Before("db3Aspect()")
public void db3() {
DbContextHolder.setDbType(DBTypeEnum.sqlserver);
}
}
2、DbContextHolder
public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal<>();
/**
* 设置数据源
* @param dbTypeEnum
*/
public static void setDbType(DBTypeEnum dbTypeEnum) {
contextHolder.set(dbTypeEnum.getValue());
}
/**
* 取得当前数据源
* @return
*/
public static String getDbType() {
return (String) contextHolder.get();
}
/**
* 清除上下文数据
*/
public static void clearDbType() {
contextHolder.remove();
}
}
3、DBTypeEnum
public enum DBTypeEnum {
db2("db2"), postgre("postgre"), sqlserver("sqlserver");
private String value;
DBTypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
4、DynamicDataSource
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DbContextHolder.getDbType();
}
}
5、MultipleDataSourceConfig
@Configuration
public class MultipleDataSourceConfig {
@Autowired
private PaginationInterceptor paginationInterceptor;
@Autowired
private PerformanceInterceptor performanceInterceptor;
@Bean(name = "db2")
@ConfigurationProperties(prefix = "spring.datasource.druid.db2")
public DataSource db2() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "postgre")
@ConfigurationProperties(prefix = "spring.datasource.druid.postgre")
public DataSource postgre() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "sqlserver")
@ConfigurationProperties(prefix = "spring.datasource.druid.sqlserver")
public DataSource sqlserver() {
return DruidDataSourceBuilder.create().build();
}
/**
* 动态数据源配置
* @return
*/
@Bean
@Primary
public DataSource multipleDataSource(@Qualifier("db2") DataSource db2,
@Qualifier("postgre") DataSource postgre,
@Qualifier("sqlserver") DataSource sqlserver) {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DBTypeEnum.db2.getValue(), db2);
targetDataSources.put(DBTypeEnum.postgre.getValue(), postgre);
targetDataSources.put(DBTypeEnum.sqlserver.getValue(), sqlserver);
dynamicDataSource.setTargetDataSources(targetDataSources);
dynamicDataSource.setDefaultTargetDataSource(db2);
return dynamicDataSource;
}
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(multipleDataSource(db2(), postgre(),sqlserver()));
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*.xml"));
sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor,performanceInterceptor});
return sqlSessionFactory.getObject();
}
}