iBatis多數據源的苦惱
在僅使用ibatis時
采用spring的AbstractRoutingDataSource就可以簡單的解決這個問題
AbstractRoutingDataSource實現了javax
Spring+iBatis多數據源的配置
下面是一個完整的配置
< beans xmlns=
xmlns:xsi=
xmlns:aop=
xmlns:tx=
xmlns:jee=
xsi:schemaLocation=
< !
< jee:jndi
< jee:jndi
< jee:jndi
< bean id=
< property name=
< map key
< entry key=
< entry key=
< entry key=
< /map>
< /property>
< property name=
< /bean>
< !
< bean id=
< property name=
< property name=
< /bean>
< bean id=
< property name=
< /bean>
< bean id=
< property name=
< /bean>
< /beans>
其核心是DynamicDataSource
package com
import org
import org
public class DynamicDataSource extends AbstractRoutingDataSource {
static Logger log = Logger
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto
return DbContextHolder
}
}
上下文DbContextHolder為一線程安全的ThreadLocal
package com
public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType) {
contextHolder
}
public static String getDbType() {
return (String) contextHolder
}
public static void clearDbType() {
contextHolder
}
}
sql
< ?xml version=
< !DOCTYPE sqlMapConfig PUBLIC
< sqlMapConfig>
< sqlMap resource=
< /sqlMapConfig>
這樣在調用service之前只需要設置一下上下文即可調用相應的數據源
DbContextHolder
//execute services
//
dao如下
package com
import java
import java
import java
import org
import org
import com
public class TestDAO extends SqlMapClientDaoSupport implements ITestDAO {
static Logger log = Logger
public TestObj getTestObj(String objID) throws Exception {
return (TestObj) getSqlMapClientTemplate()
}
}
以上就介紹了Spring+iBatis多數據源的配置方案
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28536.html