使用@DS輕松解決動態(tài)數(shù)據(jù)源的問題
@DS解決動態(tài)數(shù)據(jù)源問題
動態(tài)切換數(shù)據(jù)源,無非是繼承org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource這個類,重寫determineCurrentLookupKey()這個方法,動態(tài)變換數(shù)據(jù)源的key值,有人已經(jīng)將詳細代碼封裝到框架中,我們只需要使用它的注解@DS就好。
引入maven
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.baomidou</groupId> ? ? ? ? ? ? <artifactId>dynamic-datasource-spring-boot-starter</artifactId> ? ? ? ? ? ? <version>3.2.1</version> ? ? ? ? </dependency>
修改Application.yml
參考的是官網(wǎng)的例子
spring: ? datasource: ? ? dynamic: ? ? ? primary: master #設(shè)置默認的數(shù)據(jù)源或者數(shù)據(jù)源組,默認值即為master ? ? ? strict: false #設(shè)置嚴格模式,默認false不啟動. 啟動后在未匹配到指定數(shù)據(jù)源時候會拋出異常,不啟動則使用默認數(shù)據(jù)源. ? ? ? datasource: ? ? ? ? master: ? ? ? ? ? url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic ? ? ? ? ? username: root ? ? ? ? ? password: 123456 ? ? ? ? ? driver-class-name: com.mysql.jdbc.Driver # 3.2.0開始支持SPI可省略此配置 ? ? ? ? slave_1: ? ? ? ? ? url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic ? ? ? ? ? username: root ? ? ? ? ? password: 123456 ? ? ? ? ? driver-class-name: com.mysql.jdbc.Driver ? ? ? ? slave_2: ? ? ? ? ? url: ENC(xxxxx) # 內(nèi)置加密,使用請查看詳細文檔 ? ? ? ? ? username: ENC(xxxxx) ? ? ? ? ? password: ENC(xxxxx) ? ? ? ? ? driver-class-name: com.mysql.jdbc.Driver ? ? ? ? ? schema: db/schema.sql # 配置則生效,自動初始化表結(jié)構(gòu) ? ? ? ? ? data: db/data.sql # 配置則生效,自動初始化數(shù)據(jù) ? ? ? ? ? continue-on-error: true # 默認true,初始化失敗是否繼續(xù) ? ? ? ? ? separator: ";" # sql默認分號分隔符 ? ? ? ? ?? ? ? ? ?#......省略 ? ? ? ?#以上會配置一個默認庫master,一個組slave下有兩個子庫slave_1,slave_2
這是我自己的:
spring: ? datasource: ? ? dynamic: ? ? ? primary: master #設(shè)置默認的數(shù)據(jù)源或者數(shù)據(jù)源組,默認值即為master #設(shè)置嚴格模式,默認false不啟動. 啟動后在未匹配到指定數(shù)據(jù)源時候會拋出異常,不啟動則使用默認數(shù)據(jù)源. ? ? ? datasource: ? ? ? ? master: ? ? ? ? ? url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=balabala ? ? ? ? ? username: sa ? ? ? ? ? password: 123456Sa ? ? ? ? ? driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver ? ? ? ? sub: ? ? ? ? ? url: jdbc:mysql://127.0.0.1:3307/balabala?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai ? ? ? ? ? username: sa ? ? ? ? ? password: 123456Sa ? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver
在需要使用非默認數(shù)據(jù)庫的dao層的方法上加上注解@DS(“數(shù)據(jù)庫名”)即可
@DS("sub") int selectBusiness(@Param("startTime") String startTime, ? ? ? ? ? ? ? ? ? ?@Param("endTime") String endTime, ? ? ? ? ? ? ? ? ? ?@Param("businessType") String businessType);
@DS注解說明
寫法:
@DS(value = "數(shù)據(jù)源名稱") datasource:? 數(shù)據(jù)源名臣
注解在類上或方法上來切換數(shù)據(jù)源
Gradle:
com.baomidou:dynamic-datasource-spring-boot-starter:2.5.3
打開一個線程得方法
new Thread(() -> { ? ? 方法內(nèi)容------------------------------------- }).start();
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Spring MVC在Controller層中注入request的坑詳解
這篇文章主要給大家介紹了關(guān)于Spring MVC在Controller層中注入request的坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧。2018-04-04Java編程一維數(shù)組轉(zhuǎn)換成二維數(shù)組實例代碼
這篇文章主要介紹了Java編程一維數(shù)組轉(zhuǎn)換成二維數(shù)組,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01SpringBoot3中數(shù)據(jù)庫集成實踐詳解
項目工程中,集成數(shù)據(jù)庫實現(xiàn)對數(shù)據(jù)的增曬改查管理,是最基礎(chǔ)的能力,所以下面小編就來和大家講講SpringBoot3如何實現(xiàn)數(shù)據(jù)庫集成,需要的可以參考下2023-08-08關(guān)于Mybatis使用collection分頁問題
項目中mybatis分頁的場景是非常高頻的,當使用ResultMap并配置collection做分頁的時候,我們可能會遇到獲取當前頁的數(shù)據(jù)少于每頁大小的數(shù)據(jù)問題。接下來通過本文給大家介紹Mybatis使用collection分頁問題,感興趣的朋友一起看看吧2021-11-11Java 判斷字符串a(chǎn)和b是否互為旋轉(zhuǎn)詞
本篇文章主要介紹了判斷字符串a(chǎn)和b是否互為旋轉(zhuǎn)詞的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05