欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-17 14:25:46
前言 本文主要介绍的是关于spring mvc配置双数据源实现一个java项目同时连接两个数据库的方法,分享出来供大家参考学习,下面来看看详细的介绍: 实现方法: 数...

前言

本文主要介绍的是关于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(); 
 } 
} 

总结

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