SpringBoot+mybatis實(shí)現(xiàn)多數(shù)據(jù)源支持操作
什么是多數(shù)據(jù)源支持?
簡單的說,就是一個(gè)項(xiàng)目里,同時(shí)可以訪問多個(gè)不同的數(shù)據(jù)庫。
實(shí)現(xiàn)原理
單個(gè)數(shù)據(jù)源在配置時(shí)會(huì)綁定一套mybatis配置,多個(gè)數(shù)據(jù)源時(shí),不同的數(shù)據(jù)源綁定不同的mybatis配置就可以了,簡單的思路就是讓不同的數(shù)據(jù)源掃描不同的包,讓不同的包下的mapper對(duì)應(yīng)連接不同的數(shù)據(jù)源去處理邏輯。
業(yè)務(wù)場景假設(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ù)源不用增加硬編碼,只需簡單配置即可
背景
原有系統(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)最簡單的方式來實(shí)現(xiàn)多數(shù)據(jù)源,shardingjdbc這種方式,感覺比較簡單,而且便于日后分庫分表的拓展。
項(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ì)測試數(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ā)測試了,對(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)文章
詳解Java中的時(shí)間處理與時(shí)間標(biāo)準(zhǔn)
這篇文章主要為大家詳細(xì)介紹了三個(gè)時(shí)間標(biāo)準(zhǔn)GMT,CST,UTC的規(guī)定,以及Java進(jìn)行時(shí)間處理的相關(guān)知識(shí),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
Java?Stream實(shí)現(xiàn)多字段分組groupingBy操作詳解
Stream是Java8的一個(gè)新特性,主要用戶集合數(shù)據(jù)的處理,如排序、過濾、去重等等功能,本文就來講講如何利用Stream實(shí)現(xiàn)比較優(yōu)雅的按多字段進(jìn)行分組groupingBy吧2023-06-06
springboot多數(shù)據(jù)源配置及切換的示例代碼詳解
這篇文章主要介紹了springboot多數(shù)據(jù)源配置及切換,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Spring Boot使用過濾器和攔截器分別實(shí)現(xiàn)REST接口簡易安全認(rèn)證示例代碼詳解
這篇文章主要介紹了Spring Boot使用過濾器和攔截器分別實(shí)現(xiàn)REST接口簡易安全認(rèn)證示例代碼,通過開發(fā)實(shí)踐,理解過濾器和攔截器的工作原理,需要的朋友可以參考下2018-06-06
解決java攔截器獲取POST入?yún)?dǎo)致@RequestBody參數(shù)丟失問題
文章講述了在Java開發(fā)中使用攔截器獲取POST請(qǐng)求入?yún)r(shí),由于流關(guān)閉導(dǎo)致`@RequestBody`參數(shù)丟失的問題,并提出了一種解決方案,解決方案包括自定義方法、防止流丟失、過濾器和攔截器的合理組織和使用,最終確保了參數(shù)的正確傳遞2024-11-11

