Spring MVC配置雙數(shù)據(jù)源實(shí)現(xiàn)一個(gè)java項(xiàng)目同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)的方法
前言
本文主要介紹的是關(guān)于Spring MVC配置雙數(shù)據(jù)源實(shí)現(xiàn)一個(gè)java項(xiàng)目同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)的方法,分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)看看詳細(xì)的介紹:
實(shí)現(xiàn)方法:
數(shù)據(jù)源在配置文件中的配置
<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設(shè)置數(shù)據(jù)源的類(lèi)
package com.core; public class DatabaseContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); <span style="white-space:pre"> </span>//設(shè)置要使用的數(shù)據(jù)源 public static void setCustomerType(String customerType) { contextHolder.set(customerType); } <span style="white-space:pre"> </span>//獲取數(shù)據(jù)源 public static String getCustomerType() { return contextHolder.get(); } <span style="white-space:pre"> </span>//清除數(shù)據(jù)源,使用默認(rèn)的數(shù)據(jù)源 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>//數(shù)據(jù)源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> //數(shù)據(jù)源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數(shù)據(jù)源
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會(huì)默認(rèn)有一個(gè)數(shù)據(jù)源,當(dāng)需要更換數(shù)據(jù)源時(shí),要在調(diào)用事務(wù)之前配置
DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);//更換數(shù)據(jù)源
/** * @ClassName: DataSourceContextHolder * @Description: 數(shù)據(jù)庫(kù)切換工具類(lèi) * @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(); } }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Java Mybatis數(shù)據(jù)源之工廠模式
- Java Spring詳解如何配置數(shù)據(jù)源注解開(kāi)發(fā)以及整合Junit
- 一小時(shí)迅速入門(mén)Mybatis之bind與多數(shù)據(jù)源支持 Java API
- 如何在Java SpringBoot項(xiàng)目中配置動(dòng)態(tài)數(shù)據(jù)源你知道嗎
- Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù)
- Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)
- Java注解實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)例代碼
- java 與testng利用XML做數(shù)據(jù)源的數(shù)據(jù)驅(qū)動(dòng)示例詳解
- Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用
相關(guān)文章
macOS中搭建Java8開(kāi)發(fā)環(huán)境(基于Intel?x86?64-bit)
這篇文章主要介紹了macOS中搭建Java8開(kāi)發(fā)環(huán)境(基于Intel?x86?64-bit)?的相關(guān)資料,需要的朋友可以參考下2022-12-12Java Synchronized鎖升級(jí)原理及過(guò)程剖析
這篇文章主要為大家詳細(xì)介紹一下Java中Synchronized鎖升級(jí)原理及過(guò)程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2022-08-08Java 中POI 導(dǎo)入EXCEL2003 和EXCEL2007的實(shí)現(xiàn)方法
這篇文章主要介紹了Java 中POI 導(dǎo)入EXCEL2003 和EXCEL2007的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文大家能掌握理解這種方法,需要的朋友可以參考下2017-09-09Java使用PDFBox實(shí)現(xiàn)操作PDF文檔
這篇文章主要為大家詳細(xì)介紹了Java如何使用PDFBox實(shí)現(xiàn)操作PDF文檔,例如添加本地圖片、添加網(wǎng)絡(luò)圖片、圖片寬高自適應(yīng)、圖片水平垂直居中對(duì)齊等功能,需要的可以了解下2024-03-03解決springboot+shiro+thymeleaf頁(yè)面級(jí)元素的權(quán)限控制問(wèn)題
這篇文章主要介紹了解決springboot+shiro+thymeleaf頁(yè)面級(jí)元素的權(quán)限控制問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java常見(jiàn)問(wèn)題之javac Hello.java找不到文件的解決方法
剛開(kāi)始編寫(xiě)java代碼時(shí),肯定會(huì)遇到各種各樣的bug,當(dāng)然對(duì)于初學(xué)者這也是能理解的,下面這篇文章主要給大家介紹了關(guān)于Java常見(jiàn)問(wèn)題之javac Hello.java找不到文件解決的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01Spring Boot中使用Redis做緩存的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Redis做緩存的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06