Hello你們好,本章咱們配置多數據源功能 。有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝
修改application.propertieshtml
spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.db1.url=jdbc:mysql://localhost:3333/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.db1.username=root
spring.datasource.db1.password=123456
spring.datasource.db1.initialSize=5
spring.datasource.db1.minIdle=5
spring.datasource.db1.maxActive=20
#鏈接等待超時時間
spring.datasource.db1.maxWait=60000
#配置隔多久進行一次檢測(檢測能夠關閉的空閒鏈接)
spring.datasource.db1.timeBetweenEvictionRunsMillis=60000
#配置鏈接在池中的最小生存時間
spring.datasource.db1.minEvictableIdleTimeMillis=300000
spring.datasource.db1.validationQuery=SELECT 1 FROM DUAL
spring.datasource.db1.testWhileIdle=true
spring.datasource.db1.testOnBorrow=false
spring.datasource.db1.testOnReturn=false
# 打開PSCache,而且指定每一個鏈接上PSCache的大小
spring.datasource.db1.poolPreparedStatements=true
spring.datasource.db1.maxPoolPreparedStatementPerConnectionSize=20
# 配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆
spring.datasource.db1.filters=stat,wall,slf4j
# 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.db1.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.db2.url=jdbc:mysql://localhost:3306/chuchen?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.db2.username=root
spring.datasource.db2.password=123456
spring.datasource.db2.initialSize=5
spring.datasource.db2.minIdle=5
spring.datasource.db2.maxActive=20
#鏈接等待超時時間
spring.datasource.db2.maxWait=60000
#配置隔多久進行一次檢測(檢測能夠關閉的空閒鏈接)
spring.datasource.db2.timeBetweenEvictionRunsMillis=60000
#配置鏈接在池中的最小生存時間
spring.datasource.db2.minEvictableIdleTimeMillis=300000
spring.datasource.db2.validationQuery=SELECT 1 FROM DUAL
spring.datasource.db2.testWhileIdle=true
spring.datasource.db2.testOnBorrow=false
spring.datasource.db2.testOnReturn=false
# 打開PSCache,而且指定每一個鏈接上PSCache的大小
spring.datasource.db2.poolPreparedStatements=true
spring.datasource.db2.maxPoolPreparedStatementPerConnectionSize=20
# 配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆
spring.datasource.db2.filters=stat,wall,slf4j
# 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.db2.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000複製代碼
注意修改成本身的數據庫鏈接java
MysqlDataSource1Config
mysql
package com.example.demo.core.configurer.dataSource;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.example.demo.dao.db1"})
public class MysqlDataSource1Config {
@Bean(name = "primaryDataSource")
//需設置主數據源
@Primary
@ConfigurationProperties(prefix="spring.datasource.db1")
public DataSource dataSource(){
//跟以前不同了
return new DruidDataSource();
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean(name="primarySqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource")DataSource primaryDataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(primaryDataSource);
Resource[] mapperLocations = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml");
sessionFactory.setMapperLocations(mapperLocations);
return sessionFactory.getObject();
}
}
複製代碼
MysqlDataSource2Config
git
package com.example.demo.core.configurer.dataSource;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.example.demo.dao.db2"}, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class MysqlDataSource2Config {
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource dataSource() {
return new DruidDataSource();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
Resource[] mapperLocations = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml");
sessionFactory.setMapperLocations(mapperLocations);
return sessionFactory.getObject();
}
}複製代碼
目錄結構以下圖,db1爲主數據源,db2爲從數據源,這樣根據訪問dao層不一樣,會自動進行數據源切換github
輸入http://localhost:8080/userInfo/selectAll
spring
結果:sql
{
"code": 200,
"data": {
....省略
"list": [
{
"id": "1",
"userName": "Mr_初晨"
},
{
"id": "2",
"userName": "Mr_初晨"
}
],
.....省略
},
"msg": "success"
}複製代碼
輸入http://localhost:8080/userLabel/list
數據庫
結果:apache
{
"code": 200,
"data": {
....省略
"list": [
{
"createTime": 1523283547000,
"id": "24084baed3c6409a8619a635bf4428a1",
"labelId": "4",
"userId": "1"
}
],
.....省略
},
"msg": "success"
}複製代碼
輸入http://localhost:8080/druid/datasource.html
bash
也能夠看見有兩個數據源
碼雲地址: gitee.com/beany/mySpr…
GitHub地址: github.com/MyBeany/myS…
寫文章不易,如對您有幫助,請幫忙點下star
配置多數據源功能已完成,後續功能接下來陸續更新,有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝你們。