AbstractRoutingDataSource是spring实现的一个数据源,负责动态路由数据源的功能,我们仅需要实现determineCurrentLookupKey()方法即可
一,自定义DataSourceContextHolder
/**
*
* 数据源上下文句柄
* <p>
*
* 在dao或service层通过 {@link #set(String)} 向当前线程设置访问数据源字符串, 由DynamicDataSource类自动调用
*
* @author sunflower
* @see DynamicDataSource
*/
public class DataSourceContextHolder {
private static final ThreadLocal<String> DS_CONTEXT = new ThreadLocal<String>();
/**
* 获取当前线程的数据key 由<code>DynamicDataSource</code>类的
* {@link DynamicDataSource#determineCurrentLookupKey()}使用
*
* @return
*/
public static String get() {
return DS_CONTEXT.get();
}
/**
* 设置数据源key,
*
* @param dataSourceKey
*/
public static void set(String dataSourceKey) {
DS_CONTEXT.set(dataSourceKey);
}
/**
* 移除当前线程中设置的数据源key
*/
public static void remove() {
DS_CONTEXT.remove();
}
}
二,RoutingDataSource的实现
/**
* 动态路由数据源
* <p>
*
* @author sunflower
*
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
/**
* 确定当前选择的数据源key
* <p>
* key由当前线程{@link DataSourceContextHolder#set(String)}
*/
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.get();
}
}
三,定义dao(此略),service
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List<User> queryRecords(int first, int max, Map condition) {
// 与通常单一数据源的service不一样的是,servce方法中要设置访问的数据库key,
//其他都一样
DataSourceContextHolder.set("nj");
// ....condition.............
}
}
四,配置spring bean 文件
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="userService" class="org.zkway.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="org.zkway.dao.impl.UserDAOImpl"> <property name="userDao" ref="userDao"></property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="/WEB-INF/config/sql-map-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="org.zkway.common.util.db.DynamicDataSource"> <property name="targetDataSources"> <map> <entry key="nj" value-ref="njDataSource"></entry> <entry key="bj" value-ref="bjDataSource"></entry> <entry key="sz" value-ref="szDataSource"></entry> <entry key="sh" value-ref="njDataSource"></entry> </map> </property> </bean> <!-- 数据源配置,不同数据源配置--> <bean id="njDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="详细配置省略"></property> </bean> <bean id="shDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="详细配置省略"></property> </bean> <bean id="bjDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="详细配置省略"></property> </bean> <bean id="szDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="详细配置省略"></property> </bean> </beans>