一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼

spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼

2021-06-09 13:44擼碼識(shí)途 Java教程

這篇文章主要給大家介紹了關(guān)于spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

前幾天有個(gè)需求,需要使用不同的數(shù)據(jù)源,例如某業(yè)務(wù)要用a數(shù)據(jù)源,另一個(gè)業(yè)務(wù)要用b數(shù)據(jù)源。我上網(wǎng)收集了一些資料整合了一下,雖然最后這個(gè)需求不了了之了,但是多數(shù)據(jù)源動(dòng)態(tài)切換還是蠻好用的,所以記錄一下,或許以后有用呢?或者自己感興趣又想玩呢!

下面話不多說(shuō)了,隨著小編來(lái)一起看看詳細(xì)的介紹吧

方法如下:

1.加個(gè)依賴

?
1
2
3
4
5
<dependency>
 <groupid>org.mybatis.spring.boot</groupid>
 <artifactid>mybatis-spring-boot-starter</artifactid>
      <version>1.3.1</version>
</dependency>

2.application.properties配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
#主從數(shù)據(jù)庫(kù)
master.db.driverclassname=com.mysql.jdbc.driver
master.db.url=jdbc:mysql://localhost:3306/cbd?characterencoding=utf-8&useunicode=true&usessl=false
master.db.username=root
master.db.password=admin
slave.db.driverclassname=com.mysql.jdbc.driver
slave.db.url=jdbc:mysql://localhost:3306/cbd_test?characterencoding=utf-8&useunicode=true&usessl=false
slave.db.username=root
slave.db.password=admin
 
mybatis.config-location= classpath:config/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/**/*.xml

3.禁用springboot默認(rèn)加載數(shù)據(jù)源配置

?
1
2
3
4
5
6
7
@springbootapplication(exclude = {datasourceautoconfiguration.class})
public class application {
 
 public static void main(string[] args) throws exception {
  springapplication.run(application.class, args);
 }
}

4.數(shù)據(jù)源配置類

?
1
2
3
4
5
6
7
8
9
10
11
/**
 * 主數(shù)據(jù)源
 */
@configuration
@configurationproperties(prefix = "master.db")
public class masterdatasourceconfig {
 private string url;
 private string username;
 private string password;
 private string driverclassname;
}
?
1
2
3
4
5
6
7
8
9
10
11
/**
 * 從數(shù)據(jù)源配置
 */
@configuration
@configurationproperties(prefix = "slave.db")
public class slavedatasourceconfig {
 private string url;
 private string username;
 private string password;
 private string driverclassname;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * 數(shù)據(jù)源配置類
 */
@configuration
public class datasourcecomponent {
 
 @resource
 private masterdatasourceconfig masterdatasourceconfig;
 
 @resource
 private slavedatasourceconfig slavedatasourceconfig;
 
 
  @bean(name = "master")
 public datasource masterdatasource() {
  datasource datasource = new datasource();
  datasource.seturl(masterdatasourceconfig.geturl());
  datasource.setusername(masterdatasourceconfig.getusername());
  datasource.setpassword(masterdatasourceconfig.getpassword());
  datasource.setdriverclassname(masterdatasourceconfig.getdriverclassname());
  return datasource;
 }
 
 @bean(name = "slave")
 public datasource slavedatasource() {
  datasource datasource = new datasource();
  datasource.seturl(slavedatasourceconfig.geturl());
  datasource.setusername(slavedatasourceconfig.getusername());
  datasource.setpassword(slavedatasourceconfig.getpassword());
  datasource.setdriverclassname(slavedatasourceconfig.getdriverclassname());
  return datasource;
 }
 
 @primary//不加這個(gè)會(huì)報(bào)錯(cuò)。
 @bean(name = "multidatasource")
 public multiroutedatasource exampleroutedatasource() {
  multiroutedatasource multidatasource = new multiroutedatasource();
  map<object, object> targetdatasources = new hashmap<>();
  targetdatasources.put("master", masterdatasource());
  targetdatasources.put("slave", slavedatasource());
  multidatasource.settargetdatasources(targetdatasources);
  multidatasource.setdefaulttargetdatasource(masterdatasource());
  return multidatasource;
 }
}

5.數(shù)據(jù)源上下文

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * 數(shù)據(jù)源上下文
 */
public class datasourcecontext {
 private static final threadlocal<string> contextholder = new threadlocal<>();
 
 public static void setdatasource(string value) {
  contextholder.set(value);
 }
 
 public static string getdatasource() {
  return contextholder.get();
 }
 
 public static void cleardatasource() {
  contextholder.remove();
 }
}

6.datasource路由類

?
1
2
3
4
5
6
7
8
9
10
11
12
/*
* 重寫(xiě)的函數(shù)決定了最后選擇的datasource
*/
public class multiroutedatasource extends abstractroutingdatasource {
 
 @override
 protected object determinecurrentlookupkey() {
  //通過(guò)綁定線程的數(shù)據(jù)源上下文實(shí)現(xiàn)多數(shù)據(jù)源的動(dòng)態(tài)切換,有興趣的可以去查閱資料或源碼
  return datasourcecontext.getdatasource();
 }
 
}

7.使用,修改上下文中的數(shù)據(jù)源就可以切換自己想要使用的數(shù)據(jù)源了。

?
1
2
3
4
5
6
public uservo finduser(string username) {
 datasourcecontext.setdatasource("slave");
 uservo uservo = usermapper.findbyvo(username);
 system.out.println(uservo.getname()+"=====================");
 return null;
}

這種是在業(yè)務(wù)中使用代碼設(shè)置數(shù)據(jù)源的方式,也可以使用aop+注解的方式實(shí)現(xiàn)控制,方法多多!

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。

原文鏈接:http://www.cnblogs.com/tinyj/p/9864128.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 奇米888在线看奇米999 | 男老头澡堂gay老头456 | 日韩精品视频美在线精品视频 | 成人在线一区二区三区 | 国产精品福利一区二区亚瑟 | 欧美日韩一区二区三在线 | 火影小南被爆羞羞网站 | 色婷婷影院在线视频免费播放 | 国产90后美女露脸在线观看 | 无人区在线观看免费完整版免费 | av91在线 | 四虎影院在线免费播放 | 国产亚洲精品美女久久久 | 手机免费在线视频 | 日本成人免费在线视频 | 亚洲视频观看 | 欧美精品一区二区三区免费播放 | 欧美人与禽杂交大片 | 国产精品午夜性视频网站 | 女教师系列三上悠亚在线观看 | 女人爽到喷水的视频免费看 | 久草高清在线 | caoporn超碰最新地址进入 | 国产区成人综合色在线 | 91精品国产91久久久久 | 久久re这里精品23 | 国内精品91久久久久 | 国产精品一久久香蕉产线看 | 国产精品久久久免费视频 | 黑人草| 欧美日韩亚洲综合在线一区二区 | 青春草视频在线免费观看 | 鬼吹灯天星术在线高清观看 | 久久久久伊人 | 边摸边操 | 新版孕妇bbwbbwbbw | 激情自拍网 | 久久国产36精品色熟妇 | 色综合伊人色综合网站中国 | china中国小帅gayxnxx | 操女人bb|