destroy-method="close" init-method="init" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}" p:connectionProperties="druid.stat.slowSqlMillis=5000;druid.stat.logSlowSql=true"
p:username="${jdbc.username}" p:removeAbandoned="true"
p:removeAbandonedTimeout="3600" p:logAbandoned="true" p:password="${jdbc.password}"
p:minIdle="1" p:maxActive="10" p:poolPreparedStatements="true"
p:maxPoolPreparedStatementPerConnectionSize="20" p:initialSize="1"
p:maxWait="60000" p:testWhileIdle="true" p:validationQuery="select 1"
p:timeBetweenEvictionRunsMillis="60000" p:filters="stat,slf4j">
<description>开发环境配置</description>
</bean>
<!-- 采集数据源 -->
<bean id="cjdataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close" init-method="init" p:driverClassName="${cj.jdbc.driverClassName}"
p:url="${cj.jdbc.url}" p:connectionProperties="druid.stat.slowSqlMillis=5000;druid.stat.logSlowSql=true"
p:username="${cj.jdbc.username}" p:removeAbandoned="true"
p:removeAbandonedTimeout="3600" p:logAbandoned="true" p:password="${cj.jdbc.password}"
p:minIdle="1" p:maxActive="10" p:poolPreparedStatements="true"
p:maxPoolPreparedStatementPerConnectionSize="20" p:initialSize="1"
p:maxWait="60000" p:testWhileIdle="true" p:validationQuery="select 1"
p:timeBetweenEvictionRunsMillis="60000" p:filters="stat,slf4j">
<description>开发环境配置</description>
</bean>
<bean id="dynamicDataSource" class="com.aisino.web.util.DynamicDataSource">
<!-- 通过key-value的形式来关联数据源 -->
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="cjdataSource" key="cjdataSource"></entry>
<entry value-ref="dataSource51" key="dataSource51"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="cjdataSource">
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
p:dataSource-ref="dynamicDataSource" p:configLocation="classpath:config/ibatis/sql-map-config.xml" />
DynamicDataSource类
package com.aisino.web.util;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
//核心方法
@Override
protected Object determineCurrentLookupKey() {
return JdbcContextHolder.getJdbcType();
}
}
JdbcContextHolder 类
package com.aisino.web.util;
public class JdbcContextHolder {
//本地线程
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setJdbcType(String jdbcType)
{
contextHolder.set(jdbcType);
}
public static String getJdbcType()
{
//获取内容上下文配置
return (String) contextHolder.get();
}
public static void clearJdbcType()
{
//移除内容上下文配置
contextHolder.remove();
}
}
切换数据源的方法
JdbcContextHolder.setJdbcType("cjDataSource");