亚洲欧美日韩综合系列在线_91精品人妻一区二区_欧美大肥婆一级特大AA片_九色91视频免费观看_亚洲综合国产精品_av中文字幕在线不卡_久久精品色综合网_看黄色视频的软件_无卡无码高清中文字幕码2024_亚洲欧美日韩天堂网

SpringMVC配置雙數(shù)據(jù)源,一個(gè)java項(xiàng)目同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)

來源:白開水挺好喝 發(fā)布時(shí)間:2018-11-14 11:31:26 閱讀量:1034

[java] view plain copy

 在CODE上查看代碼片派生到我的代碼片


  1. <pre name=“code” class=“java”><?xml version=“1.0” encoding=“UTF-8”?>  

  2. <beans xmlns=”http://www.springframework.org/schema/beans”  

  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:aop=“http://www.springframework.org/schema/aop”  

  4.     xmlns:cache=”http://www.springframework.org/schema/cache”  

  5.     xmlns:context=”http://www.springframework.org/schema/context”  

  6.     xmlns:jdbc=”http://www.springframework.org/schema/jdbc” xmlns:jee=“http://www.springframework.org/schema/jee”  

  7.     xmlns:jms=”http://www.springframework.org/schema/jms” xmlns:lang=“http://www.springframework.org/schema/lang”  

  8.     xmlns:mvc=”http://www.springframework.org/schema/mvc” xmlns:oxm=“http://www.springframework.org/schema/oxm”  

  9.     xmlns:p=”http://www.springframework.org/schema/p” xmlns:task=“http://www.springframework.org/schema/task”  

  10.     xmlns:tx=”http://www.springframework.org/schema/tx” xmlns:util=“http://www.springframework.org/schema/util”  

  11.     xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    

  12.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    

  13.     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    

  14.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd    

  15.     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    

  16.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd    

  17.     http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd    

  18.     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd    

  19.     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd    

  20.     http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd    

  21.     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd    

  22.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    

  23.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd”>  

  24.   

  25.     <context:annotation-config />  

  26.   

  27.     <context:component-scan base-package=“com”></context:component-scan>  

  28.   

  29.     <bean class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>  

  30.         <property name=”locations”>  

  31.             <list>  

  32.                 <value>classpath:com/resource/config.properties</value>  

  33.             </list>  

  34.         </property>  

  35.     </bean>  

  36.   

  37.     <bean id=”dataSourceOne” class=“com.mchange.v2.c3p0.ComboPooledDataSource”  

  38.         destroy-method=”close”>  

  39.         <property name=”driverClass” value=“${dbOne.jdbc.driverClass}” />  

  40.         <property name=”jdbcUrl” value=“${dbOne.jdbc.url}” />  

  41.         <property name=”user” value=“${dbOne.jdbc.user}” />  

  42.         <property name=”password” value=“${dbOne.jdbc.password}” />  

  43.         <property name=”initialPoolSize” value=“${dbOne.jdbc.initialPoolSize}” />  

  44.         <property name=”minPoolSize” value=“${dbOne.jdbc.minPoolSize}” />  

  45.         <property name=”maxPoolSize” value=“${dbOne.jdbc.maxPoolSize}” />  

  46.     </bean>  

  47.   

  48.     <bean id=”dataSourceTwo” class=“com.mchange.v2.c3p0.ComboPooledDataSource”  

  49.         destroy-method=”close”>  

  50.         <property name=”driverClass” value=“${dbTwo.jdbc.driverClass}” />  

  51.         <property name=”jdbcUrl” value=“${dbTwo.jdbc.url}” />  

  52.         <property name=”user” value=“${dbTwo.jdbc.user}” />  

  53.         <property name=”password” value=“${dbTwo.jdbc.password}” />  

  54.         <property name=”initialPoolSize” value=“${dbTwo.jdbc.initialPoolSize}” />  

  55.         <property name=”minPoolSize” value=“${dbTwo.jdbc.minPoolSize}” />  

  56.         <property name=”maxPoolSize” value=“${dbTwo.jdbc.maxPoolSize}” />  

  57.     </bean>  

  58.   

  59.     <bean id=”dynamicDataSource” class=“com.core.DynamicDataSource”>  

  60.         <property name=”targetDataSources”>  

  61.             <map key-type=”java.lang.String”>  

  62.                 <entry value-ref=”dataSourceOne” key=“dataSourceOne”></entry>  

  63.                 <entry value-ref=”dataSourceTwo” key=“dataSourceTwo”></entry>  

  64.             </map>  

  65.         </property>  

  66.         <property name=”defaultTargetDataSource” ref=“dataSourceOne”>  

  67.         </property>  

  68.     </bean>  

  69.   

  70.     <bean id=”sessionFactory” class=“org.springframework.orm.hibernate4.LocalSessionFactoryBean”>  

  71.         <property name=”dataSource” ref=“dynamicDataSource” />  

  72.         <property name=”hibernateProperties”>  

  73.             <props>  

  74.                 <prop key=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</prop>  

  75.                 <prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop>  

  76.                 <prop key=”hibernate.show_sql”>false</prop>  

  77.                 <prop key=”hibernate.format_sql”>true</prop>  

  78.                 <prop key=”hbm2ddl.auto”>create</prop>  

  79.             </props>  

  80.         </property>  

  81.         <property name=”packagesToScan”>  

  82.             <list>  

  83.                 <value>com.po</value>  

  84.             </list>  

  85.         </property>  

  86.     </bean>  

  87.   

  88.     <bean id=”transactionManager” class=“org.springframework.orm.hibernate4.HibernateTransactionManager”>  

  89.         <property name=”sessionFactory” ref=“sessionFactory” />  

  90.     </bean>  

  91.   

  92.     <aop:config>  

  93.         <aop:pointcut id=”transactionPointCut” expression=“execution(* com.dao..*.*(..))” />  

  94.         <aop:advisor advice-ref=”txAdvice” pointcut-ref=“transactionPointCut” />  

  95.     </aop:config>  

  96.   

  97.     <tx:advice id=”txAdvice” transaction-manager=“transactionManager”>  

  98.         <tx:attributes>  

  99.             <tx:method name=”add*” propagation=“REQUIRED” />  

  100.             <tx:method name=”save*” propagation=“REQUIRED” />  

  101.             <tx:method name=”update*” propagation=“REQUIRED” />  

  102.             <tx:method name=”delete*” propagation=“REQUIRED” />  

  103.             <tx:method name=”*” read-only=“true” />  

  104.         </tx:attributes>  

  105.     </tx:advice>  

  106.   

  107.     <aop:config>  

  108.         <aop:aspect id=”dataSourceAspect” ref=“dataSourceInterceptor”>  

  109.             <aop:pointcut id=”daoOne” expression=“execution(* com.dao.one.*.*(..))” />  

  110.             <aop:pointcut id=”daoTwo” expression=“execution(* com.dao.two.*.*(..))” />  

  111.             <aop:before pointcut-ref=”daoOne” method=“setdataSourceOne” />  

  112.             <aop:before pointcut-ref=”daoTwo” method=“setdataSourceTwo” />  

  113.         </aop:aspect>  

  114.     </aop:config>  

  115. </beans>  






DynamicDataSource.class

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. package com.core;  

  2.   

  3. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  

  4.   

  5. public class DynamicDataSource extends AbstractRoutingDataSource{  

  6.   

  7.     @Override  

  8.     protected Object determineCurrentLookupKey() {  

  9.         return DatabaseContextHolder.getCustomerType();   

  10.     }  

  11.   

  12. }  

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

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. package com.core;  

  2.   

  3. public class DatabaseContextHolder {  

  4.   

  5.     private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  

  6. <span style=”white-space:pre”>    </span>//設(shè)置要使用的數(shù)據(jù)源  

  7.     public static void setCustomerType(String customerType) {  

  8.         contextHolder.set(customerType);  

  9.     }  

  10. <span style=”white-space:pre”>    </span>//獲取數(shù)據(jù)源  

  11.     public static String getCustomerType() {  

  12.         return contextHolder.get();  

  13.     }  

  14. <span style=”white-space:pre”>    </span>//清除數(shù)據(jù)源,使用默認(rèn)的數(shù)據(jù)源  

  15.     public static void clearCustomerType() {  

  16.         contextHolder.remove();  

  17.     }  

  18. }  

DataSourceInterceptor.class
[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. package com.core;  

  2.   

  3. import org.aspectj.lang.JoinPoint;  

  4. import org.springframework.stereotype.Component;  

  5.   

  6. @Component  

  7. public class DataSourceInterceptor {  

  8. <span style=”white-space:pre”>    </span>//數(shù)據(jù)源1  

  9.     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>  

  10.     //數(shù)據(jù)源2  

  11. <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ù)源

[html] view plain copy

 在CODE上查看代碼片派生到我的代碼片


  1. jdbc_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver  

  2. <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=[java] view plain copy

 在CODE上查看代碼片派生到我的代碼片


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


Spring MVC會(huì)默認(rèn)有一個(gè)數(shù)據(jù)源,當(dāng)需要更換數(shù)據(jù)源時(shí),要在調(diào)用事務(wù)之前配置

[java] view plain copy

 在CODE上查看代碼片派生到我的代碼片


  1. DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);//更換數(shù)據(jù)源  

[java] view plain copy

 在CODE上查看代碼片派生到我的代碼片


  1. /** 

  2.  * @ClassName: DataSourceContextHolder 

  3.  * @Description: 數(shù)據(jù)庫(kù)切換工具類 

  4.  * @author: wzx 

  5.  * @date: 2016-07-27 上午10:26:01 

  6.  */  

  7. public class DataSourceContextHolder {  

  8.     private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  

  9.   

  10.     public static void setDbType(String dbType) {  

  11.         contextHolder.set(dbType);  

  12.     }  

  13.   

  14.     public static String getDbType() {  

  15.         return ((String) contextHolder.get());  

  16.     }  

  17.   

  18.     public static void clearDbType() {  

  19.         contextHolder.remove();  

  20.     }  

  21. }  



標(biāo)簽: 數(shù)據(jù)庫(kù)
分享:
評(píng)論:
你還沒有登錄,請(qǐng)先