参考来自链接:Spring Boot配置多数据源并实现Druid自动切换_数组斯诺-CSDN博客_druid多数据源切换
参考链接2:搞定SpringBoot多数据源(2):动态数据源 - Mason技术记录 - 博客园 (cnblogs.com)
1、数据源配置:application-dev.yml
spring:
datasource:
ssp:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@133.96.9.78:1521:zyptdb
username: bsdp_app
password: BSDP_APP_1
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000 #unit:ms
timeBetweenEvictionRunsMillis: 60000 #unit:ms
minEvictableIdleTimeMillis: 300000 #unit:ms
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
gms:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@133.96.9.125:1533:hbgs
username: pure_hb
password: pure_hb_4rfv
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000 #unit:ms
timeBetweenEvictionRunsMillis: 60000 #unit:ms
minEvictableIdleTimeMillis: 300000 #unit:ms
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
2、文件目录:
3、数据源config 配置:
package com.bonc.ssp.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
@MapperScan(basePackages = "com.bonc.ssp.dao.ssp", sqlSessionTemplateRef = "sspSqlSessionTemplate")
public class DruidDataSourceConfig {
private static Logger logger = LoggerFactory.getLogger(DruidDataSourceConfig.class);
@Value("${spring.datasource.ssp.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.ssp.url}")
private String url;
@Value("${spring.datasource.ssp.username}")
private String username;
@Value("${spring.datasource.ssp.password}")
private String password;
@Value("${spring.datasource.ssp.initialSize}")
private int initialSize;
@Value("${spring.datasource.ssp.minIdle}")
private int minIdle;
@Value("${spring.datasource.ssp.maxActive}")
private int maxActive;
@Value("${spring.datasource.ssp.maxWait}")
private int maxWait;
@Value("${spring.datasource.ssp.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.ssp.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.ssp.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.ssp.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.ssp.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.ssp.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.ssp.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.ssp.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.ssp.filters}")
private String filters;
@Primary
@Bean(name = "sspDataSource")
@ConfigurationProperties(prefix = "spring.datasource.ssp")
public DataSource dataSource() {
DruidDataSource s = new DruidDataSource();
s.setDriverClassName(driverClassName);
s.setUrl(url);
s.setUsername(username);
s.setPassword(password);
s.setInitialSize(initialSize);
s.setMinIdle(minIdle);
s.setMaxActive(maxActive);
s.setMaxWait(maxWait);
s.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
s.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
s.setValidationQuery(validationQuery);
s.setTestWhileIdle(testWhileIdle);
s.setTestOnBorrow(testOnBorrow);
s.setTestOnReturn(testOnReturn);
s.setPoolPreparedStatements(poolPreparedStatements);
s.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
s.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
return s;
}
@Primary
@Bean(name = "sspTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("sspDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "sspSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("sspDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/ssp/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Primary
@Bean(name = "sspSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("sspSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.bonc.ssp.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
@MapperScan(basePackages = "com.bonc.ssp.dao.gms", sqlSessionTemplateRef = "gmsSqlSessionTemplate")
public class GMSDataSourceConfig {
private static Logger logger = LoggerFactory.getLogger(GMSDataSourceConfig.class);
@Value("${spring.datasource.gms.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.gms.url}")
private String url;
@Value("${spring.datasource.gms.username}")
private String username;
@Value("${spring.datasource.gms.password}")
private String password;
@Value("${spring.datasource.gms.initialSize}")
private int initialSize;
@Value("${spring.datasource.gms.minIdle}")
private int minIdle;
@Value("${spring.datasource.gms.maxActive}")
private int maxActive;
@Value("${spring.datasource.gms.maxWait}")
private int maxWait;
@Value("${spring.datasource.gms.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.gms.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.gms.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.gms.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.gms.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.gms.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.gms.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.gms.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.gms.filters}")
private String filters;
@Bean(name = "gmsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.gms")
public DataSource dataSource() {
DruidDataSource s = new DruidDataSource();
s.setDriverClassName(driverClassName);
s.setUrl(url);
s.setUsername(username);
s.setPassword(password);
s.setInitialSize(initialSize);
s.setMinIdle(minIdle);
s.setMaxActive(maxActive);
s.setMaxWait(maxWait);
s.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
s.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
s.setValidationQuery(validationQuery);
s.setTestWhileIdle(testWhileIdle);
s.setTestOnBorrow(testOnBorrow);
s.setTestOnReturn(testOnReturn);
s.setPoolPreparedStatements(poolPreparedStatements);
s.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
s.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
return s;
}
@Bean(name = "gmsTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("gmsDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "gmsSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("gmsDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/gms/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "gmsSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("gmsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}