Manchmal werden in einem Projekt mehrere Datenbanken verbunden, und mehrere Datenquellen müssen im Frühjahr konfiguriert werden. Ich bin in letzter Zeit auf dieses Problem gestoßen. Da mein Projekt zuvor auf allgemeinem DAO basierte, gibt es während der Konfiguration ständige Probleme, und diese Methode steht in Konflikt mit Ressourcendateien. Wenn Sie die Zuordnungsdatei scannen, muss der Bean -Name der SQLSessionFactory SQLSessionFactory sein. Es kann SQLSessionFactory2 oder andere Namen nicht lesen. Die endgültige Lösung lautet wie folgt:
1. Fügen Sie das folgende KlassenmultiPledatasource.java zum Projekt hinzu
Paket com.etoak.util; import org.springframework.jdbc.datasource.lookup.abstractingDataSource; öffentliche Klasse MultiPledataSource erweitert AbstractroutingDataSource {private statische endgültige ThreadLocal <string> dataSourceKey = new inheritablethreadLocal <string> (); public static void setDataSourceKey (String DataSource) {DataSourceKey.set (DataSource); } @Override Protected Object DesticalCurrentLookupkey () {// Todo automatisch generierte Methode Stub return DataSourceKey.get (); }} Die Federkonfigurationsdatei lautet wie folgt:
<beans xmlns = "http://www.springframework.org/schema/bean xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://www.springframework.org/schema/beans/spring-beans-3.xsd http://www.springframework.org/schema/context 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: Eigentumsplatzhalle Lace =" Classpath: DB.Properties "/<boane Ids1" DB.Properties "//> <Bean id. p:driverClassName="${mysql.driver}" p:url="${mysql.url}" p:username="${mysql.username}" p:password="${mysql.password}"/> <bean id="ds2" p:driverClassName="${mysql2.driver}" p: url = "$ {mySql2.url}" p: userername = "$ {mysql2.username}" p: password = "$ {mysql2.password}"/> <bean id = "multiPledataSource"> <Eigentum name = "DefaultTargetDataSources" - "DS1" - " -" yampledaTaSources " -" DS1 "/"/"<Botschaft" = "TAGELTARGEDAGE". key = "ds1" value-ref = "ds1"/> <Eintrag key = "ds2" value-ref = "ds2"/> </map> </property> </bean> <bean id = " p: mapperlocations = "classPath: com/etoak/dao/*-mapper.xml"/> <bean> <bean> <Eigenschaft name = "Basepackage" value = "com.etoak.dao"/> <Property name = "markerInterface" value = "com.etoak.dao.basedao"/> </> </> </> </> </> Die Testklasse lautet wie folgt:
Paket com.etoak.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.filesystemxMlApplicationContext; import com.etoak.dao.productdaoif; import com.etoak.util.multiplidatasource; public class test {public static void main (String [] args) {applicationContext ac = new FileSystemXMLApplicationContext ("WebContent/webinf/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); }}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.