激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring boot + mybatis實現動態切換數據源實例代碼

spring boot + mybatis實現動態切換數據源實例代碼

2021-06-09 13:44擼碼識途 Java教程

這篇文章主要給大家介紹了關于spring boot + mybatis實現動態切換數據源的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

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

下面話不多說了,隨著小編來一起看看詳細的介紹吧

方法如下:

1.加個依賴

?
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
#主從數據庫
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默認加載數據源配置

?
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.數據源配置類

?
1
2
3
4
5
6
7
8
9
10
11
/**
 * 主數據源
 */
@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
/**
 * 從數據源配置
 */
@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
/**
 * 數據源配置類
 */
@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//不加這個會報錯。
 @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.數據源上下文

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * 數據源上下文
 */
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
/*
* 重寫的函數決定了最后選擇的datasource
*/
public class multiroutedatasource extends abstractroutingdatasource {
 
 @override
 protected object determinecurrentlookupkey() {
  //通過綁定線程的數據源上下文實現多數據源的動態切換,有興趣的可以去查閱資料或源碼
  return datasourcecontext.getdatasource();
 }
 
}

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

?
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;
}

這種是在業務中使用代碼設置數據源的方式,也可以使用aop+注解的方式實現控制,方法多多!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本黄网 | 一级黄色大片在线观看 | 久久久国产一区二区三区 | 久久婷婷一区二区三区 | 欧美日韩免费观看视频 | 日本黄色一级毛片 | 久久免费视频7 | 久久91精品 | 日本成人一区二区三区 | 一级黄色在线免费观看 | 久久精品国产清自在天天线 | 国产精品久久久久久婷婷天堂 | 免费欧美一级视频 | 亚洲小视频在线 | 国产成人自拍视频在线 | 久久电影一区二区 | 精品91av| 色视频在线播放 | 91成人午夜性a一级毛片 | 国产精品.com | 欧美日韩免费看 | 在线 日本 制服 中文 欧美 | 国产精品一区自拍 | 精品一区二区三区免费看 | 露脸各种姿势啪啪的清纯美女 | 播色网 | 亚洲午夜精品视频 | 成人nv在线观看 | 精品久久久久久久久久久久 | 久久午夜免费视频 | 男女无遮挡羞羞视频 | 久久久噜噜噜久久熟有声小说 | 色婷婷久久久久久 | 国产午夜精品一区二区三区不卡 | 久久久鲁 | 嗯哈~不行好大h双性 | 国产精品久久久久久久久久了 | 国产精品久久久久久久久久大牛 | 国产正在播放 | 国产精品成人亚洲一区二区 | 久久99国产伦子精品免费 |