今天就跟大家聊聊有關(guān)Spring boot 中怎么連接多數(shù)據(jù)源,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)擁有10多年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站服務(wù),對(duì)于網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、app開發(fā)定制、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、空間域名等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。
1.application.yml中添加兩個(gè)datasource
server: port: 8080 spring: application: name: king datasource: master: type: com.zaxxer.hikari.HikariDataSource jdbc-url: jdbc:MySQL://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 idle-timeout: 10000 minimum-idle: 1 maximum-pool-size: 10 pool-name: master auto-commit: false connection-test-query: SELECT 1 slave: type: com.zaxxer.hikari.HikariDataSource jdbc-url: jdbc:mysql://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 idle-timeout: 10000 minimum-idle: 1 maximum-pool-size: 10 pool-name: slave auto-commit: false connection-test-query: SELECT 1
2.手動(dòng)初始化數(shù)據(jù)源masterDataSource
package cn.bj.king.config; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; 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.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; /** * @author ARongking * @date 2019-03-28 */ @Configuration @EnableTransactionManagement @MapperScan(basePackages = {"cn.bj.king.mapper.master"}, sqlSessionFactoryRef = "masterSqlSessionFactory") public class WriteDataSourceConfig { @Bean(name = "masterDataSource") @Primary @Qualifier("masterDataSource") public DataSource masterDataSource(@Qualifier("masterHikariConfig")HikariConfig hikariConfig) { System.out.println("實(shí)例化主庫(kù)"); HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig); return hikariDataSource; } /** * 配置連接池信息 * @return */ @ConfigurationProperties(prefix = "spring.datasource.master") @Bean("masterHikariConfig") public HikariConfig masterHikariConfig(){ HikariConfig hikariConfig=new HikariConfig(); return hikariConfig; } /** * SqlSessionFactory配置 * * @return * @throws Exception */ @Bean(name = "masterSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // 配置mapper文件位置 sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/master/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("cn.bj.king.entity"); return sqlSessionFactoryBean.getObject(); } /** * 配置事物管理器 * * @return */ @Bean(name = "masterTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }
3.手動(dòng)初始化數(shù)據(jù)源ReadDataSource
package cn.bj.king.config; import com.github.pagehelper.PageInterceptor; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; 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.core.env.Environment; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.util.Properties; @Configuration @EnableTransactionManagement @MapperScan(basePackages = {"cn.bj.king.mapper.slave"}, sqlSessionFactoryRef = "slaveSqlSessionFactory") public class ReadDataSourceConfig { @Autowired Environment environment; @Bean(name = "slaveDataSource") @Qualifier("slaveDataSource") public DataSource slaveDataSource(@Qualifier("slaveHikariConfig")HikariConfig hikariConfig) { System.out.println("實(shí)例化從庫(kù)"); HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig); return hikariDataSource; } /** * 配置連接池信息 * @return */ @ConfigurationProperties(prefix = "spring.datasource.slave") @Bean("slaveHikariConfig") public HikariConfig slaveHikariConfig(){ HikariConfig hikariConfig=new HikariConfig(); return hikariConfig; } /** * SqlSessionFactory配置 * * @return * @throws Exception */ @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // 配置mapper文件位置 sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/slave/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("cn.bj.king.entity"); return sqlSessionFactoryBean.getObject(); } /** * 配置事物管理器 * * @return */ @Bean(name = "slaveTransactionManager") public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }
4.測(cè)試啟動(dòng)是否正常初始化master和slave 兩個(gè)數(shù)據(jù)源
5.這里需要注意幾個(gè)地方:
配置數(shù)據(jù)庫(kù)連接池的時(shí)候,如果不是手動(dòng)設(shè)置HikariConfig ,即通過(guò)Environment 去一個(gè)個(gè)獲取數(shù)據(jù)庫(kù)連接池在yml中的配置的話,數(shù)據(jù)庫(kù)連接池的(idle-timeout,minimum-idle, maximum-pool-size,pool-name,auto-commit,connection-test-query)等,一定要與slave節(jié)點(diǎn)平級(jí),不然 HikariConfig 是讀取不到這些信息的。
如果手動(dòng)賦值HikariConfig 的話,那就沒有什么限制了,寫到哪里都行,只需要用Environment 去讀取配置文件的值,手動(dòng)設(shè)置到HikariConfig 就行了。
看完上述內(nèi)容,你們對(duì)Spring boot 中怎么連接多數(shù)據(jù)源有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章標(biāo)題:Springboot中怎么連接多數(shù)據(jù)源
標(biāo)題網(wǎng)址:http://muchs.cn/article22/iioejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、小程序開發(fā)、全網(wǎng)營(yíng)銷推廣、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)