SpringMVC配置双数据源实现⼀个java项⽬同时连接两个数据库的⽅法前⾔
本⽂主要介绍的是关于Spring MVC配置双数据源实现⼀个java项⽬同时连接两个数据库的⽅法,分享出来供⼤家参考学习,下⾯来看看详细的介绍:
实现⽅法:
数据源在配置⽂件中的配置
<pre name="code" class="java"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:aop="/schema/aop"
xmlns:cache="/schema/cache"
xmlns:context="/schema/context"
xmlns:jdbc="/schema/jdbc" xmlns:jee="/s
chema/jee"
xmlns:jms="/schema/jms" xmlns:lang="/schema/lang"
xmlns:mvc="/schema/mvc" xmlns:oxm="/schema/oxm"
xmlns:p="/schema/p" xmlns:task="/schema/task"
xmlns:tx="/schema/tx" xmlns:util="/schema/util"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd
/schema/aop /schema/aop/spring-aop-3.1.xsd
/schema/cache /schema/cache/spring-cache-3.1.xsd
/schema/context /schema/context/spring-context-3.1.xsd
/schema/jdbc /schema/jdbc/spring-jdbc-3.1.xsd
/schema/jee /schema/jee/spring-jee-3.1.xsd
/schema/jms /schema/jms/spring-jms-3.1.xsd
/schema/lang /schema/lang/spring-lang-3.1.xsd
/schema/mvc /schema/mvc/spring-mvc-3.1.xsd
java连接sqlserver数据库/schema/oxm /schema/oxm/spring-oxm-3.1.xsd
/schema/task /schema/task/spring-task-3.1.xsd
/schema/tx /schema/tx/spring-tx-3.1.xsd
/schema/util /schema/util/spring-util-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com"></context:component-scan>
<bean class="org.springframework.fig.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:com/resource/config.properties</value>
</list>
</property>
</bean>
<bean id="dataSourceOne" class="hange.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="hange.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="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="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">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="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(* *.*(..))" />
<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
;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
CustomerType();
}
}
DatabaseContextHolder.class设置数据源的类
;
public class DatabaseContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
<span > </span>//设置要使⽤的数据源
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
<span > </span>//获取数据源
public static String getCustomerType() {
();
}
<span > </span>//清除数据源,使⽤默认的数据源
public static void clearCustomerType() {
}
}
DataSourceInterceptor.class
;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;
@Component
public class DataSourceInterceptor {
<span > </span>//数据源1
public static final String SOURCE_PLAN = "<span >dataSourceOne</span><span >";</span>
//数据源2
<pre name="code" class="java"><span > </span>public static final String SOURCE_FUND = "<span >dataSourceTwo</span>"; }
springMVC数据源
jdbc_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
<pre name="code" class="java">dataSourceOne<span >=jdbc:sqlserver://115.29.***.**;DatabaseName=DB_GuiHua</span>
jdbc_username=**jdbc_password=**
dataSourceTwo<span >=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) ());
}
public static void clearDbType() {
}
}
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。