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

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

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

服務(wù)器之家 - 編程語言 - Java教程 - Spring配置多數(shù)據(jù)源切換

Spring配置多數(shù)據(jù)源切換

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

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

多數(shù)據(jù)源切換

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
#定義初始連接數(shù)
initialsize=0
#定義最大連接數(shù)
maxactive=1000
#定義最大空閑
maxidle=20
#定義最小空閑
minidle=1
#定義最長等待時間
maxwait=60000
#mysql
# driverclassname 根據(jù)url自動識別 這一項(xiàng)可配可不配,如果不配置druid會根據(jù)url自動識別dbtype,然后選擇相應(yīng)的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
# 初始化時建立物理連接的個數(shù)。初始化發(fā)生在顯示調(diào)用init方法,或者第一次getconnection時
initialsize1=0
# 最大連接池?cái)?shù)量
maxactive1=1000
#定義最小空閑
minidle1=1
# 獲取連接時最大等待時間,單位毫秒。配置了maxwait之后,
# 缺省啟用公平鎖,并發(fā)效率會有所下降,
# 如果需要可以通過配置useunfairlock屬性為true使用非公平鎖。
maxwait1=60000
# druid 監(jiān)控
# 屬性類型是字符串,通過別名的方式配置擴(kuò)展插件,
# 常用的插件有:
# 監(jiān)控統(tǒng)計(jì)用的filter:stat
# 日志用的filter:log4j
# 防御sql注入的filter:wall
filters1=stat,log4j
# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
timebetweenevictionrunsmillis1=60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minevictableidletimemillis1=300000
# 建議配置為true,不影響性能,并且保證安全性。
# 申請連接的時候檢測,如果空閑時間大于
# timebetweenevictionrunsmillis,
# 執(zhí)行validationquery檢測連接是否有效。
testwhileidle1=true
# 申請連接時執(zhí)行validationquery檢測連接是否有效,做了這個配置會降低性能。
testonborrow1=false
# 歸還連接時執(zhí)行validationquery檢測連接是否有效,做了這個配置會降低性能
testonreturn1=false
# 是否緩存preparedstatement,也就是pscache。
# pscache對支持游標(biāo)的數(shù)據(jù)庫性能提升巨大,比如說oracle。
# 在mysql5.5以下的版本中沒有pscache功能,建議關(guān)閉掉。
# 作者在5.5版本中使用pscache,通過監(jiān)控界面發(fā)現(xiàn)pscache有緩存命中率記錄,
# 該應(yīng)該是支持pscache。
poolpreparedstatements1=false
# 要啟用pscache,必須配置大于0,當(dāng)大于0時,
# poolpreparedstatements自動觸發(fā)修改為true。
# 在druid中,不會存在oracle下pscache占用內(nèi)存過多的問題,
# 可以把這個數(shù)值配置大一些,比如說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里邊已經(jīng)掃描一次了
   這時就要加上屬性 use-default-filters="true"  這個屬性就是使用默認(rèn)的掃描
   默認(rèn)就掃描com.ys下所有 設(shè)置為false 在下邊配置需要掃描的部分-->
   <!-- 現(xiàn)在的配置就只會掃描帶@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>
  <!-- 配置連接池?cái)?shù)據(jù)源  文檔搜索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>
    <!-- 連接池最大數(shù)量 -->
    <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}" />
    <!-- 最大連接池?cái)?shù)量 -->
    <property name="maxactive" value="${maxactive1}" />
    <!-- 獲取連接最大等待時間 -->
    <property name="maxwait" value="${maxwait1}"/>
    <!-- 監(jiān)控統(tǒng)計(jì)用的filter:stat  日志用的filter:log4j 防御sql注入的filter:wall -->
    <property name="filters" value="${filters1}" />
    <!-- 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 -->
    <property name="timebetweenevictionrunsmillis" value="${timebetweenevictionrunsmillis1}" />
    <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
    <property name="minevictableidletimemillis" value="${minevictableidletimemillis1}" />
    <!-- 建議配置為true,不影響性能,并且保證安全性。 申請連接的時候檢測 -->
    <property name="testwhileidle" value="${testwhileidle1}"/>
    <!-- 申請連接時執(zhí)行validationquery檢測連接是否有效,做了這個配置會降低性能。 -->
    <property name="testonborrow" value="${testonborrow1}"/>
    <!-- 歸還連接時執(zhí)行validationquery檢測連接是否有效,做了這個配置會降低性能 -->
    <property name="testonreturn" value="false" />
    <!-- 是否緩存preparedstatement,也就是pscache 適用支持游標(biāo)的數(shù)據(jù)庫 如oracle -->
    <property name="poolpreparedstatements" value="${poolpreparedstatements1}"/>
    <!-- 要啟用pscache,必須配置大于0,當(dāng)大于0時 poolpreparedstatements自動觸發(fā)修改為true。 -->
    <property name="maxopenpreparedstatements" value="${maxopenpreparedstatements1}"/>
    <!-- 定義監(jiān)控日志輸出間隔 -->
    <property name="timebetweenlogstatsmillis" value="60000"/>
    <!--<property name="statlogger" ref ="statloggerb"/> -->
    <!-- 若需要mybatis的批量sql需配置   不配置則報(bào)錯: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>
  <!-- 多數(shù)據(jù)源配置 -->
  <bean id="multipledatasource" class="com.ys.dbconfig.multipledatasource">
    <!-- 默認(rèn)數(shù)據(jù)源 -->
    <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">
    <!-- 數(shù)據(jù)庫連接池 -->
    <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包(映射文件) 本身應(yīng)該在mybatis里 現(xiàn)在交給spring -->
  <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
    <property name="basepackage" value="com.ys.dao"/>
  </bean>
<!-- 事務(wù)配置 --> 
  <!-- 配置事務(wù)管理 -->
  <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
    <property name="datasource" ref="multipledatasource"></property>
  </bean>
  <!-- 開啟注解事務(wù) 引用transactionmanager -->
  <tx:annotation-driven transaction-manager="transactionmanager"/>
</beans>

創(chuàng)建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();
  }
}

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

切換數(shù)據(jù)源

?
1
multipledatasourcehandler.setroutekey(“datasource1”);

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品馆 | 99国产在线视频 | 欧美日韩精品在线视频 | 日韩美一区二区三区 | 久久精麻豆亚洲AV国产品 | 欧美日韩亚洲综合在线一区二区 | 饭冈加奈子黑人解禁在线播放 | 1313午夜精品久久午夜片 | 国产欧美日韩精品一区二区三区 | 国产在线精品99一卡2卡 | 污污的动态图合集 | 精品人伦一区二区三区潘金莲 | heyzo在线播放 | 高跟丝袜人妖sissy露出调教 | chinese男男gay | 欧美一卡2卡三卡4卡5卡免费观看 | 激情影院免费 | julia ann黑人巨大 | 视频在线观看大片 | 男生同性啪视频在线观看 | a看片| 国产在线视频欧美亚综合 | 香蕉精品国产高清自在自线 | 欧美精品国产一区二区 | 国产成人精选免费视频 | 欧美日韩1区2区 | 国产精品久久久精品视频 | 精新精新国产自在现 | 视频免费视频观看网站 | 色女的乖男人 | 亚洲精品久久久打桩机 | 91视频免费观看网站 | 亚洲欧美国产另类视频 | 精油按摩日本 | 俄罗斯性高清完整版 | 国产精品视频网 | 欧美成人禁片在线观看俄罗斯 | 亚洲视频久久 | 亚洲欧美精品天堂久久综合一区 | 国产一二区视频 | 欧美日韩一品道 |