[TOP]
1. 动态切换数据源
1.1 思路
1.2 配置文件示例
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
# db1
jdbc.db1.username=db1
jdbc.db1.password=db1
jdbc.db1.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
#db2
jdbc.db2.username=db2
jdbc.db2.password=db2
jdbc.db2.url=jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:jdbc.properties" />
<bean id="abstractDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" abstract="true">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="initialSize" value="15" /> <!-- 初始化连接数 -->
<property name="minIdle" value="3" /> <!-- 最小空闲连接数 -->
<property name="maxActive" value="20" /> <!-- 最大连接池数量 -->
<property name="maxWait" value="1000" /> <!-- 获取连接时最大等待时间,单位毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 空闲对象回收器由运行间隔(毫秒),若需要回收, 该值最好小于 minEvictableIdleTimeMillis值 -->
<property name="minEvictableIdleTimeMillis" value="300000"/> <!-- 被空闲对象回收器回收前在池中保持空闲状态的最小时间(毫秒) -->
<property name="validationQuery" value="SELECT 'x'"/> <!-- 用来检测连接是否有效的sql,要求是一个查询语句 -->
<property name="testWhileIdle" value="true"/> <!-- 空闲时是否验证, 若不通过断掉连接, 前提是空闲对象回收器开启状态 -->
<property name="testOnBorrow" value="false"/> <!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 -->
<property name="testOnReturn" value="false"/> <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
<property name="filters" value="stat"/> <!-- 属性类型是字符串,通过别名的方式配置扩展插件 -->
</bean>
<bean id="apiDataSource" class="com.alibaba.druid.pool.DruidDataSource" parent="abstractDataSource">
<property name="url" value="${jdbc.api.url}"/>
<property name="username" value="${jdbc.api.username}"/>
<property name="password" value="${jdbc.api.password}"/>
</bean>
<bean id="consoleDataSource" class="com.alibaba.druid.pool.DruidDataSource" parent="abstractDataSource">
<property name="url" value="${jdbc.console.url}"/>
<property name="username" value="${jdbc.console.username}"/>
<property name="password" value="${jdbc.console.password}"/>
</bean>
<bean id="commonDataSource" class="com.alibaba.druid.pool.DruidDataSource" parent="abstractDataSource">
<property name="url" value="${jdbc.common.url}"/>
<property name="username" value="${jdbc.common.username}"/>
<property name="password" value="${jdbc.common.password}"/>
</bean>
<bean id="payDataSource" class="com.alibaba.druid.pool.DruidDataSource" parent="abstractDataSource">
<property name="url" value="${jdbc.pay.url}" />
<property name="username" value="${jdbc.pay.username}" />
<property name="password" value="${jdbc.pay.password}" />
</bean>
<bean id="updateDataSource" class="com.alibaba.druid.pool.DruidDataSource" parent="abstractDataSource">
<property name="url" value="${jdbc.update.url}"/>
<property name="username" value="${jdbc.update.username}"/>
<property name="password" value="${jdbc.update.password}"/>
</bean>
<bean id="dynamicDataSource" class="com.shinian.dataSource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="api" value-ref="apiDataSource" />
<entry key="console" value-ref="consoleDataSource" />
<entry key="common" value-ref="commonDataSource" />
<entry key="pay" value-ref="payDataSource" />
<entry key="update" value-ref="updateDataSource" />
</map>
</property>
<property name="defaultTargetDataSource" ref="consoleDataSource" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dynamicDataSource" />
</bean>
</beans>