如何在spring配置多个数据源
2个回答
展开全部
给你一个例子:
context.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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<jee:jndi-lookup id="mysqlds" jndi-name="java:comp/env/jdbc/mysqlds" />
<jee:jndi-lookup id="orads" jndi-name="java:comp/env/jdbc/orads" />
<bean id="dataSource" class="util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="0" value-ref="mysqlds" />
<entry key="1" value-ref="orads" />
</map>
</property>
<property name="defaultTargetDataSource" ref="orads" />
</bean>
</beans>
package util;
public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return (String) contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
package util;
import java.sql.SQLException;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
//static Logger log = Logger.getLogger("DynamicDataSource");
@Override
protected Object determineCurrentLookupKey() {
// TODO
return DbContextHolder.getDbType();
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
测试程序:
DbContextHolder.setDbType("1");
UserDomain od = this.testService.queryUserAccount("admin");
System.out.println("username=="+od.getName());
DbContextHolder.setDbType("0");
d = this.testService.queryUserAccount("admin");
context.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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<jee:jndi-lookup id="mysqlds" jndi-name="java:comp/env/jdbc/mysqlds" />
<jee:jndi-lookup id="orads" jndi-name="java:comp/env/jdbc/orads" />
<bean id="dataSource" class="util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="0" value-ref="mysqlds" />
<entry key="1" value-ref="orads" />
</map>
</property>
<property name="defaultTargetDataSource" ref="orads" />
</bean>
</beans>
package util;
public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return (String) contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
package util;
import java.sql.SQLException;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
//static Logger log = Logger.getLogger("DynamicDataSource");
@Override
protected Object determineCurrentLookupKey() {
// TODO
return DbContextHolder.getDbType();
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
测试程序:
DbContextHolder.setDbType("1");
UserDomain od = this.testService.queryUserAccount("admin");
System.out.println("username=="+od.getName());
DbContextHolder.setDbType("0");
d = this.testService.queryUserAccount("admin");
2014-10-27
展开全部
配置两个SessionFactory
在DAO定义时指定sessionFactory
在DAO定义时指定sessionFactory
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询