Springboot多數(shù)據(jù)源配置之整合dynamic-datasource方式
多數(shù)據(jù)源配置之整合dynamic-datasource
技術:SpringBoot + Mybatis-Plus + Druid
SpringBoot里做多數(shù)據(jù)源配置,可以直接使用dynamic-datasource提供的服務,簡單便捷
POM里加入依賴包
<!--多數(shù)據(jù)源依賴--> ? ? <dependency> ? ? ? <groupId>com.baomidou</groupId> ? ? ? <artifactId>dynamic-datasource-spring-boot-starter</artifactId> ? ? ? <version>3.3.1</version> ? ? </dependency>
yml增加多數(shù)據(jù)配置:
autoconfigure:
? ? # 自動化配置 例外處理
? ? exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
? datasource:
? ? type: com.alibaba.druid.pool.DruidDataSource
? ? #多數(shù)據(jù)源配置
? ? dynamic:
? ? ? #設置默認的數(shù)據(jù)源
? ? ? primary: master
? ? ? datasource:
? ? ? ? # 數(shù)據(jù)庫1
? ? ? ? master:
? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver
? ? ? ? ? url: jdbc:mysql://localhost:3306/praticce_a?useUnicode=true&characterEncoding=utf-8&useSSL=false
? ? ? ? ? username: root
? ? ? ? ? password: e0BhauLW2yhi2se9L+7QFaBsPMVgdPR1udDSqZe75Yh5Obp8YHyRw==
? ? ? ? ? druid:
? ? ? ? ? ? filters: stat,slf4j,wall,config
? ? ? ? ? ? initial-size: 10
? ? ? ? ? ? max-active: 100
? ? ? ? ? ? max-open-prepared-statements: 20
? ? ? ? ? ? max-pool-prepared-statement-per-connection-size: 20
? ? ? ? ? ? max-wait: 60000
? ? ? ? ? ? min-evictable-idle-time-millis: 300000
? ? ? ? ? ? min-idle: 10
? ? ? ? ? ? pool-prepared-statements: true
? ? ? ? ? ? test-on-borrow: false
? ? ? ? ? ? test-on-return: false
? ? ? ? ? ? test-while-idle: true
? ? ? ? ? ? time-between-eviction-runs-millis: 60000
? ? ? ? ? ? validation-query: select 'x'
? ? ? ? ? ? publicKey: MFwwDQYJKoZIhvcNAr7HWXiJQjmIZN6IYPMPzfRe20da5d8EAAQ==
? ? ? ? ? ? connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.dynamic.datasource.master.druid.publicKey}
? ? ? ? # 數(shù)據(jù)庫2
? ? ? ? slave1:
? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver
? ? ? ? ? url: jdbc:mysql://localhost:3306/pratice_b?useUnicode=true&characterEncoding=utf-8
? ? ? ? ? username: root
? ? ? ? ? password: EVHrRAfXPl0Qpd6j4GsodQOwvsV9Q==
? ? ? ? ? druid:
? ? ? ? ? ? filters: stat,slf4j,wall,config
? ? ? ? ? ? initial-size: 10
? ? ? ? ? ? max-active: 100
? ? ? ? ? ? max-open-prepared-statements: 20
? ? ? ? ? ? max-pool-prepared-statement-per-connection-size: 20
? ? ? ? ? ? max-wait: 60000
? ? ? ? ? ? min-evictable-idle-time-millis: 300000
? ? ? ? ? ? min-idle: 10
? ? ? ? ? ? pool-prepared-statements: true
? ? ? ? ? ? test-on-borrow: false
? ? ? ? ? ? test-on-return: false
? ? ? ? ? ? test-while-idle: true
? ? ? ? ? ? time-between-eviction-runs-millis: 60000
? ? ? ? ? ? validation-query: select 'x'
? ? ? ? ? ? publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwQ==
? ? ? ? ? ? connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.dynamic.datasource.msg.druid.publicKey}說明:
數(shù)據(jù)庫配置這里使用了Druid的加密方式,如果數(shù)據(jù)庫連接密碼不需要加密,則password可以直接使用明文,druid配置中去掉config配置(filters中去掉config,connection-properties可以都去掉)
spring.datasource.dynamic.primary是用于設置默認的數(shù)據(jù)源,這個最好設置一個,因為我們不可能每個類或接口都指定數(shù)據(jù)源
spring.autoconfigure.exclude是去除Druid自動裝載數(shù)據(jù)庫配置,也可以直接在項目啟動類XXXApplication上加
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
在需要切換數(shù)據(jù)源的類或方法上加@DS注解
@Mapper
@DS("msg")
public interface TestMapper {
?
? ? /**
? ? ?* 批量數(shù)據(jù)
? ? ?*
? ? ?* @param poList 數(shù)據(jù)
? ? ?*/
? ? void batchAdd(@Param("poList") List<TestDTO> poList);
?
}@DS優(yōu)先級:方法 > 類
dynamic-datasource-spring-boot-starter多數(shù)據(jù)源 + pagehelper出現(xiàn)分頁異常
解決方法
pagehelper: autoRuntimeDialect: true
在yml配置文件添加如上配置即可,注意必須是駝峰型,這個坑是真的坑

后續(xù)
經(jīng)過查看源碼發(fā)現(xiàn),PageHelper的配置類不是使用普通的Bean,而是繼承Properties類。
斷點發(fā)現(xiàn),代碼都沒有進入
public void setAutoRuntimeDialect(Boolean autoRuntimeDialect) {
this.setProperty("autoRuntimeDialect", autoRuntimeDialect.toString());
}
導致這個參數(shù)配置的沒有生效。
搞得我還一直納悶是不是dynamic-datasource-spring-boot-starter的問題
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java如何實現(xiàn)http接口參數(shù)和返回值加密
這篇文章主要介紹了Java如何實現(xiàn)http接口參數(shù)和返回值加密問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

