spring boot druid mybatis 多數據源 配置
spring boot 在配置時做了很多簡化配置的設置,但是簡化的配置往往已犧牲一定的定製化,比如在數據源的配置時,spring boot 隻提供4種數據庫連接池的配置,其中並不支持常用的druid
閱讀spring boot DataSourceBuilder 的源碼可以發現 spring boot 提供的4種數據源類型並不是我們想要的
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
"org.apache.tomcat.jdbc.pool.DataSource",
"com.zaxxer.hikari.HikariDataSource",
"org.apache.commons.dbcp.BasicDataSource", // deprecated
"org.apache.commons.dbcp2.BasicDataSource" };
但是 DataSourceBuilder 提供了type方法來自定義DataSource類型
public DataSourceBuilder type(Class<? extends DataSource> type) {
this.type = type;
return this;
}
知道了方法,下麵配置就簡單許多了
首先是application.properties 文件的配置
spring.datasource.sso.url=jdbc:mysql://localhost:3306/sso?useSSL=false
spring.datasource.sso.username=root
spring.datasource.sso.password=root
spring.datasource.sso.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.sso.max-idle=5
spring.datasource.sso.max-wait=10000
spring.datasource.sso.min-idle=1
spring.datasource.sso.initial-size=1
spring.datasource.sso.validation-query=SELECT 1
spring.datasource.sso.test-on-borrow=false
spring.datasource.sso.test-while-idle=true
spring.datasource.sso.time-between-eviction-runs-millis=18800
spring.datasource.message.url=jdbc:mysql://localhost:3306/message?useSSL=false
spring.datasource.message.username=root
spring.datasource.message.password=root
spring.datasource.message.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.message.max-idle=5
spring.datasource.message.max-wait=10000
spring.datasource.message.min-idle=1
spring.datasource.message.initial-size=1
spring.datasource.message.validation-query=SELECT 1
spring.datasource.message.test-on-borrow=false
spring.datasource.message.test-while-idle=true
spring.datasource.message.time-between-eviction-runs-millis=18800
然後是具體的主數據源配置類
@Configuration
@MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.sso"}, sqlSessionFactoryRef = "ssoSqlSessionFactory")
public class SsoConfig {
@Bean(name = "ssoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sso")
@Primary
public DataSource ssoDataSource() {
//指定使用DruidDataSource
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
@Bean(name = "ssoSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("ssoDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sso/*.xml"));
return bean.getObject();
}
@Primary
@Bean(name = "ssoTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("ssoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "ssoSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ssoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
_ @ConfigurationProperties(prefix = "spring.datasource.sso") 引入配置項_
使用如下方式來創建DruidDataSource,簡化配置
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
第二數據源
@Configuration
@MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.message"}, sqlSessionFactoryRef = "messageSqlSessionFactory")
public class MessageConfig {
@Bean(name = "messageDataSource")
@ConfigurationProperties(prefix = "spring.datasource.message")
public DataSource messageDataSource() {
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
@Bean(name = "messageSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("messageDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/message/*.xml"));
return bean.getObject();
}
@Bean(name = "messageTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("messageDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "messageSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("messageSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
數據源配置完畢
每個數據源都會生成自己的sqlSession,相互獨立
最後更新:2017-09-17 22:34:27
上一篇:
Java中的static關鍵字解析
下一篇:
Java和C有什麼區別,應該學習Java還是C
PostgreSQL SQL 語言:性能提示
微軟瓦解Nitol僵屍網絡
ant的高級使用,ant命令詳解,ant打包,ant編譯後打包去掉jar文件
java中采用Pull解析器對XML文件進行解析
6月22日雲棲精選夜讀:業界首個非侵入式熱修複方案Sophix重磅推出,顛覆移動端傳統更新流程!
醫療行業互聯網營銷思路
Big Data Application Case Study – Technical Architecture of a Big Data Platform
VB6中從內存中(Byte 字節數組)加載圖片
互聯網企業安全高級指南3.7.4 SDL在互聯網企業的發展
App製作幹貨:真旺雲開發App之如何配置iOS發布?