spring基于通用Dao的多数据源配置详解
程序员文章站
2022-06-19 23:41:51
有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用dao的,配置的时候问题不断,这种方式和资源文件冲突...
有时候在一个项目中会连接多个数据库,需要在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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。