mybatis-flex實現(xiàn)多數據源操作
多數據源?
MyBaits-Flex 內置了功能完善的多數據源支持^1.0.6,不需要借助第三方插件或者依賴,開箱即用, 支持包括 druid、hikaricp、dbcp2、beecp 在內的任何數據源,MyBatis-Flex 多數據源配置如下:
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 是由用戶自定義的,我們可以理解為數據源的名稱,或者數據源的 key,這個在動態(tài)切換數據庫中非常有用。
在無 Spring 框架的場景下,代碼如下:
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();開始使用?
默認使用第一個配置的數據源:
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);通過編碼的方式,切換到數據源 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"));數據源切換(設置)?
MyBatis-Flex 提供了 4 種方式來配置數據源:
- 1、編碼,使用
DataSourceKey.use方法。 - 2、
@UseDataSource("dataSourceName")在 Mapper 類上,添加注解,用于指定使用哪個數據源。 - 3、
@UseDataSource("dataSourceName")在 Mapper 方法上,添加注解,用于指定使用哪個數據源。 - 4、
@Table(dataSource="dataSourceName")在 Entity 類上添加注解,該 Entity 的增刪改查請求默認使用該數據源。
在 SpringBoot 項目上,
@UseDataSource("dataSourceName")也可用于在 Controller 或者 Service 上。若是 Spring 項目(非 SpringBoot), 用戶需要參考MultiDataSourceAutoConfiguration進行配置后才能使用。
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;
}數據源配置的優(yōu)先級
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 并未指定使用哪個數據連接池,MyBatis-Flex 會 自動探測 當前項目依賴了哪個連接池。 目前支持了 druid、hikaricp、dbcp2、beecp 的自動探測,如果項目中使用的不是這 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同時,項目若使用到了多個數據源類型,則也需要添加 type 來指定當前數據源的類型。
除了 type、url、username、password 的配置以外,MyBatis-Flex 支持該 DataSource 類型的所有參數配置, 例如,在 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 方法,我們就可以在配置文件中進行配。與此相反的是:若該 DataSource 類型沒有該屬性,則不能使用這個配置。
提示
在數據源的配置中,type 可以配置為某個 DataSource 的類名,也可以配置為別名,別名支持有:druid、 hikari、hikaricp、bee、beecp、dbcp、dbcp2。
動態(tài)添加新的數據源?
在多租戶等某些場景下,我們可能需要用到動態(tài)的添加新的數據源,此時可以通過如下的方式進行添加。
FlexDataSource flexDataSource = FlexGlobalConfig.getDefaultConfig()
.getDataSource();
//新的數據源
HikariDataSource newDataSource = new HikariDataSource();
flexDataSource.addDataSource("newKey", newDataSource);需要注意的是: 通過 FlexGlobalConfig 去獲取 FlexDataSource 時,需等待應用完全啟動成功后,才能正常獲取 FlexDataSource, 否則將會得到 null 值。
Spring 用戶可以通過 ApplicationListener 去監(jiān)聽 ContextRefreshedEvent 事件,然后再去添加新的數據源,如下代碼所示:
public class DataSourceInitListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
FlexDataSource dataSource = FlexGlobalConfig.getDefaultConfig()
.getDataSource();
dataSource.addDataSource("....", new DruidDataSource("..."));
}
}多數據源負載均衡 ^1.5.4?
數據源負載均衡指的是:在進行數據查詢的時候,隨機使用一個數據源。 這是的在高并發(fā)的場景下,起到負載的效果。
假設多數據源配置如下:
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以上配置了三個數據源,分別為:ds1、ds2、other,假設我們想負載均衡使用 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 開頭的任意一個數據源。ds* 必須以 "*" 結尾, 中間不能有空格,比如 "ds *" 中間有空格是不行的。
到此這篇關于mybatis-flex實現(xiàn)多數據源操作的文章就介紹到這了,更多相關mybatis-flex 多數據源操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot使用Thymeleaf自定義標簽的實例代碼
這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標簽的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java中WeakHashMap和HashMap的區(qū)別詳解
這篇文章主要介紹了Java中WeakHashMap和HashMap的區(qū)別詳解,WeakHashMap和HashMap一樣,WeakHashMap也是一個散列表,它存儲的內容也是鍵值對(key-value)映射,而且鍵和值都可以為null,需要的朋友可以參考下2023-09-09
Java利用HttpClient模擬POST表單操作應用及注意事項
本文主要介紹JAVA中利用HttpClient模擬POST表單操作,希望對大家有所幫助。2016-04-04
java中LinkedBlockingQueue與ArrayBlockingQueue的異同
這篇文章主要介紹了java中LinkedBlockingQueue與ArrayBlockingQueue的異同,需要的朋友可以參考下2016-08-08

