SpringBoot+mybatis實(shí)現(xiàn)多數(shù)據(jù)源支持操作
什么是多數(shù)據(jù)源支持?
簡(jiǎn)單的說,就是一個(gè)項(xiàng)目里,同時(shí)可以訪問多個(gè)不同的數(shù)據(jù)庫。
實(shí)現(xiàn)原理
單個(gè)數(shù)據(jù)源在配置時(shí)會(huì)綁定一套mybatis配置,多個(gè)數(shù)據(jù)源時(shí),不同的數(shù)據(jù)源綁定不同的mybatis配置就可以了,簡(jiǎn)單的思路就是讓不同的數(shù)據(jù)源掃描不同的包,讓不同的包下的mapper對(duì)應(yīng)連接不同的數(shù)據(jù)源去處理邏輯。
業(yè)務(wù)場(chǎng)景假設(shè)
項(xiàng)目底層有正常業(yè)務(wù)庫和日志庫,希望解決的是將項(xiàng)目中的一些日志單獨(dú)記錄到一個(gè)庫里,比如用戶操作記錄、產(chǎn)品更新記錄等。
說一下為什么會(huì)有這個(gè)需求:用戶操作記錄和產(chǎn)品更新記錄可能很多,而實(shí)際中使用的又很少,就只是在某些頁面單獨(dú)展示一下操作或更新記錄,絕大部分時(shí)間都在不停的做著插入操作,這時(shí)就可以把這種記錄放到業(yè)務(wù)核心庫外面。
實(shí)現(xiàn)步驟
1.定義多個(gè)數(shù)據(jù)源的mybatis配置
application.properties mybatis.mapper-locations=mappers/*.xml mybatisLog.mapper-locations=mappersLog/*.xml ## datasource master # spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test1?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=466420182 ## datasource log # spring.datasourceLog.type=com.alibaba.druid.pool.DruidDataSource spring.datasourceLog.driver-class-name=com.mysql.jdbc.Driver spring.datasourceLog.url=jdbc:mysql://localhost:3306/log?characterEncoding=UTF-8 spring.datasourceLog.username=root spring.datasourceLog.password=466420182
2.定義多個(gè)數(shù)據(jù)源
@Configuration public class DatasourceConfig { @Bean(destroyMethod = "close", name = DataSources.MASTER_DB) @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } @Bean(destroyMethod = "close", name = DataSources.LOG_DB) @ConfigurationProperties(prefix = "spring.datasourceLog") public DataSource dataSourceLog() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } }
3.分別配置多個(gè)數(shù)據(jù)源
@Configuration @MapperScan(basePackages = {"com.mmall.practice.dao"}) public class MybatisConfig { @Autowired @Qualifier(DataSources.MASTER_DB) private DataSource masterDB; @Bean @Primary @ConfigurationProperties(prefix = "mybatis") public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(masterDB); return sqlSessionFactoryBean; } }
@Configuration @MapperScan(basePackages = {"com.mmall.practice.daoLog"}, sqlSessionFactoryRef = "logSqlSessionFactory") public class MybatisLogConfig { @Autowired @Qualifier("logDB") private DataSource logDB; @Bean(name = "logSqlSessionFactory") @ConfigurationProperties(prefix = "mybatisLog") public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(logDB); return sqlSessionFactoryBean; } }
這里需要注意兩個(gè)數(shù)據(jù)源配置的差別,也是支持多數(shù)據(jù)源的關(guān)鍵
1)Configuration 掃描不同的前綴,取不同包下的sql對(duì)應(yīng)的xml文件
2)SqlSessionFactoryBean 實(shí)例化時(shí),默認(rèn)的額外添加了 @Primary注解
3)MapperScan 掃描的不同的包,如果掃描相同的包也能做,但是還需要做額外的配置,可以自己嘗試
4)不同的數(shù)據(jù)源使用不同的SqlSessionFactoryBean實(shí)例
至此,不同包下面的 Mapper.java 文件就可以連接不同的數(shù)據(jù)源了。這里就不說如何去使用了,和之前正常一樣去使用 Mapper.java 就可以了,只是操作的是不同的數(shù)據(jù)庫。
補(bǔ)充知識(shí):springboot+mybatis多數(shù)據(jù)源不用增加硬編碼,只需簡(jiǎn)單配置即可
背景
原有系統(tǒng)增加統(tǒng)計(jì)功能,數(shù)據(jù)源有本地系統(tǒng)的數(shù)據(jù),還有其他系統(tǒng)數(shù)據(jù)。其他系統(tǒng)數(shù)據(jù)可以同步到mysql表。但是又不想與當(dāng)前頁面表混用,打算使用另外的庫,并且不想單獨(dú)提供接口,想通過當(dāng)前系統(tǒng)配置數(shù)據(jù)源來實(shí)現(xiàn)此功能。
目前常用的方式是分包或切面等,感覺要改的地方比較多,最后采用了一種改動(dòng)最簡(jiǎn)單的方式來實(shí)現(xiàn)多數(shù)據(jù)源,shardingjdbc這種方式,感覺比較簡(jiǎn)單,而且便于日后分庫分表的拓展。
項(xiàng)目實(shí)施
目標(biāo)
系統(tǒng)增加一個(gè)數(shù)據(jù)源,統(tǒng)計(jì)數(shù)據(jù),此數(shù)據(jù)源與系統(tǒng)原有數(shù)據(jù)源不發(fā)生關(guān)系。
當(dāng)前環(huán)境
viewer.sql:原業(yè)務(wù)庫,大概有二十多張表(當(dāng)前舉例,展示user,role)
test.sql:新增的統(tǒng)計(jì)庫,目前只使用一張表(模擬統(tǒng)計(jì)測(cè)試數(shù)據(jù)zhy)
使用mybatis,自動(dòng)生成了原業(yè)務(wù)的mapper和test中zhy表的數(shù)據(jù)
代碼
第一步 引入shardingjdbc依賴
gradle
compile 'org.apache.shardingsphere:sharding-jdbc-spring-boot-starter:4.0.1'
maven
<dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.1</version> </dependency>
第二步 配置properties
原來連接數(shù)據(jù)庫
# jdbc_config datasource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/viewer?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 # Hikari will use the above plus the following to setup connection pooling spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=15 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=DatebookHikariCP spring.datasource.hikari.max-lifetime=1800000 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.connection-test-query=SELECT 1
修改后
spring.shardingsphere.datasource.names=ds-viewer,ds-test # 系統(tǒng)原有數(shù)據(jù)源 spring.shardingsphere.datasource.ds-viewer.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds-viewer.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds-viewer.jdbc-url=jdbc:mysql://127.0.0.1:3306/viewer?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC spring.shardingsphere.datasource.ds-viewer.username=root spring.shardingsphere.datasource.ds-viewer.password=123456 # 新增統(tǒng)計(jì)數(shù)據(jù)源 spring.shardingsphere.datasource.ds-test.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds-test.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds-test.jdbc-url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC spring.shardingsphere.datasource.ds-test.username=root spring.shardingsphere.datasource.ds-test.password=123456 # 規(guī)則 spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds-viewer.user spring.shardingsphere.sharding.tables.role.actual-data-nodes=ds-viewer.role spring.shardingsphere.sharding.tables.zhy.actual-data-nodes=ds-test.zhy
主要需要看,配置分片規(guī)則這塊,根據(jù)表名進(jìn)行分庫。
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds-viewer.user
解釋:
user:原有數(shù)據(jù)庫中的表
ds-viewer:定義的數(shù)據(jù)源spring.shardingsphere.datasource.names=ds-viewer,ds-test
有幾張表就可以定義幾張(可以使用idea縱列編輯,很方便改造)
大功告成,可以進(jìn)行開發(fā)測(cè)試了,對(duì)于代碼層來說,沒有任何改動(dòng)。
如果系統(tǒng)有配置文件,有的配置啟動(dòng)不需要多數(shù)據(jù)源,可以在配置文件中禁止啟動(dòng)shardingjdbc
spring.shardingsphere.enabled=false
以上這篇SpringBoot+mybatis實(shí)現(xiàn)多數(shù)據(jù)源支持操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
- Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳解
- 詳解SpringBoot和Mybatis配置多數(shù)據(jù)源
- springboot + mybatis配置多數(shù)據(jù)源示例
- mybatis-plus返回map自動(dòng)轉(zhuǎn)駝峰配置操作
- SpringBoot配置mybatis駝峰命名規(guī)則自動(dòng)轉(zhuǎn)換的實(shí)現(xiàn)
- mybatisplus解決駝峰命名映射問題詳解
- SpringBoot項(xiàng)目多數(shù)據(jù)源及mybatis 駝峰失效的問題解決方法
相關(guān)文章
SpringBoot整合Spring?Boot?Admin實(shí)現(xiàn)服務(wù)監(jiān)控的方法
這篇文章主要介紹了SpringBoot整合Spring?Boot?Admin實(shí)現(xiàn)服務(wù)監(jiān)控,內(nèi)容包括Server端服務(wù)開發(fā),Client端服務(wù)開發(fā)其中Spring Boot Admin還可以對(duì)其監(jiān)控的服務(wù)提供告警功能,如服務(wù)宕機(jī)時(shí),可以及時(shí)以郵件方式通知運(yùn)維人員,感興趣的朋友跟隨小編一起看看吧2022-03-03Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例
這篇文章主要介紹了Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Mybatis動(dòng)態(tài)SQL?foreach批量操作方法
這篇文章主要介紹了Mybatis動(dòng)態(tài)SQL?foreach批量操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03JAVA開發(fā)中的一些規(guī)范講解(阿里巴巴Java開發(fā)規(guī)范手冊(cè))
這篇文章主要介紹了JAVA開發(fā)中的一些規(guī)范講解(阿里巴巴Java開發(fā)規(guī)范手冊(cè)),需要的朋友可以參考下2018-04-04Java內(nèi)部類應(yīng)用之靜態(tài)內(nèi)部類應(yīng)用示例
這篇文章主要介紹了Java內(nèi)部類應(yīng)用之靜態(tài)內(nèi)部類應(yīng)用,結(jié)合實(shí)例形式分析了Java靜態(tài)內(nèi)部類的原理、功能、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09Java反射機(jī)制,反射相關(guān)API,反射API使用方式(反射獲取實(shí)體類字段名和注解值)
這篇文章主要介紹了Java反射機(jī)制,反射相關(guān)API,反射API使用方式(反射獲取實(shí)體類字段名和注解值),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式
我們?cè)谶M(jìn)行前后端分離開發(fā)的時(shí)候,一般是將前端項(xiàng)目部署到nginx服務(wù)器上,與后端項(xiàng)目分開部署,這篇文章主要給大家介紹了關(guān)于SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式的相關(guān)資料,需要的朋友可以參考下2024-01-01