spring基于通用Dao的多数据源配置
来源:互联网 发布:呼死你 淘宝上叫什么 编辑:程序博客网 时间:2024/05/17 02:22
有时候在一个项目中会连接多个数据库,需要在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); }@Overrideprotected Object determineCurrentLookupKey() {// TODO Auto-generated method stubreturn 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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvchttp://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);}}
3 0
- spring基于通用Dao的多数据源配置
- 基于标注的spring多数据源配置与使用
- 基于注解的Spring多数据源配置和使用
- 基于xml的Spring多数据源配置和使用
- 基于注解的Spring多数据源配置和使用
- 基于注解的spring多数据源配置
- 基于注解的Spring多数据源配置和使用
- 基于注解的Spring多数据源配置和使用
- 基于注解的Spring多数据源配置和使用
- 基于注解的Spring多数据源配置和使用
- 基于注解的spring多数据源配置和使用
- 基于SQL语句的Spring多数据源配置
- Spring基于注解的多数据源配置
- 基于注解的Spring多数据源配置和使用
- 基于spring boot项目的多数据源配置
- 基于Spring Boot与Spring Data JPA的多数据源配置
- Spring配置多数据源以及基于Atomikos的事务一致性配置
- 基于注解spring中多数据源配置
- JQuery事件处理
- git分支
- 第2章第1节 栈
- 菜鸟学习中-javascript中获取时间
- SQL Where特殊的三个条件(between,in,like(字符串匹配,模糊查寻))
- spring基于通用Dao的多数据源配置
- libcudart.so.7.0: cannot open shared object file: No such file or directory
- jasperreport的使用
- 电脑蓝屏代码大全
- Hello 2016
- 上架相关
- Iphone 5s 8.0.2越狱
- cas 单点登录,退出配置
- 协方差的意义和计算公式