最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring MVC配置雙數(shù)據(jù)源實現(xiàn)一個java項目同時連接兩個數(shù)據(jù)庫的方法

 更新時間:2017年05月21日 08:48:31   作者:荒野的塵埃  
這篇文章主要給大家介紹了關(guān)于Spring MVC如何配置雙數(shù)據(jù)源實現(xiàn)一個java項目同時連接兩個數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

本文主要介紹的是關(guān)于Spring MVC配置雙數(shù)據(jù)源實現(xiàn)一個java項目同時連接兩個數(shù)據(jù)庫的方法,分享出來供大家參考學習,下面來看看詳細的介紹:

實現(xiàn)方法:

數(shù)據(jù)源在配置文件中的配置

<pre name="code" class="java"><?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:aop="http://www.springframework.org/schema/aop" 
 xmlns:cache="http://www.springframework.org/schema/cache" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 
 xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" 
 xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" 
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd 
 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd 
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
 http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd 
 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd 
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> 
 
 <context:annotation-config /> 
 
 <context:component-scan base-package="com"></context:component-scan> 
 
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  <property name="locations"> 
   <list> 
    <value>classpath:com/resource/config.properties</value> 
   </list> 
  </property> 
 </bean> 
 
 <bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
  destroy-method="close"> 
  <property name="driverClass" value="${dbOne.jdbc.driverClass}" /> 
  <property name="jdbcUrl" value="${dbOne.jdbc.url}" /> 
  <property name="user" value="${dbOne.jdbc.user}" /> 
  <property name="password" value="${dbOne.jdbc.password}" /> 
  <property name="initialPoolSize" value="${dbOne.jdbc.initialPoolSize}" /> 
  <property name="minPoolSize" value="${dbOne.jdbc.minPoolSize}" /> 
  <property name="maxPoolSize" value="${dbOne.jdbc.maxPoolSize}" /> 
 </bean> 
 
 <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
  destroy-method="close"> 
  <property name="driverClass" value="${dbTwo.jdbc.driverClass}" /> 
  <property name="jdbcUrl" value="${dbTwo.jdbc.url}" /> 
  <property name="user" value="${dbTwo.jdbc.user}" /> 
  <property name="password" value="${dbTwo.jdbc.password}" /> 
  <property name="initialPoolSize" value="${dbTwo.jdbc.initialPoolSize}" /> 
  <property name="minPoolSize" value="${dbTwo.jdbc.minPoolSize}" /> 
  <property name="maxPoolSize" value="${dbTwo.jdbc.maxPoolSize}" /> 
 </bean> 
 
 <bean id="dynamicDataSource" class="com.core.DynamicDataSource"> 
  <property name="targetDataSources"> 
   <map key-type="java.lang.String"> 
    <entry value-ref="dataSourceOne" key="dataSourceOne"></entry> 
    <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry> 
   </map> 
  </property> 
  <property name="defaultTargetDataSource" ref="dataSourceOne"> 
  </property> 
 </bean> 
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
  <property name="dataSource" ref="dynamicDataSource" /> 
  <property name="hibernateProperties"> 
   <props> 
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> 
    <prop key="hibernate.show_sql">false</prop> 
    <prop key="hibernate.format_sql">true</prop> 
    <prop key="hbm2ddl.auto">create</prop> 
   </props> 
  </property> 
  <property name="packagesToScan"> 
   <list> 
    <value>com.po</value> 
   </list> 
  </property> 
 </bean> 
 
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
  <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 
 <aop:config> 
  <aop:pointcut id="transactionPointCut" expression="execution(* com.dao..*.*(..))" /> 
  <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointCut" /> 
 </aop:config> 
 
 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
  <tx:attributes> 
   <tx:method name="add*" propagation="REQUIRED" /> 
   <tx:method name="save*" propagation="REQUIRED" /> 
   <tx:method name="update*" propagation="REQUIRED" /> 
   <tx:method name="delete*" propagation="REQUIRED" /> 
   <tx:method name="*" read-only="true" /> 
  </tx:attributes> 
 </tx:advice> 
 
 <aop:config> 
  <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor"> 
   <aop:pointcut id="daoOne" expression="execution(* com.dao.one.*.*(..))" /> 
   <aop:pointcut id="daoTwo" expression="execution(* com.dao.two.*.*(..))" /> 
   <aop:before pointcut-ref="daoOne" method="setdataSourceOne" /> 
   <aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" /> 
  </aop:aspect> 
 </aop:config> 
</beans> 

DynamicDataSource.class

package com.core; 
 
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 
 
public class DynamicDataSource extends AbstractRoutingDataSource{ 
 
 @Override 
 protected Object determineCurrentLookupKey() { 
  return DatabaseContextHolder.getCustomerType(); 
 } 
 
} 

DatabaseContextHolder.class設(shè)置數(shù)據(jù)源的類

package com.core; 
 
public class DatabaseContextHolder { 
 
 private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); 
<span style="white-space:pre"> </span>//設(shè)置要使用的數(shù)據(jù)源 
 public static void setCustomerType(String customerType) { 
  contextHolder.set(customerType); 
 } 
<span style="white-space:pre"> </span>//獲取數(shù)據(jù)源 
 public static String getCustomerType() { 
  return contextHolder.get(); 
 } 
<span style="white-space:pre"> </span>//清除數(shù)據(jù)源,使用默認的數(shù)據(jù)源 
 public static void clearCustomerType() { 
  contextHolder.remove(); 
 } 
} 

DataSourceInterceptor.class

package com.core; 
 
import org.aspectj.lang.JoinPoint; 
import org.springframework.stereotype.Component; 
 
@Component 
public class DataSourceInterceptor { 
<span style="white-space:pre"> </span>//數(shù)據(jù)源1 
 public static final String SOURCE_PLAN = "<span style="font-family: Arial, Helvetica, sans-serif;">dataSourceOne</span><span style="font-family: Arial, Helvetica, sans-serif;">";</span> 
 //數(shù)據(jù)源2 
<pre name="code" class="java"><span style="white-space:pre"> </span>public static final String SOURCE_FUND = "<span style="font-family: Arial, Helvetica, sans-serif;">dataSourceTwo</span>"; 
}

springMVC數(shù)據(jù)源

jdbc_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver 
<pre name="code" class="java">dataSourceOne<span style="font-family: Arial, Helvetica, sans-serif;">=jdbc:sqlserver://115.29.***.**;DatabaseName=DB_GuiHua</span> 

jdbc_username=**jdbc_password=**

dataSourceTwo<span style="font-family: Arial, Helvetica, sans-serif;">=jdbc:sqlserver://115.29.***.*;DatabaseName=DB_Fund</span> 

Spring MVC會默認有一個數(shù)據(jù)源,當需要更換數(shù)據(jù)源時,要在調(diào)用事務(wù)之前配置

DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);//更換數(shù)據(jù)源 
/** 
 * @ClassName: DataSourceContextHolder 
 * @Description: 數(shù)據(jù)庫切換工具類 
 * @author: wzx 
 * @date: 2016-07-27 上午10:26:01 
 */ 
public class DataSourceContextHolder { 
 private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); 
 
 public static void setDbType(String dbType) { 
  contextHolder.set(dbType); 
 } 
 
 public static String getDbType() { 
  return ((String) contextHolder.get()); 
 } 
 
 public static void clearDbType() { 
  contextHolder.remove(); 
 } 
} 

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn)

    SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn)

    這篇文章主要介紹了SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • JavaEE中關(guān)于ServletConfig的小結(jié)

    JavaEE中關(guān)于ServletConfig的小結(jié)

    ServletConfig是針對特定的Servlet的參數(shù)或?qū)傩?。ServletConfig是表示單獨的Servlet的配置和參數(shù),只是適用于特定的Servlet。從一個servlet被實例化后,對任何客戶端在任何時候訪問有效,但僅對本servlet有效,一個servlet的ServletConfig對象不能被另一個servlet訪問
    2014-10-10
  • 基于java TCP網(wǎng)絡(luò)通信的實例詳解

    基于java TCP網(wǎng)絡(luò)通信的實例詳解

    本篇文章是對java中TCP網(wǎng)絡(luò)通信的實例進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Java正則表達式之全量匹配和部分匹配

    Java正則表達式之全量匹配和部分匹配

    正則表達式異常強大,一直理解不深,用的也不深,這次項目中嘗試,體會到了它的強大之處,這篇文章主要給大家介紹了關(guān)于Java正則表達式之全量匹配和部分匹配的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Java警告:原發(fā)性版11需要目標發(fā)行版11的解決方法和步驟

    Java警告:原發(fā)性版11需要目標發(fā)行版11的解決方法和步驟

    這篇文章主要介紹了Java警告:原發(fā)性版11需要目標發(fā)行版11的解決方法和步驟,文中通過圖文介紹的非常詳細,對大家學習或者使用java具有一定的參考借鑒價值,需要的朋友可以參考下
    2025-04-04
  • Spring?Boot虛擬線程Webflux在JWT驗證和MySQL查詢性能比較

    Spring?Boot虛擬線程Webflux在JWT驗證和MySQL查詢性能比較

    這篇文章主要為大家介紹了Spring Boot虛擬線程與Webflux在JWT驗證和MySQL查詢上的性能比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • java Gui實現(xiàn)肯德基點餐收銀系統(tǒng)

    java Gui實現(xiàn)肯德基點餐收銀系統(tǒng)

    這篇文章主要為大家詳細介紹了java Gui實現(xiàn)肯德基點餐收銀系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • hibernate屬性級別注解實例代碼

    hibernate屬性級別注解實例代碼

    這篇文章主要介紹了hibernate屬性級別注解實例代碼,涉及屬性級別注釋添加方式及種類,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • MyBatis的SUM映射問題及解決

    MyBatis的SUM映射問題及解決

    這篇文章主要介紹了MyBatis的SUM映射問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 教你用JAVA寫文本編輯器(三)

    教你用JAVA寫文本編輯器(三)

    這篇文章主要給大家介紹了關(guān)于用JAVA寫文本編輯器的相關(guān)資料,本文主要實現(xiàn)的是一個點擊選擇文本格式的窗口,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-11-11

最新評論

四子王旗| 清流县| 清水河县| 青海省| 舒兰市| 昌黎县| 黑水县| 观塘区| 凯里市| 鸡泽县| 分宜县| 津市市| 苍梧县| 巴楚县| 沛县| 辽源市| 静乐县| 罗平县| 海原县| 隆子县| 祁门县| 大渡口区| 来安县| 溧阳市| 九江县| 冷水江市| 临湘市| 武功县| 长宁县| 嘉禾县| 四川省| 林州市| 龙海市| 策勒县| 定陶县| 文山县| 凌源市| 涞源县| 犍为县| 隆化县| 平武县|