配置同时使用2个数据源
1. application.properties
spring.datasource.dbold.jdbc-url=jdbc:oracle:thin:@10.16.4.77:1521:urpdb
spring.datasource.dbold.username=DEPLOYS_CLOUD
spring.datasource.dbold.password=wisedu
spring.datasource.dbold.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.dbnew.jdbc-url=jdbc:oracle:thin:@10.16.4.77:1521:urpdb
spring.datasource.dbnew.username=DEVOPS_CLOUD1
spring.datasource.dbnew.password=DEVOPS_CLOUD1
spring.datasource.dbnew.driver-class-name=oracle.jdbc.driver.OracleDriver
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
logging.level.com.wisedu=info
2. 排除自动数据源配置
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Db2newApplication {
public static void main(String[] args) {
SpringApplication.run(Db2newApplication.class, args);
System.out.println("启动完成....");
}
}
3. 自定义数据源配置

@Configuration
@MapperScan(basePackages = {"com.wisedu.devops.db2new.mapper.dbnew"}, sqlSessionTemplateRef = "newSqlSessionTemplate")
public class MybatisNewDBConfig {
@Bean(name = "dbnew")
@ConfigurationProperties(prefix = "spring.datasource.dbnew")
public DataSource newDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "newSqlSessionFactory")
public SqlSessionFactory newSqlSessionFactory(@Qualifier("dbnew") DataSource ds) throws Exception {
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(ds);
return factoryBean.getObject();
}
@Bean(name = "newTransactionManager")
public DataSourceTransactionManager newTransactionManager(@Qualifier("dbnew") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
@Bean(name = "newSqlSessionTemplate")
public SqlSessionTemplate newSqlSessionTemplate(@Qualifier("newSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = {"com.wisedu.devops.db2new.mapper.dbold"}, sqlSessionFactoryRef = "oldSqlSessionFactory")
public class MybatisOldDBConfig {
@Bean(name = "dbold")
@ConfigurationProperties(prefix = "spring.datasource.dbold")
public DataSource oldDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "oldSqlSessionFactory")
public SqlSessionFactory oldSqlSessionFactory(@Qualifier("dbold") DataSource ds) throws Exception {
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(ds);
return factoryBean.getObject();
}
@Bean(name = "oldTransactionManager")
public DataSourceTransactionManager oldTransactionManager(@Qualifier("dbold") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
@Bean(name = "oldSqlSessionTemplate")
public SqlSessionTemplate oldSqlSessionTemplate(@Qualifier("oldSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
配置重点:
需要使用 MybatisSqlSessionFactoryBean
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
结果:
com.wisedu.devops.db2new.mapper.dbold 使用 dbold 数据源配置
com.wisedu.devops.db2new.mapper.dbnew 使用 dbnew 数据源配置
配置2个数据源,根据自定义注解动态切换
AbstractRoutingDataSource 根据用户定义的规则选择当前的数据源
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDB();
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.METHOD
})
public @interface DS {
String value() default "dbold";
}
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDB();
}
}
@Aspect
@Component
public class DynamicDataSourceAspect {
@Before("@annotation(DS)")
public void beforeSwitchDS(JoinPoint point) {
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature) point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DEFAULT_DS;
try {
// 得到访问的方法对象
Method method = className.getMethod(methodName, argClass);
// 判断是否存在@DS注解
if (method.isAnnotationPresent(DS.class)) {
DS annotation = method.getAnnotation(DS.class);
// 取出注解中的数据源名
dataSource = annotation.value();
}
} catch (Exception e) {
e.printStackTrace();
}
// 切换数据源
DataSourceContextHolder.setDB(dataSource);
}
@After("@annotation(DS)")
public void afterSwitchDS(JoinPoint point) {
DataSourceContextHolder.clearDB();
}
}
@Configuration
public class DataSourceConfig {
@Autowired()
@Qualifier("dbold")
private DataSource dataSourceOld;
@Autowired()
@Qualifier("dbnew")
private DataSource dataSourceNew;
@Bean(name = "dynamicDS1")
public DataSource dataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 默认数据源
dynamicDataSource.setDefaultTargetDataSource(dataSourceOld);
// 配置多数据源
Map<Object, Object> dsMap = new HashMap<>(2);
dsMap.put("dbold", dataSourceOld);
dsMap.put("dbnew", dataSourceNew);
dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource;
}
}

浙公网安备 33010602011771号