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

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

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

服務器之家 - 編程語言 - Java教程 - Spring配置多數據源切換

Spring配置多數據源切換

2021-06-27 17:14丶Melody Java教程

今天小編就為大家分享一篇關于Spring配置多數據源切換,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

多數據源切換

db.properties

?
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#mysql
jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/test?autoreconnect=true&characterencoding=utf-8
jdbc.username=root
jdbc.password=admin
#定義初始連接數
initialsize=0
#定義最大連接數
maxactive=1000
#定義最大空閑
maxidle=20
#定義最小空閑
minidle=1
#定義最長等待時間
maxwait=60000
#mysql
# driverclassname 根據url自動識別 這一項可配可不配,如果不配置druid會根據url自動識別dbtype,然后選擇相應的driverclassname
jdbc.driver1=com.mysql.jdbc.driver
jdbc.url1=jdbc:mysql://localhost:3306/test1?allowmultiqueries=true&autoreconnect=true&characterencoding=utf-8
jdbc.username1=root
jdbc.password1=admin
# 初始化時建立物理連接的個數。初始化發生在顯示調用init方法,或者第一次getconnection時
initialsize1=0
# 最大連接池數量
maxactive1=1000
#定義最小空閑
minidle1=1
# 獲取連接時最大等待時間,單位毫秒。配置了maxwait之后,
# 缺省啟用公平鎖,并發效率會有所下降,
# 如果需要可以通過配置useunfairlock屬性為true使用非公平鎖。
maxwait1=60000
# druid 監控
# 屬性類型是字符串,通過別名的方式配置擴展插件,
# 常用的插件有:
# 監控統計用的filter:stat
# 日志用的filter:log4j
# 防御sql注入的filter:wall
filters1=stat,log4j
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
timebetweenevictionrunsmillis1=60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minevictableidletimemillis1=300000
# 建議配置為true,不影響性能,并且保證安全性。
# 申請連接的時候檢測,如果空閑時間大于
# timebetweenevictionrunsmillis,
# 執行validationquery檢測連接是否有效。
testwhileidle1=true
# 申請連接時執行validationquery檢測連接是否有效,做了這個配置會降低性能。
testonborrow1=false
# 歸還連接時執行validationquery檢測連接是否有效,做了這個配置會降低性能
testonreturn1=false
# 是否緩存preparedstatement,也就是pscache。
# pscache對支持游標的數據庫性能提升巨大,比如說oracle。
# 在mysql5.5以下的版本中沒有pscache功能,建議關閉掉。
# 作者在5.5版本中使用pscache,通過監控界面發現pscache有緩存命中率記錄,
# 該應該是支持pscache。
poolpreparedstatements1=false
# 要啟用pscache,必須配置大于0,當大于0時,
# poolpreparedstatements自動觸發修改為true
# 在druid中,不會存在oracle下pscache占用內存過多的問題,
# 可以把這個數值配置大一些,比如說100
maxopenpreparedstatements1=-1

applicationcontext.xml

?
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:websocket="http://www.springframework.org/schema/websocket"
  xsi:schemalocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/websocket
    http://www.springframework.org/schema/websocket/spring-websocket.xsd">
  <!-- spring使用注解 配置掃描  com.ys下的掃描 但是在springmvc里邊已經掃描一次了
   這時就要加上屬性 use-default-filters="true"  這個屬性就是使用默認的掃描
   默認就掃描com.ys下所有 設置為false 在下邊配置需要掃描的部分-->
   <!-- 現在的配置就只會掃描帶@service@repository注解的類 -->
  <context:component-scan base-package="com" use-default-filters="false">
  <!-- org.springframework.stereotype.service就是注解@service 這個注解使用在service里 所以就是掃描service包
  org.springframework.stereotype.repository  repository倉庫-->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.controller"/>
    <context:include-filter type="annotation" expression="org.springframework.beans.factory.annotation.autowired"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.service"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.repository"/>
  </context:component-scan>
  <!-- 讀取properties文件 -->
  <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
    <property name="locations">
      <list>
        <value>classpath:db.properties</value>
      </list>
    </property>
  </bean>
  <!-- 配置連接池數據源  文檔搜索basicdatasource -->
  <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">
    <!-- results in a setdriverclassname(string) call -->
    <property name="driverclassname" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <!-- 初始化連接大小 -->
    <property name="initialsize" value="${initialsize}"></property>
    <!-- 連接池最大數量 -->
    <property name="maxactive" value="${maxactive}"></property>
    <!-- 連接池最大空閑 -->
    <property name="maxidle" value="${maxidle}"></property>
    <!-- 連接池最小空閑 -->
    <property name="minidle" value="${minidle}"></property>
    <!-- 獲取連接最大等待時間 -->
    <property name="maxwait" value="${maxwait}"></property>
  </bean>
  <bean id="datasource1" class="com.alibaba.druid.pool.druiddatasource" destroy-method="close">
    <property name="url" value="${jdbc.url1}" />
    <property name="username" value="${jdbc.username1}" />
    <property name="password" value="${jdbc.password1}" />
    <property name="driverclassname" value="${jdbc.driver1}" />
    <!-- 初始化連接大小 -->
    <property name="initialsize" value="${initialsize1}"/>
    <!-- 最小空閑 -->
    <property name="minidle" value="${minidle1}" />
    <!-- 最大連接池數量 -->
    <property name="maxactive" value="${maxactive1}" />
    <!-- 獲取連接最大等待時間 -->
    <property name="maxwait" value="${maxwait1}"/>
    <!-- 監控統計用的filter:stat  日志用的filter:log4j 防御sql注入的filter:wall -->
    <property name="filters" value="${filters1}" />
    <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 -->
    <property name="timebetweenevictionrunsmillis" value="${timebetweenevictionrunsmillis1}" />
    <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
    <property name="minevictableidletimemillis" value="${minevictableidletimemillis1}" />
    <!-- 建議配置為true,不影響性能,并且保證安全性。 申請連接的時候檢測 -->
    <property name="testwhileidle" value="${testwhileidle1}"/>
    <!-- 申請連接時執行validationquery檢測連接是否有效,做了這個配置會降低性能。 -->
    <property name="testonborrow" value="${testonborrow1}"/>
    <!-- 歸還連接時執行validationquery檢測連接是否有效,做了這個配置會降低性能 -->
    <property name="testonreturn" value="false" />
    <!-- 是否緩存preparedstatement,也就是pscache 適用支持游標的數據庫 如oracle -->
    <property name="poolpreparedstatements" value="${poolpreparedstatements1}"/>
    <!-- 要啟用pscache,必須配置大于0,當大于0時 poolpreparedstatements自動觸發修改為true。 -->
    <property name="maxopenpreparedstatements" value="${maxopenpreparedstatements1}"/>
    <!-- 定義監控日志輸出間隔 -->
    <property name="timebetweenlogstatsmillis" value="60000"/>
    <!--<property name="statlogger" ref ="statloggerb"/> -->
    <!-- 若需要mybatis的批量sql需配置   不配置則報錯:nested exception is java.sql.sqlexception: sql injection violation, multi-statement not allow-->
    <property name="proxyfilters" ref="wall-filter"/>
  </bean>
  <!-- 若需要mybatis的批量sql需配置 -->
  <bean id="wall-filter" class="com.alibaba.druid.wall.wallfilter">
    <property name="config" ref="wall-config" />
  </bean>
  <bean id="wall-config" class="com.alibaba.druid.wall.wallconfig">
    <property name="multistatementallow" value="true" />
  </bean>
  <!-- 多數據源配置 -->
  <bean id="multipledatasource" class="com.ys.dbconfig.multipledatasource">
    <!-- 默認數據源 -->
    <property name="defaulttargetdatasource" ref="datasource" />
    <property name="targetdatasources">
      <map key-type="java.lang.string">
        <entry key="datasource" value-ref="datasource"/>
        <entry key="datasource1" value-ref="datasource1"/>
      </map>
    </property>
  </bean>
  <!-- 配置 sqlsessionfactory -->
  <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
    <!-- 數據庫連接池 -->
    <property name="datasource" ref="multipledatasource"/>
    <property name="configlocation" value="classpath:mybatis.cfg.xml"/>
    <!-- 加載mybatis全局配置文件 -->
    <property name="mapperlocations" value="classpath:com/ys/mapper/*.xml"/>
  </bean>
  <!-- 掃描的dao包(映射文件) 本身應該在mybatis里 現在交給spring -->
  <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
    <property name="basepackage" value="com.ys.dao"/>
  </bean>
<!-- 事務配置 --> 
  <!-- 配置事務管理 -->
  <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
    <property name="datasource" ref="multipledatasource"></property>
  </bean>
  <!-- 開啟注解事務 引用transactionmanager -->
  <tx:annotation-driven transaction-manager="transactionmanager"/>
</beans>

創建multipledatasource.java

?
1
2
3
4
5
6
7
8
package com.ys.dbconfig;
import org.springframework.jdbc.datasource.lookup.abstractroutingdatasource;
public class multipledatasource extends abstractroutingdatasource{
  @override
  protected object determinecurrentlookupkey() {
    return multipledatasourcehandler.getroutekey();
  }
}

創建multipledatasourcehandler.java

?
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
package com.ys.dbconfig;
/**
*@ title multipledatasourcehandler.java
*@ description: 多數據源handler
*@ time 創建時間:2018年8月25日 上午10:52:12
**/
public class multipledatasourcehandler {
  private static threadlocal<string> routekey = new threadlocal<string>();
  /**
   * @title: getroutekey
   * @description: 獲取當前線程的數據源路由的key
   * @param @return
   * @return string
   * @date createtime:2018年8月27日上午10:34:52
   */
  public static string getroutekey(){
    return routekey.get();
  }
  /**
   * @title: setroutekey
   * @description: 綁定當前線程數據源路由的key 使用完成后必須調用removeroutekey()方法刪除
   * @param @param key
   * @return void
   * @date createtime:2018年8月27日上午10:35:03
   */
  public static void setroutekey(string key){
    routekey.set(key);
  }
  /**
   * @title: removeroutekey
   * @description: 刪除與當前線程綁定的數據源路由的key
   * @return void
   * @date createtime:2018年8月27日上午10:35:31
   */
  public static void removeroutekey(){
    routekey.remove();
  }
}

切換數據源

?
1
multipledatasourcehandler.setroutekey(“datasource1”);

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/qq_36476972/article/details/82108755

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品免费国产一区二区三区 | 色婷婷久久综合中文久久一本 | 5g影院天天爽爽 | 欧美kkk4444在线观看 | 91久久福利国产成人精品 | 白丝校花掀起短裙呻吟小说 | 包射屋 | 男人曰女人 | 国产精品久久国产精品99 | 女人麻豆国产香蕉久久精品 | 日韩国产成人精品视频 | aⅴ免费视频| 乳女教师欲乱动漫无修版动画3d | 嫩草香味| 国产一区二区在线观看视频 | 色噜噜视频影院 | 99热在这里只有精品 | 亚洲欧美日韩另类在线 | 亚洲国产精品一区二区首页 | 九九精品视频在线观看 | 亚州春色 | 免费视频片在线观看大片 | 思思91精品国产综合在线 | 被教官揉了一晚上的奶小说 | 秋霞午夜视频在线观看 | 鸥美三级 | 国色天香 社区视频 | 69pao强力打造免费高速 | 美女叽叽 | 好大好粗好舒服 | 国产日韩在线 | 国产成人啪精品午夜在线观看 | 调教小龙女 | 高肉h护士办公室play | 日韩黄色录像 | 日韩成人影视 | av中文字幕网免费观看 | 日韩欧美一区二区三区视频 | 俺去也亚洲色图 | 无码AV精品一区二区三区 | 亚洲精品国产一区二区在线 |