springboot druid mybatis多數(shù)據(jù)源配置方式
springboot druid mybatis多數(shù)據(jù)源配置
spring boot 在配置時(shí)做了很多簡(jiǎn)化配置的設(shè)置,但是簡(jiǎn)化的配置往往已犧牲一定的定制化,比如在數(shù)據(jù)源的配置時(shí),spring boot 只提供4種數(shù)據(jù)庫(kù)連接池的配置,其中并不支持常用的druid
閱讀spring boot DataSourceBuilder 的源碼可以發(fā)現(xiàn) spring boot 提供的4種數(shù)據(jù)源類型并不是我們想要的
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方法來(lái)自定義DataSource類型
public DataSourceBuilder type(Class<? extends DataSource> type) { this.type = type; return this; }
知道了方法,下面配置就簡(jiǎn)單許多了
首先是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
然后是具體的主數(shù)據(jù)源配置類
@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”) 引入配置項(xiàng)_
使用如下方式創(chuàng)建DruidDataSource
簡(jiǎn)化配置
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
第二數(shù)據(jù)源
@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); } }
數(shù)據(jù)源配置完畢
每個(gè)數(shù)據(jù)源都會(huì)生成自己的sqlSession,相互獨(dú)立
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
說(shuō)說(shuō)在Spring中如何引用外部屬性文件的方法
這篇文章主要介紹了說(shuō)說(shuō)在Spring中如何引用外部屬性文件的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Java 字符數(shù)組轉(zhuǎn)字符串的常用方法
文章總結(jié)了在Java中將字符數(shù)組轉(zhuǎn)換為字符串的幾種常用方法,包括使用String構(gòu)造函數(shù)、String.valueOf()方法、StringBuilder以及Arrays.toString()方法,每種方法都有其適用的場(chǎng)景和性能特點(diǎn),感興趣的朋友跟隨小編一起看看吧2025-01-01springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明
這篇文章主要介紹了springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01springboot獲取微信JSDK簽名信息的實(shí)現(xiàn)示例
本文介紹了如何在Spring Boot應(yīng)用中獲取微信JSDK的簽名信息,包括獲取接口URL、參數(shù)設(shè)置、簽名算法和獲取簽名結(jié)果的步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11Java監(jiān)聽(tīng)器ActionListener與MouseListener的執(zhí)行順序說(shuō)明
這篇文章主要介紹了Java監(jiān)聽(tīng)器ActionListener與MouseListener的執(zhí)行順序說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java中方法優(yōu)先調(diào)用可選參數(shù)還是固定參數(shù)
這篇文章主要介紹了Java中方法優(yōu)先調(diào)用可選參數(shù)還是固定參數(shù),可選參數(shù)是?JDK?5?中新增的特性,也叫變長(zhǎng)參數(shù)或可變參數(shù),固定參數(shù)的概念恰好與可選參數(shù)相反,固定參數(shù)也就是普通的參,下文更多詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-05-05Java實(shí)現(xiàn)動(dòng)態(tài)規(guī)劃背包問(wèn)題
本文主要介紹使用java實(shí)現(xiàn)動(dòng)態(tài)規(guī)劃的背包問(wèn)題,詳細(xì)使用圖文和多種案例進(jìn)行解析,幫助理解該算法2021-06-06