application.yml配置文件
#durid數(shù)據(jù)庫連接池設(shè)置
spring:
profiles:
##此處引入配置其他數(shù)據(jù)源配置文件
include: dev-datasource
aop:
proxy-target-class: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/primarydatasource?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# 下面為連接池的補(bǔ)充設(shè)置胎围,應(yīng)用到上面所有數(shù)據(jù)源中
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 15
# 配置獲取連接等待超時(shí)的時(shí)間
maxWait: 60000
# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接鹃祖,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache宅广,并且指定每個(gè)連接上PSCache的大小
poolPreparedStatements: true
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì)目尖,'wall'用于防火墻
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j2
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
useGlobalDataSourceStat: true
druid:
remove-abandoned: true
remove-abandoned-timeout: 180
log-abandoned: true
application-dev-datasource.yml配置文件 (個(gè)人習(xí)慣單獨(dú)寫了個(gè)yml扎运,再增加數(shù)據(jù)源可以在這里配置)
## 第二個(gè)數(shù)據(jù)源
spring:
datasource2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/seconddatasource?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
主數(shù)據(jù)源配置代碼
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Primary
@Configuration
@MapperScan(basePackages = "com.btwl.primary.mapper" , sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSource {
@Bean(name = "DataSource_primary")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("DataSource_primary") DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("DataSource_primary") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第二個(gè)數(shù)據(jù)源配置代碼
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.btwl.second.mapper" , sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSource {
@Bean(name = "DataSource_second")
@ConfigurationProperties(prefix = "spring.datasource1")
public DataSource secondDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("DataSource_second") DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager secondTransactionManager(@Qualifier("DataSource_second") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Primary:指定為默認(rèn)數(shù)據(jù)源瑟曲,沒有該注解會報(bào)錯(cuò),系統(tǒng)找不到默認(rèn)數(shù)據(jù)源
@MapperScan:掃描指定包的mapper作為對應(yīng)數(shù)據(jù)源豪治,建議每個(gè)數(shù)據(jù)源都用不同的包區(qū)分
@Qualifier:與@Autowired類似洞拨,用作區(qū)分如果存在多個(gè)實(shí)現(xiàn)類要指定要注入哪個(gè) 參數(shù)為指定Bean的name
spring-database-jdbc-url 不要寫成 spring-database-url 會報(bào)錯(cuò) exception : jdbcUrl is required with driverClassName,(嗯负拟,不知道為啥
業(yè)務(wù)邏輯代碼就不放了跟其他的沒什么區(qū)別烦衣,再添加數(shù)據(jù)源的話就按以上配置再加一個(gè)就好了,