前言
前幾天有個(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