SpringBoot入門三十一,多數(shù)據(jù)源的使用

2022-12-22 14:01:27 來源:51CTO博客


(資料圖)

一、環(huán)境配置

項(xiàng)目基本配置參考??SpringBoot入門一,使用myEclipse新建一個(gè)SpringBoot項(xiàng)目??,使用MyEclipse新建一個(gè)SpringBoot項(xiàng)目即可,數(shù)據(jù)源使用MyBatis。下面開始多數(shù)據(jù)源的整合

二、pom.xml主要配置信息

  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:

四、多數(shù)據(jù)源使用

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);        List selectAll();    }

使用: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);      List selectAll();    int insert(Student student);}

五、事物控制

多數(shù)據(jù)源建議使用@DSTransactional

@DSTransactionalpublic Map addStudent(Student student) {  return studentDao.insert(student);}

六、官網(wǎng)

可去官網(wǎng)查看詳細(xì)使用說明:

??https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter ??

標(biāo)簽: 使用說明 基本配置 配置文件

上一篇:Kubernetes監(jiān)控手冊(cè)07-監(jiān)控controller-manager
下一篇:世界快看:設(shè)置及查看Linux的環(huán)境變量詳細(xì)教程