spring基于通用Dao的多數(shù)據(jù)源配置詳解
有時候在一個項目中會連接多個數(shù)據(jù)庫,需要在spring中配置多個數(shù)據(jù)源,最近就遇到了這個問題,由于我的項目之前是基于通用Dao的,配置的時候問題不斷,這種方式和資源文件沖突;掃描映射文件的話,SqlSessionFactory的bean名字必須是sqlSessionFactory 他讀不到sqlSessioNFactory2或者其他名字,最終解決方法如下:
1.在項目中加入如下類MultipleDataSource.java
package com.etoak.util; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class MultipleDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>(); public static void setDataSourceKey(String dataSource) { dataSourceKey.set(dataSource); } @Override protected Object determineCurrentLookupKey() { // TODO Auto-generated method stub return dataSourceKey.get(); } }
spring配置文件如下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com"/> <mvc:annotation-driven/> <context:property-placeholder location="classpath:db.properties"/> <bean id="ds1" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${mysql.driver}" p:url="${mysql.url}" p:username="${mysql.username}" p:password="${mysql.password}"/> <bean id="ds2" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${mysql2.driver}" p:url="${mysql2.url}" p:username="${mysql2.username}" p:password="${mysql2.password}"/> <bean id="multipleDataSource" class="com.etoak.util.MultipleDataSource"> <property name="defaultTargetDataSource" ref="ds1"/> <property name="targetDataSources"> <map> <entry key="ds1" value-ref="ds1"/> <entry key="ds2" value-ref="ds2"/> </map> </property> </bean> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="multipleDataSource" p:mapperLocations="classpath:com/etoak/dao/*-mapper.xml"/> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.etoak.dao"/> <property name="markerInterface" value="com.etoak.dao.BaseDao" /> </bean> </beans>
測試類如下:
package com.etoak.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.etoak.dao.ProductDaoIf; import com.etoak.util.MultipleDataSource; public class Test { public static void main(String[] args) { ApplicationContext ac = new FileSystemXmlApplicationContext("WebContent/WEB-INF/etoak-servlet.xml"); ProductDaoIf proDao = (ProductDaoIf)ac.getBean(ProductDaoIf.class); MultipleDataSource.setDataSourceKey("ds1"); int count1 = proDao.selectProductCount(); MultipleDataSource.setDataSourceKey("ds2"); int count2 = proDao.selectProductCount(); System.out.println(count1); System.out.println(count2); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置
- 深入理解spring多數(shù)據(jù)源配置
- spring+Jpa多數(shù)據(jù)源配置的方法示例
- 詳解基于Spring Boot與Spring Data JPA的多數(shù)據(jù)源配置
- Spring+MyBatis多數(shù)據(jù)源配置實(shí)現(xiàn)示例
- springboot-mongodb的多數(shù)據(jù)源配置的方法步驟
- springboot v2.0.3版本多數(shù)據(jù)源配置方法
- Spring動態(tài)多數(shù)據(jù)源配置實(shí)例Demo
- Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
- Servlet+MyBatis項目轉(zhuǎn)Spring Cloud微服務(wù),多數(shù)據(jù)源配置修改建議
- Spring Boot 2.0多數(shù)據(jù)源配置方法實(shí)例詳解
- spring多數(shù)據(jù)源配置實(shí)現(xiàn)方法實(shí)例分析
相關(guān)文章
springboot項目啟動指定對應(yīng)環(huán)境的方法
這篇文章主要介紹了springboot項目啟動指定對應(yīng)環(huán)境的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08解決Feign調(diào)用的GET參數(shù)傳遞的問題
這篇文章主要介紹了解決Feign調(diào)用的GET參數(shù)傳遞的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03JavaWeb如何實(shí)現(xiàn)統(tǒng)一查詢接口(jfinal)
這篇文章主要介紹了JavaWeb如何實(shí)現(xiàn)統(tǒng)一查詢接口(jfinal),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Java輕松使用工具類實(shí)現(xiàn)獲取MP3音頻時長
在Java中,工具類定義了一組公共方法,這篇文章將介紹Java中使用工具類來獲取一個MP3音頻文件的時間長度,感興趣的同學(xué)繼續(xù)往下閱讀吧2021-10-10java實(shí)現(xiàn)操作系統(tǒng)中的最佳置換Optimal算法
這篇文章主要介紹了java實(shí)現(xiàn)操作系統(tǒng)中的最佳置換Optimal算法 ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02