您好,登錄后才能下訂單哦!
有時候在一個項目中會連接多個數據庫,需要在spring中配置多個數據源,最近就遇到了這個問題,由于我的項目之前是基于通用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); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。