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

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

SpringMVC配置雙數據源

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

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(); }}

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 千阳县| 昌图县| 宣化县| 虹口区| 廊坊市| 抚顺市| 锦屏县| 新龙县| 年辖:市辖区| 清徐县| 永胜县| 天祝| 西林县| 获嘉县| 浦北县| 湄潭县| 连江县| 保山市| 乌兰县| 阳信县| 海南省| 肥城市| 万安县| 凌源市| 禄丰县| 潍坊市| 静海县| 德格县| 高平市| 河南省| 沙雅县| 新乡县| 中宁县| 徐汇区| 光山县| 桂东县| 五常市| 嘉义县| 宝兴县| 射阳县| 镇安县|