国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SpringMVC配置雙數據源

2019-11-09 19:22:58
字體:
來源:轉載
供稿:網友

1.在xml中配置對應的bean

<!--數據源dataSourceLocal--><bean id="dataSourceLocal" class="org.apache.commons.dbcp.BasicDataSource"> <PRoperty name="driverClassName" value="${jdbc.className}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="passWord" value="${jdbc.password}" /></bean><!--數據源dataSource137--><bean id="dataSource137" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.localDataBase.className}" /> <property name="url" value="${jdbc.localDataBase.url}" /> <property name="username" value="${jdbc.localDataBase.username}" /> <property name="password" value="${jdbc.localDataBase.password}" /></bean><!--將數據源引入的dataSource--><bean id="dataSource" class="com.simple.common.dataconfig.DynamicDataSource"> <!--將兩個數據源引入到Map<dataSourceName,dataSource>--> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="dataSourceLocal" key="dataSourceLocal"></entry> <entry value-ref="dataSource137" key="dataSource137"></entry> </map> </property> <!--指定默認使用的數據源--> <property name="defaultTargetDataSource" ref="dataSourceLocal"> </property></bean>

2.自己實現DynamicDataSource這個bean

package com.simple.common.dataconfig;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;//需要繼承AbstractRoutingDataSource,通過determineCurrentLookupKey方法設置當前數據源public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDBType(); }}

3.封裝數據源

package com.simple.common.dataconfig;public class DataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); //設置使用的數據源,dbType為bean的名字(xml中配置了dataSourceLocal,dataSource137) public static void setDBType(String dbType) { contextHolder.set(dbType); } //獲取當前使用的數據源 public static String getDBType() { return ((String) contextHolder.get()); } //使用默認數據源 public static void clearDBType() { contextHolder.remove(); } }

4.實際調用用的類

package com.simple.common.dataconfig;//設置使用的數據源,獲取使用的數據源public class DataSourceGetSet { // Oracle數據庫 private static final String WEBADMORACLE = "dataSource137"; // 本地數據源--MySQL數據庫 private static final String LOCALMYSQL = "dataSourceLocal"; /** * 使用137環境數據源 * @return 配置成功返回true */ public static boolean SetDataSourceTO137() { DataSourceContextHolder.setDBType(WEBADMORACLE); String DBType = DataSourceContextHolder.getDBType(); if (WEBADMORACLE.equals(DBType)) { return true; } return false; } /** * 使用本地環境數據源 * @return 配置成功返回true */ public static boolean SetDataSourceTOLocal() { DataSourceContextHolder.setDBType(LOCALMYSQL); String DBType = DataSourceContextHolder.getDBType(); if (LOCALMYSQL.equals(DBType)) { return true; } return false; } /** * 獲取當前數據源名稱 */ public static String GetDBType(){ return DataSourceContextHolder.getDBType(); }}

如果需要配置更多數據源,同理可知!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平阴县| 湖南省| 慈溪市| 潜山县| 甘洛县| 玛纳斯县| 浪卡子县| 江安县| 商城县| 宜川县| 长岭县| 苍山县| 化州市| 崇礼县| 鲁山县| 丹凤县| 吉木乃县| 大悟县| 军事| 临武县| 灯塔市| 洪泽县| 长寿区| 固安县| 金溪县| 尖扎县| 壶关县| 丽水市| 湖北省| 建德市| 襄城县| 英超| 县级市| 昔阳县| 新竹市| 永昌县| 西昌市| 安达市| 桃江县| 榕江县| 麦盖提县|