MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼
多數(shù)據(jù)源的目的在于一個(gè)代碼模塊可調(diào)用多個(gè)數(shù)據(jù)庫的數(shù)據(jù)進(jìn)行某些業(yè)務(wù)操作。
MyBatis-Plus開發(fā)者寫了一個(gè)多數(shù)據(jù)源叫dynamic-datasource-spring-boot-starter ,非常簡單易用。
dynamic-datasource-spring-boot-starter文檔
官方文檔部分截圖:
第三方集成的,基本上是目前比較主流的(用的比較多)。
一、添加Maven依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.4</version> </dependency>
二、配置文件修改(application.yml)
spring: datasource: dynamic: primary: db1 #設(shè)置默認(rèn)的數(shù)據(jù)源,默認(rèn)值為master datasource: db1: #數(shù)據(jù)源db1 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/wordpress_master?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 db2: #數(shù)據(jù)源db2 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/wordpress_slave?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 10 max-active: 100 min-idle: 10 max-wait: 60000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 #Oracle需要打開注釋 #validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: false test-on-return: false stat-view-servlet: enabled: true url-pattern: /druid/* #login-username: admin #login-password: admin filter: stat: log-slow-sql: true slow-sql-millis: 1000 merge-sql: false wall: config: multi-statement-allow: true
三、完成成1、2步后,啟動(dòng)應(yīng)用
如果控制臺不報(bào)錯(cuò)且出現(xiàn)如下圖所示,就表示成功整合:
四、注意事項(xiàng)
啟動(dòng)主類需要排除Druid相關(guān)依賴,否則會出現(xiàn)如下錯(cuò)誤:
***************************
APPLICATION FAILED TO START
***************************Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
解決辦法,加上如下代碼即可:
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
到此這篇關(guān)于MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis-Plus 多數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題
- MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例
- SpringBoot整合Mybatis-Plus+Druid實(shí)現(xiàn)多數(shù)據(jù)源配置功能
- Mybatis-plus多數(shù)據(jù)源配置的兩種方式總結(jié)
- MyBatis-Plus 集成動(dòng)態(tài)多數(shù)據(jù)源的實(shí)現(xiàn)示例
- Mybatis-Plus的多數(shù)據(jù)源你了解嗎
- Mybatis-Plus進(jìn)階分頁與樂觀鎖插件及通用枚舉和多數(shù)據(jù)源詳解
- Spring Boot + Mybatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的方法
- MyBatis-Plus多數(shù)據(jù)源的示例代碼
相關(guān)文章
Java實(shí)現(xiàn)excel動(dòng)態(tài)列導(dǎo)出的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)excel動(dòng)態(tài)列導(dǎo)出,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Spring security自定義用戶認(rèn)證流程詳解
這篇文章主要介紹了Spring security自定義用戶認(rèn)證流程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Java實(shí)現(xiàn)開箱即用的redis分布式鎖
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)開箱即用的基于redis的分布式鎖,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以收藏一下2022-12-12Java農(nóng)夫過河問題的繼承與多態(tài)實(shí)現(xiàn)詳解
這篇文章主要介紹了Java農(nóng)夫過河問題的繼承與多態(tài)實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01從0構(gòu)建Oauth2Server服務(wù)之Refreshing-access-tokens
這篇文章主要為大家介紹了從0構(gòu)建Oauth2Server服務(wù)之Refreshing-access-tokens刷新令牌示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05