java中的接口是类吗
309
2023-02-11
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
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return dataSourceKey.get();
}
}
spring配置文件如下:
xmlns:context=xqmfVvYE"http://springframework.org/schema/context" xmlns:p="http://springframework.org/schema/p" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd"> p:driverClassName="${mysql.driver}" p:url="${mysql.url}" p:username="${mysql.username}" p:password="${mysql.password}"/> p:driverClassName="${mysql2.driver}" p:url="${mysql2.url}xqmfVvYE" p:username="${mysql2.username}" p:password="${mysql2.password}"/> p:dataSource-ref="multipleDataSource" p:mapperLocations="classpath:com/etoak/dao/*-mapper.xmlxqmfVvYE"/>
xmlns:context=xqmfVvYE"http://springframework.org/schema/context"
xmlns:p="http://springframework.org/schema/p"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.2.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.2.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">
p:driverClassName="${mysql.driver}" p:url="${mysql.url}" p:username="${mysql.username}" p:password="${mysql.password}"/> p:driverClassName="${mysql2.driver}" p:url="${mysql2.url}xqmfVvYE" p:username="${mysql2.username}" p:password="${mysql2.password}"/> p:dataSource-ref="multipleDataSource" p:mapperLocations="classpath:com/etoak/dao/*-mapper.xmlxqmfVvYE"/>
p:driverClassName="${mysql.driver}"
p:url="${mysql.url}"
p:username="${mysql.username}"
p:password="${mysql.password}"/>
p:driverClassName="${mysql2.driver}" p:url="${mysql2.url}xqmfVvYE" p:username="${mysql2.username}" p:password="${mysql2.password}"/>
p:driverClassName="${mysql2.driver}"
p:url="${mysql2.url}xqmfVvYE"
p:username="${mysql2.username}"
p:password="${mysql2.password}"/>
p:dataSource-ref="multipleDataSource" p:mapperLocations="classpath:com/etoak/dao/*-mapper.xmlxqmfVvYE"/>
p:dataSource-ref="multipleDataSource"
p:mapperLocations="classpath:com/etoak/dao/*-mapper.xmlxqmfVvYE"/>
测试类如下:
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 = (xqmfVvYEProductDaoIf)ac.getBean(ProductDaoIf.class);
MultipleDataSource.setDataSourceKey("ds1");
xqmfVvYEint count1 = proDao.selectProductCount();
MultipleDataSource.setDataSourceKey("ds2");
int count2 = proDao.selectProductCount();
System.out.println(count1);
System.out.println(count2);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~