springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例
啟用@DS實(shí)現(xiàn)數(shù)據(jù)源切換
POM內(nèi)添加核心jar包
?? ??? ?<dependency> ? ? ? ? ? ? <groupId>com.baomidou</groupId> ? ? ? ? ? ? <artifactId>dynamic-datasource-spring-boot-starter</artifactId> ? ? ? ? ? ? <version>3.0.0</version> ? ? ? ? </dependency>
yml配置
spring: ? datasource: ? ? #配置hikari連接池 ? ? hikari: ? ? ? minimum-idle: 4 ? ? ? maximum-pool-size: 16 ? ? ? connection-timeout: 10000 ? ? ? idle-timeout: 30000 ? ? ? connection-init-sql: set names utf8mb4 ? ? #動(dòng)態(tài)數(shù)據(jù)源配置 ? ? dynamic: ? ? ? #主數(shù)據(jù)源,默認(rèn)啟用 ? ? ? primary: business ? ? ? datasource: ? ? ? ? #數(shù)據(jù)源1 ? ? ? ? business: ? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver ? ? ? ? ? url: jdbc:mysql://localhost:3306/db_business?useUnicode=true&characterEncoding=utf-8 ? ? ? ? ? username: **** ? ? ? ? ? password: **** ? ? ? ? #數(shù)據(jù)源2 ? ? ? ? user: ? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver ? ? ? ? ? url: jdbc:mysql://localhost:3306/db_user?useUnicode=true&characterEncoding=utf-8 ? ? ? ? ? username: **** ? ? ? ? ? password: **** ? ? ? ? #數(shù)據(jù)源3 ? ? ? ? order: ? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver ? ? ? ? ? url: jdbc:mysql://localhost:3306/db_order?useUnicode=true&characterEncoding=utf-8 ? ? ? ? ? username: **** ? ? ? ? ? password: ****
“核心”-使用@DS注解
使用@DS注解的核心是什么呢?
1.注解添加在dao.mapper上無(wú)效
2.注解添加到interface Service類上無(wú)效
3.注解添加到interface Service方法上無(wú)效
那么,此注解應(yīng)該如何使用呢?
添加@DS注解到實(shí)現(xiàn)類或者實(shí)現(xiàn)類的方法上才可以
當(dāng)注解添加到類上,意味著此類里的方法都使用此數(shù)據(jù)源;
當(dāng)注解添加到方法上時(shí),意味著此方法上使用的數(shù)據(jù)源優(yōu)先級(jí)高于其他一切配置
@Service @DS("slave") public class UserServiceImpl implements UserService { @Autowired private JdbcTemplate jdbcTemplate; public List<Map<String, Object>> selectAll() { return jdbcTemplate.queryForList("select * from user"); } @Override @DS("slave_1") public List<Map<String, Object>> selectByCondition() { return jdbcTemplate.queryForList("select * from user where age >10"); }
最后
好了,就說這么多了,數(shù)據(jù)源切換的方式、策略有多種,比如按包區(qū)分不同數(shù)據(jù)源的方式、手動(dòng)配置的數(shù)據(jù)源的方式,以及@DS注解的方式,該方式的最大特點(diǎn),相對(duì)比較靈活,配置簡(jiǎn)潔易懂,后期維護(hù)及迭代比較樂觀。
到此這篇關(guān)于springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例的文章就介紹到這了,更多相關(guān)springboot @DS注解數(shù)據(jù)源切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基礎(chǔ)高級(jí)綜合練習(xí)題撲克牌的創(chuàng)建
今天小編就為大家分享一篇關(guān)于Java基礎(chǔ)高級(jí)綜合練習(xí)題撲克牌的創(chuàng)建,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01Spring+Junit4進(jìn)行接口測(cè)試實(shí)例代碼
這篇文章主要介紹了Spring+Junit4進(jìn)行接口測(cè)試實(shí)例代碼,涉及pom.xml、bean的配置,以及接口測(cè)試代碼等相關(guān)內(nèi)容,小編覺得還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下2018-01-01Mybatis動(dòng)態(tài)Sql標(biāo)簽使用小結(jié)
本文主要介紹了Mybatis動(dòng)態(tài)Sql標(biāo)簽使用,常用的動(dòng)態(tài)sql標(biāo)簽包括?if、choose(when、otherwise)、trim(where、set)、foreach,下面就來(lái)介紹一下2024-04-04Java 十進(jìn)制轉(zhuǎn)二、八、十六進(jìn)制的字符串
本文主要介紹了十進(jìn)制轉(zhuǎn)二進(jìn)制;十進(jìn)制轉(zhuǎn)八進(jìn)制;十進(jìn)制轉(zhuǎn)十六進(jìn)制的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02基于SpringBoot實(shí)現(xiàn)圖片上傳及圖片回顯
本篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)圖片上傳及圖片回顯,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-08-08