最近在项目中使用mybatis配置多数据源,这里使用AbstractRoutingDataSource 实现:
1、文件结构
2、jdbc.properties
driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8
jdbc_username=root
jdbc_password=123456
driverClassName_1=com.mysql.jdbc.Driver
jdbc_url_1=jdbc:mysql://localhost:3306/ssm2?useUnicode=true&characterEncoding=UTF-8
jdbc_username_1=root
jdbc_password_1=123456
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
2、spring 与 mybatis 整合的 xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.lsy.ssm.*">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName_1}" />
<property name="url" value="${jdbc_url_1}" />
<property name="username" value="${jdbc_username_1}" />
<property name="password" value="${jdbc_password_1}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 继承 spring AbstractRoutingDataSource 实现多数据源 -->
<bean id="multipleDataSource" class="com.lsy.ssm.util.multipleDataSource">
<!-- 设置默认的目标数据源 -->
<property name="defaultTargetDataSource" ref="dataSource" />
<property name="targetDataSources">
<map>
<entry key="dataSource" value-ref="dataSource" />
<entry key="dataSource2" value-ref="dataSource2" />
</map>
</property>
</bean>
<!-- spring和MyBatis整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="multipleDataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/lsy/ssm/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lsy.ssm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
3、编写 multipleDataSource 类
package com.lsy.ssm.util;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class multipleDataSource extends AbstractRoutingDataSource {
public static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
@Override
protected Object determineCurrentLookupKey() {
return dataSourceKey.get();
}
}
4、service层
package com.lsy.ssm.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lsy.ssm.dao.IUserDao;
import com.lsy.ssm.pojo.User;
import com.lsy.ssm.service.IUserService;
import com.lsy.ssm.util.multipleDataSource;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
public User getUserById(int userId) {
//切换数据源,默认为配置的dataSource
multipleDataSource.setDataSourceKey("dataSource2");
return this.userDao.selectByPrimaryKey(userId);
}
}
5、测试结果(这里切到了ssm2的数据源查出了数据)
6、拓展
这里也可以拓展使用spring AOP方式实现自动切换,大家可以自己尝试一下,道理是一样的。
用的不精,还请各位大神多多提建议~~