最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例

 更新時(shí)間:2022年03月09日 14:33:27   作者:后臺(tái)小白鼠  
本文主要介紹了springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

啟用@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上無效
2.注解添加到interface Service類上無效
3.注解添加到interface Service方法上無效

那么,此注解應(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)文章

最新評(píng)論

潍坊市| 淮安市| 海南省| 彰化县| 星子县| 招远市| 隆回县| 肃北| 宽城| 高邮市| 河池市| 静海县| 上蔡县| 岑巩县| 永济市| 神农架林区| 深州市| 垣曲县| 印江| 阜城县| 富源县| 松滋市| 堆龙德庆县| 黄山市| 吴堡县| 蓝田县| 平原县| 远安县| 叶城县| 水富县| 琼海市| 和政县| 南岸区| 哈尔滨市| 崇明县| 都昌县| 津市市| 宽城| 额尔古纳市| 阳新县| 巴南区|