Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法

 更新时间:2017年05月21日 08:48:31   作者:荒野的尘埃  
这篇文章主要给大家介绍了关于Spring MVC如何配置双数据源实现一个java项目同时连接两个数据库的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

前言

本文主要介绍的是关于Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法,分享出来供大家参考学习,下面来看看详细的介绍:

实现方法:

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

<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设置数据源的类

package com.core; 
 
public class DatabaseContextHolder { 
 
 private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); 
<span style="white-space:pre"> </span>//设置要使用的数据源 
 public static void setCustomerType(String customerType) { 
  contextHolder.set(customerType); 
 } 
<span style="white-space:pre"> </span>//获取数据源 
 public static String getCustomerType() { 
  return contextHolder.get(); 
 } 
<span style="white-space:pre"> </span>//清除数据源,使用默认的数据源 
 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>//数据源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> 
 //数据源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数据源

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会默认有一个数据源,当需要更换数据源时,要在调用事务之前配置

DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);//更换数据源 
/** 
 * @ClassName: DataSourceContextHolder 
 * @Description: 数据库切换工具类 
 * @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(); 
 } 
} 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对程序员之家的支持。

相关文章

  • IDEA连接mysql数据库报错的解决方法

    IDEA连接mysql数据库报错的解决方法

    这篇文章主要介绍了IDEA连接mysql数据库报错的解决方法,文中有非常详细的图文示例,对出现Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope报错的小伙伴们很有帮助哟,需要的朋友可以参考下
    2021-05-05
  • Java文件(io)编程之文件字符流使用方法详解

    Java文件(io)编程之文件字符流使用方法详解

    这篇文章主要为大家详细介绍了Java文件(io)编程之文件字符流使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • java接口性能优化技巧

    java接口性能优化技巧

    这篇文章主要为大家介绍了java接口性能优化技巧示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Java?Spring的两种事务你知道吗

    Java?Spring的两种事务你知道吗

    这篇文章主要为大家详细介绍了Java?Spring的两种事务,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Intellij IDEA解析jacoco结果文件的方法

    Intellij IDEA解析jacoco结果文件的方法

    这篇文章主要介绍了Intellij IDEA解析jacoco结果文件的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • SpringBoot图文并茂讲解登录拦截器

    SpringBoot图文并茂讲解登录拦截器

    其实spring boot拦截器的配置方式和springMVC差不多,只有一些小的改变需要注意下就ok了,下面这篇文章主要给大家介绍了关于如何在Springboot实现登陆拦截器功能的相关资料,需要的朋友可以参考下
    2022-06-06
  • SpringBoot服务访问路径动态处理方式

    SpringBoot服务访问路径动态处理方式

    这篇文章主要介绍了SpringBoot服务访问路径动态处理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • springmvc字符编码过滤器CharacterEncodingFilter的使用

    springmvc字符编码过滤器CharacterEncodingFilter的使用

    这篇文章主要介绍了springmvc字符编码过滤器CharacterEncodingFilter的使用,具有很好的参考价值,希望对大家有所帮助。
    2021-08-08
  • Spring Cloud Stream异常处理过程解析

    Spring Cloud Stream异常处理过程解析

    这篇文章主要介绍了Spring Cloud Stream异常处理过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Java File类常用方法与文件过滤器详解

    Java File类常用方法与文件过滤器详解

    Java File类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。File对象代表磁盘中实际存在的文件和目录。本篇文章我们来讲解File类的常用方法与文件过滤器
    2022-04-04

最新评论

?


http://www.vxiaotou.com