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

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

SpringMVC配置雙數據源

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

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

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 调兵山市| 河津市| 广汉市| 长宁县| 应用必备| 房产| 泌阳县| 玉环县| 八宿县| 文山县| 右玉县| 蛟河市| 苗栗县| 台中县| 罗田县| 灵宝市| 全椒县| 澎湖县| 绿春县| 维西| 枞阳县| 托里县| 蒙山县| 望都县| 建宁县| 肇庆市| 甘谷县| 平顺县| 琼海市| 清新县| 涟水县| 辽宁省| 江北区| 秦安县| 班玛县| 常德市| 特克斯县| 邻水| 嘉定区| 桂阳县| 武定县|