mybatis-flex實(shí)現(xiàn)多數(shù)據(jù)源操作
多數(shù)據(jù)源?
MyBaits-Flex 內(nèi)置了功能完善的多數(shù)據(jù)源支持^1.0.6,不需要借助第三方插件或者依賴,開箱即用, 支持包括 druid、hikaricp、dbcp2、beecp 在內(nèi)的任何數(shù)據(jù)源,MyBatis-Flex 多數(shù)據(jù)源配置如下:
mybatis-flex:
datasource:
ds1:
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
ds2:
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456在以上配置中,ds1 和 ds2 是由用戶自定義的,我們可以理解為數(shù)據(jù)源的名稱,或者數(shù)據(jù)源的 key,這個(gè)在動(dòng)態(tài)切換數(shù)據(jù)庫中非常有用。
在無 Spring 框架的場(chǎng)景下,代碼如下:
DataSource dataSource1 = new HikariDataSource();
dataSource1.setJdbcUrl(....)
DataSource dataSource2 = new HikariDataSource();
dataSource2.setJdbcUrl(....)
DataSource dataSource3 = new HikariDataSource();
dataSource3.setJdbcUrl(....)
MybatisFlexBootstrap.getInstance()
.setDataSource("ds1", dataSource1)
.addDataSource("ds2", dataSource2)
.addDataSource("ds3", dataSource3)
.start();開始使用?
默認(rèn)使用第一個(gè)配置的數(shù)據(jù)源:
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);通過編碼的方式,切換到數(shù)據(jù)源 ds2:
try{
DataSourceKey.use("ds2")
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);
}finally{
DataSourceKey.clear();
}或者
List<Row> rows = DataSourceKey.use("ds2"
, () -> Db.selectAll("tb_account"));數(shù)據(jù)源切換(設(shè)置)?
MyBatis-Flex 提供了 4 種方式來配置數(shù)據(jù)源:
- 1、編碼,使用
DataSourceKey.use方法。 - 2、
@UseDataSource("dataSourceName")在 Mapper 類上,添加注解,用于指定使用哪個(gè)數(shù)據(jù)源。 - 3、
@UseDataSource("dataSourceName")在 Mapper 方法上,添加注解,用于指定使用哪個(gè)數(shù)據(jù)源。 - 4、
@Table(dataSource="dataSourceName")在 Entity 類上添加注解,該 Entity 的增刪改查請(qǐng)求默認(rèn)使用該數(shù)據(jù)源。
在 SpringBoot 項(xiàng)目上,
@UseDataSource("dataSourceName")也可用于在 Controller 或者 Service 上。若是 Spring 項(xiàng)目(非 SpringBoot), 用戶需要參考MultiDataSourceAutoConfiguration進(jìn)行配置后才能使用。
DataSourceKey.use 示例:
try{
DataSourceKey.use("ds2")
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);
}finally{
DataSourceKey.clear();
}@UseDataSource("dataSourceName") 示例:
@UseDataSource("ds2")
interface AccountMapper extends BaseMapper{
List<Account> myMethod();
}或者
interface AccountMapper extends BaseMapper{
@UseDataSource("ds2")
List<Account> myMethod();
}@Table(dataSource="dataSourceName") 示例:
@Table(value = "tb_account", dataSource = "ds2")
public class Account {
@Id(keyType = KeyType.Auto)
private Long id;
private String userName;
}數(shù)據(jù)源配置的優(yōu)先級(jí)
DataSourceKey.use() > @UseDataSource()在方法上 > @UseDataSource()在類上 >@Table(dataSource="...")
更多的 Spring Yaml 配置支持?
mybatis-flex:
datasource:
ds1:
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
ds2:
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456在以上配置中,ds1 和 ds2 并未指定使用哪個(gè)數(shù)據(jù)連接池,MyBatis-Flex 會(huì) 自動(dòng)探測(cè) 當(dāng)前項(xiàng)目依賴了哪個(gè)連接池。 目前支持了 druid、hikaricp、dbcp2、beecp 的自動(dòng)探測(cè),如果項(xiàng)目中使用的不是這 4 種類型只有,需要添加 type 配置,如下所示:
mybatis-flex:
datasource:
ds1:
type: com.your.datasource.type1
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
ds2:
type: com.your.datasource.type2
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456同時(shí),項(xiàng)目若使用到了多個(gè)數(shù)據(jù)源類型,則也需要添加 type 來指定當(dāng)前數(shù)據(jù)源的類型。
除了 type、url、username、password 的配置以外,MyBatis-Flex 支持該 DataSource 類型的所有參數(shù)配置, 例如,在 DruidDataSource 類中存在 setAsyncInit 方法,我們就可以添加 asyncInit 的配置,如下所示:
mybatis-flex:
datasource:
ds1:
type: druid
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
asyncInit: true
ds2:
type: com.your.datasource.type2
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456因此,只要該 DataSource 有 setter 方法,我們就可以在配置文件中進(jìn)行配。與此相反的是:若該 DataSource 類型沒有該屬性,則不能使用這個(gè)配置。
提示
在數(shù)據(jù)源的配置中,type 可以配置為某個(gè) DataSource 的類名,也可以配置為別名,別名支持有:druid、 hikari、hikaricp、bee、beecp、dbcp、dbcp2。
動(dòng)態(tài)添加新的數(shù)據(jù)源?
在多租戶等某些場(chǎng)景下,我們可能需要用到動(dòng)態(tài)的添加新的數(shù)據(jù)源,此時(shí)可以通過如下的方式進(jìn)行添加。
FlexDataSource flexDataSource = FlexGlobalConfig.getDefaultConfig()
.getDataSource();
//新的數(shù)據(jù)源
HikariDataSource newDataSource = new HikariDataSource();
flexDataSource.addDataSource("newKey", newDataSource);需要注意的是: 通過 FlexGlobalConfig 去獲取 FlexDataSource 時(shí),需等待應(yīng)用完全啟動(dòng)成功后,才能正常獲取 FlexDataSource, 否則將會(huì)得到 null 值。
Spring 用戶可以通過 ApplicationListener 去監(jiān)聽 ContextRefreshedEvent 事件,然后再去添加新的數(shù)據(jù)源,如下代碼所示:
public class DataSourceInitListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
FlexDataSource dataSource = FlexGlobalConfig.getDefaultConfig()
.getDataSource();
dataSource.addDataSource("....", new DruidDataSource("..."));
}
}多數(shù)據(jù)源負(fù)載均衡 ^1.5.4?
數(shù)據(jù)源負(fù)載均衡指的是:在進(jìn)行數(shù)據(jù)查詢的時(shí)候,隨機(jī)使用一個(gè)數(shù)據(jù)源。 這是的在高并發(fā)的場(chǎng)景下,起到負(fù)載的效果。
假設(shè)多數(shù)據(jù)源配置如下:
mybatis-flex:
datasource:
ds1:
type: druid
url: jdbc:mysql://127.0.0.1:3306/db
username: root
password: 123456
asyncInit: true
ds2:
type: com.your.datasource.type2
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456
other:
type: com.your.datasource.type2
url: jdbc:mysql://127.0.0.1:3306/db2
username: root
password: 123456以上配置了三個(gè)數(shù)據(jù)源,分別為:ds1、ds2、other,假設(shè)我們想負(fù)載均衡使用 ds1、ds2 ,那么代碼如下:
try{
DataSourceKey.use("ds*");
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);
}finally{
DataSourceKey.clear();
}DataSourceKey.use("ds*") 中的 ds* 指的是使用 ds 開頭的任意一個(gè)數(shù)據(jù)源。ds* 必須以 "*" 結(jié)尾, 中間不能有空格,比如 "ds *" 中間有空格是不行的。
到此這篇關(guān)于mybatis-flex實(shí)現(xiàn)多數(shù)據(jù)源操作的文章就介紹到這了,更多相關(guān)mybatis-flex 多數(shù)據(jù)源操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis-Flex實(shí)現(xiàn)分頁查詢的示例代碼
- Mybatis-flex整合達(dá)夢(mèng)數(shù)據(jù)庫的實(shí)現(xiàn)示例
- Spring Boot整合MyBatis-Flex全過程
- SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫訪問
- mybatis-flex實(shí)現(xiàn)鏈?zhǔn)讲僮鞯氖纠a
- MyBatis-Flex實(shí)現(xiàn)多表聯(lián)查(自動(dòng)映射)
- Springboot集成Mybatis-Flex的示例詳解
- mybatis-flex與springBoot整合的實(shí)現(xiàn)示例
- MyBatis-Flex 邏輯刪除的用法小結(jié)
相關(guān)文章
SpringBoot+Jersey跨域文件上傳的實(shí)現(xiàn)示例
在SpringBoot開發(fā)后端服務(wù)時(shí),我們一般是提供接口給前端使用,本文主要介紹了SpringBoot+Jersey跨域文件上傳的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
SpringBoot使用?Sleuth?進(jìn)行分布式跟蹤的過程分析
Spring Boot Sleuth是一個(gè)分布式跟蹤解決方案,它可以幫助您在分布式系統(tǒng)中跟蹤請(qǐng)求并分析性能問題,Spring Boot Sleuth是Spring Cloud的一部分,它提供了分布式跟蹤的功能,本文將介紹如何在Spring Boot應(yīng)用程序中使用Sleuth進(jìn)行分布式跟蹤,感興趣的朋友一起看看吧2023-10-10
Java heap space OOM 精準(zhǔn)定位與體系化排查方案詳解
精準(zhǔn)定位Java堆內(nèi)存溢出(OOM)需要結(jié)合監(jiān)控、JVM參數(shù)、內(nèi)存快照和可視化工具,本文介紹Java heap space OOM 精準(zhǔn)定位與體系化排查方案,感興趣的朋友一起看看吧2026-04-04
Java中消息隊(duì)列任務(wù)的平滑關(guān)閉詳解
對(duì)于消息隊(duì)列的監(jiān)聽,我們一般使用Java寫一個(gè)獨(dú)立的程序,在Linux服務(wù)器上運(yùn)行。程序啟動(dòng)后,通過消息隊(duì)列客戶端接收消息,放入一個(gè)線程池進(jìn)行異步處理,并發(fā)的快速處理。這篇文章主要給大家介紹了關(guān)于Java中消息隊(duì)列任務(wù)的平滑關(guān)閉的相關(guān)資料,需要的朋友可以參考下。2017-11-11
Java中this關(guān)鍵字用法梳理詳細(xì)講解
this關(guān)鍵字是java常用的關(guān)鍵字,可用于任何實(shí)例方法內(nèi)指向當(dāng)前對(duì)象,也可指向?qū)ζ湔{(diào)用當(dāng)前方法的對(duì)象,下面這篇文章主要介紹了Java中this關(guān)鍵字用法梳理的相關(guān)資料,需要的朋友可以參考下2025-07-07
SpringBoot3集成SLF4J+logback進(jìn)行日志記錄的實(shí)現(xiàn)
本文主要介紹了SpringBoot3集成SLF4J+logback進(jìn)行日志記錄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Spring中的注解之@Override和@Autowired
看別人寫的代碼,經(jīng)常會(huì)用到 @Override 和 @Autowired 這兩個(gè)注解.這邊總結(jié)一下這兩個(gè)注解的作用,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05

