springboot+mybatis集成多數據源MySQL/Oracle/SqlServer
日常開發中可能時常會遇到一些這樣的需求,業務數據庫和第三方數據庫,兩個或多個數據庫屬於不同數據庫廠商,這時候就需要通過配置來實現對數據庫實現多源處理.大致說一下我的業務場景,框架本身是配置的springboot+mybatis+MySQL,業務數據庫為mysql數據庫,現在需要做一個統計平臺,統計數據來自於第三方Oracle數據庫.
閑話不多說,直接上代碼:
1.maven配置,將涉及到的數據庫配置到pom.xml文件中
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- oracle jdbc -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/ojdbc7-12.1.0.2.jar</systemPath>
</dependency>
<!-- SqlServer jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
2.application.properties配置,配置對應的數據庫連接
#MySQL
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#oracle
oracle.datasource.url=jdbc:oracle:thin:@xxx.xxx.xxx:11521/ORCL
oracle.datasource.username=xxx
oracle.datasource.password=xxx
oracle.datasource.driverClassName=oracle.jdbc.OracleDriver
#SqlServer
sqlserver.datasource.url=jdbc:sqlserver://xxx.xxx.xxx:1433;DatabaseName=test
sqlserver.datasource.username=xxx
sqlserver.datasource.password=xxx
sqlserver.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
3.去除默認數據庫配置,每個數據庫單獨寫一個config.java文件配置
3.1 MySQL配置
@Configuration
// 掃描 Mapper 接口並容器管理
@MapperScan(basePackages = MysqlDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlDataSourceConfig {
// 精確到 mysql 目錄,以便跟其他數據源隔離
static final String PACKAGE = "com.xxx.xxx.mysqlmapper";//對應的mysql dao層包
static final String MAPPER_LOCATION = "classpath:mysqlmybatis/*.xml";//對應mysql的mapper.xml文件
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClass;
@Bean(name = "mysqlDataSource")
@Primary
public DataSource mysqlDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "mysqlTransactionManager")
@Primary
public DataSourceTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(mysqlDataSource());
}
@Bean(name = "mysqlSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(mysqlDataSource);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(MysqlDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
3.2 Oracle配置
@Configuration
//掃描 Mapper 接口並容器管理
@MapperScan(basePackages= OracleDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {
// 精確到 oracle 目錄,以便跟其他數據源隔離
static final String PACKAGE = "com.xxx.xxx.oraclemapper";
static final String MAPPER_LOCATION = "classpath:oraclemybatis/*.xml";
@Value("${oracle.datasource.url}")
private String url;
@Value("${oracle.datasource.username}")
private String user;
@Value("${oracle.datasource.password}")
private String password;
@Value("${oracle.datasource.driverClassName}")
private String driverClass;
@Bean(name = "oracleDataSource")
public DataSource oracleDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "oracleTransactionManager")
public DataSourceTransactionManager oracleTransactionManager() {
return new DataSourceTransactionManager(oracleDataSource());
}
@Bean(name = "oracleSqlSessionFactory")
public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(oracleDataSource);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(OracleDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
3.3 SqlServer配置
@Configuration
//掃描 Mapper 接口並容器管理
@MapperScan(basePackages=SqlServerDataSourceConfig.PACKAGE,sqlSessionFactoryRef="sqlserverSqlSessionFactory")
public class SqlServerDataSourceConfig {
// 精確到 sqlserver 目錄,以便跟其他數據源隔離
static final String PACKAGE = "com.xxx.xxx.sqlservermapper";
static final String MAPPER_LOCATION = "classpath:sqlservermybatis/*.xml";
@Value("${sqlserver.datasource.url}")
private String url;
@Value("${sqlserver.datasource.username}")
private String user;
@Value("${sqlserver.datasource.password}")
private String password;
@Value("${sqlserver.datasource.driverClassName}")
private String driverClass;
@Bean(name = "sqlserverDataSource")
public DataSource sqlserverDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "sqlserverTransactionManager")
public DataSourceTransactionManager sqlserverTransactionManager() {
return new DataSourceTransactionManager(sqlserverDataSource());
}
@Bean(name = "sqlserverSqlSessionFactory")
public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource sqlserverDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(sqlserverDataSource);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(SqlServerDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
如果還有其他數據庫,配置類似....
通過以上配置,即可實現多數據源管理,具體配置按照自己的業務需求
---------------------
作者:nice_hcq
來源:CSDN
原文:https://blog.csdn.net/nice_hcq/article/details/80404827
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
https://blog.csdn.net/nice_hcq/article/details/80404827
springboot+mybatis集成多數據源MySQL/Oracle/SqlServer