继续上篇文章如何使用mybatis3+spring3并且配置多数据源呢
先上代码在讲解吧
替换上一篇中spring中datasource的配置
<!-- 数据源配置 --> <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/payoffdatabase?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource" class="a.b.router.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="ds1" key="ds1"></entry> <entry value-ref="ds2" key="ds2"></entry> </map> </property> <property name="defaultTargetDataSource" ref="ds1"></property> </bean>
新的类DynamicDataSource
package a.b.router;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDbType();
}
}新的类DataSourceContextHolder
package a.b.router;
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return ((String) contextHolder.get());
}
public static void clearDbType() {
contextHolder.remove();
}
}原来的单元测试新添加一行信息,修改为
@Test
public void addTest() throws Exception {
UserLogin userLogin = new UserLogin();
userLogin.setPassword("102");
userLogin.setUsername("102");
DataSourceContextHolder.setDbType("ds2");
service.add(userLogin);
}最主要的变化是DynamicDataSource 类,这个类继承了AbstractRoutingDataSource,我们再继续考察,发现这个类实现了datasource这个接口。
再仔细研究,发现我配置了多个数据源给DynamicDataSource,默认的数据源是ds1。
我们研究下AbstractRoutingDataSource类的代码,主要是两个实现datasource的方法
public Connection getConnection() throws SQLException {
return determineTargetDataSource().getConnection();
}
public Connection getConnection(String username, String password) throws SQLException {
return determineTargetDataSource().getConnection(username, password);
}根据这段代码发现主要玄机都在determineTargetDataSource()方法上。
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}根据这段代码发现,首先在使用数据源之前,首先判断使用数据源的key,也就是我们配置给
private Map<Object, Object> targetDataSources;
这个map中的key值,找到key值之后再找到对应的datasource然后并使用这个数据源。
从上面我们发现,实际上DynamicDataSource只是在内部封装了数据源,然后调用它,只不过在内部他加了一些控制而已。(此处不知道是否可以理解为代理模式)
再深一步想想,此处使用的orm层是mybatis,如果换成hibernate呢,或者jdbctemplate呢。
实际上这个方法都适用。