SpringMVC配置双数据源,一个java项目同时连接两个数据库

Posted 红尘太年少

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC配置双数据源,一个java项目同时连接两个数据库相关的知识,希望对你有一定的参考价值。

数据源在配置文件中的配置

请点击---》   java架构师项目实战,高并发集群分布式,大数据高可用,视频教程  

 

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


请点击---》   java架构师项目实战,高并发集群分布式,大数据高可用,视频教程  


DynamicDataSource.class

 

 

[java] view plain copy
 
  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设置数据源的类

 

 

[java] view plain copy
 
  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>//设置要使用的数据源  
  7.     public static void setCustomerType(String customerType) {  
  8.         contextHolder.set(customerType);  
  9.     }  
  10. <span style="white-space:pre">    </span>//获取数据源  
  11.     public static String getCustomerType() {  
  12.         return contextHolder.get();  
  13.     }  
  14. <span style="white-space:pre">    </span>//清除数据源,使用默认的数据源  
  15.     public static void clearCustomerType() {  
  16.         contextHolder.remove();  
  17.     }  
  18. }  

DataSourceInterceptor.class

[java] view plain copy
 
  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>//数据源1  
  9.     public static final String SOURCE_PLAN = "dataSourceOne;  
  10.     //数据源2  
  11.         public static final String SOURCE_FUND = dataSourceTwo;  

}

请点击---》   java架构师项目实战,高并发集群分布式,大数据高可用,视频教程  


springMVC数据源

 

[html] view plain copy
 
  1. jdbc_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver  
  2. dataSourceOne=jdbc:sqlserver://115.29.***.**;DatabaseName=DB_GuiHua  

jdbc_username=**jdbc_password=**

[java] view plain copy
 
  1. dataSourceTwo=jdbc:sqlserver://115.29.***.*;DatabaseName=DB_Fund  

 


Spring MVC会默认有一个数据源,当需要更换数据源时,要在调用事务之前配置

 

 

[java] view plain copy
 
  1. DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);//更换数据源  
[java] view plain copy
 
  1. /** 
  2.  * @ClassName: DataSourceContextHolder 
  3.  * @Description: 数据库切换工具类 
  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. }  

 

请点击---》   java架构师项目实战,高并发集群分布式,大数据高可用,视频教程  

以上是关于SpringMVC配置双数据源,一个java项目同时连接两个数据库的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC_HelloWorld_02

Spring Boot 双数据源Mybatis+MongoDB配置

使用IDEA结合MAVEN创建一个基于SpringMVC为前端JdbcTemplate为数据库组件的Java Web项目

Maven 组装 WAR 时出错:使用纯基于 Java 的配置构建 SpringMVC 项目且没有 xml 时需要 webxml 属性

SpringBoot Mybatis双数据源的配置

springMvc+Mybatis多数据源配置报错 Invalid bound statement