
(資料圖)
項(xiàng)目基本配置參考??SpringBoot入門一,使用myEclipse新建一個(gè)SpringBoot項(xiàng)目??,使用MyEclipse新建一個(gè)SpringBoot項(xiàng)目即可,數(shù)據(jù)源使用MyBatis。下面開始多數(shù)據(jù)源的整合
com.baomidou dynamic-datasource-spring-boot-starter 3.5.2
# ----------------數(shù)據(jù)庫連接基本配置---------------## 連接池類型,這里我們采用hikari連接池spring.datasource.type=com.zaxxer.hikari.HikariDataSource## 設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值即為masterspring.datasource.dynamic.primary=master## 嚴(yán)格匹配數(shù)據(jù)源,默認(rèn)false. true未匹配到指定數(shù)據(jù)源時(shí)拋異常,false使用默認(rèn)數(shù)據(jù)源spring.datasource.dynamic.strict=false# ----------------多數(shù)據(jù)源信息配置---------------## 默認(rèn)數(shù)據(jù)源("master"為指定的數(shù)據(jù)源名稱,對(duì)應(yīng)spring.datasource.dynamic.primary的值)spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.dynamic.datasource.master.url=jdbc:mysql://127.0.0.1:3307/qfx_test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8spring.datasource.dynamic.datasource.master.username=rootspring.datasource.dynamic.datasource.master.password=666666## 讀庫數(shù)據(jù)源(可多個(gè),"db2"為指定的數(shù)據(jù)源名稱,自定義即可)spring.datasource.dynamic.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.dynamic.datasource.db2.url=jdbc:mysql://127.0.0.1:3307/qfx_test2?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8spring.datasource.dynamic.datasource.db2.username=rootspring.datasource.dynamic.datasource.db2.password=666666
使用說明
1 約定 (1)配置文件所有以下劃線"_"分割的數(shù)據(jù)源,下劃線"_"之前的為組名稱,同組名稱的數(shù)據(jù)源會(huì)放在一個(gè)組下 (2)切換數(shù)據(jù)源可以是組名,也可以是具體數(shù)據(jù)源名稱,組名則切換時(shí)采用負(fù)載均衡算法切換 (3)默認(rèn)的數(shù)據(jù)源名稱為"master",可以通過"spring.datasource.dynamic.primary"修改 (4)方法上的注解優(yōu)先于類上注解2 多數(shù)據(jù)源配置方案 # 一主多從 # 多主多從 純粹多庫(記得設(shè)置primary) 混合配置 spring: spring: spring: spring: datasource: datasource: datasource: datasource: dynamic: dynamic: dynamic: dynamic: datasource: datasource: datasource: datasource: master: master_1: mysql: master: slave_1: master_2: oracle: slave_1: slave_2: slave_1: sqlserver: slave_2: slave_3: slave_2: postgresql: oracle_1: slave_4: slave_3: h2: oracle_2:
1.在DAO類中添加”@DS”注解信息,使用@DS切換數(shù)據(jù)源.
2."@DS"可以注解在方法上或類上,如果同時(shí)存在則采用就近原則:
方法上注解 優(yōu)先于 類上注解,如果什么都不寫則使用默認(rèn)數(shù)據(jù)源
3.實(shí)際上到這一步多數(shù)據(jù)源配置就已經(jīng)完成了
注解 結(jié)果----------------------------------------------------沒有@DS 默認(rèn)數(shù)據(jù)源@DS("dsName") dsName可以為組名也可以為具體某個(gè)庫的名稱示例: @DS("slave") public interface StudentDao { @DS("slave_1") Student selectByPK(Long id); ListselectAll(); }
使用:DAO類
import java.util.List;import com.baomidou.dynamic.datasource.annotation.DS;import com.qfx.modules.student.entity.Student;/** * @DS可以注解在方法上或類上,如果同時(shí)存在則采用就近原則:方法上注解 優(yōu)先于 類上注解 */@DS("db2")public interface StudentDao { @DS("db2") Student selectByPK(Long id); ListselectAll(); int insert(Student student);}
多數(shù)據(jù)源建議使用@DSTransactional
@DSTransactionalpublic MapaddStudent(Student student) { return studentDao.insert(student);}
可去官網(wǎng)查看詳細(xì)使用說明:
??https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter ??