导入Maven依赖
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
1、配置多个dataSource
<bean id="dataSourceCar" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${car.jdbc.url}"/> <property name="user" value="${car.jdbc.username}"/> <property name="password" value="${car.jdbc.password}"/> <property name="initialPoolSize" value="10"/> <property name="maxPoolSize" value="20"/> <property name="minPoolSize" value="10"/> <property name="maxIdleTime" value="1800"/> <property name="acquireRetryAttempts" value="3"/> <property name="acquireRetryDelay" value="300"/> <property name="autoCommitOnClose" value="false"/> <property name="idleConnectionTestPeriod" value="60"/> <property name="preferredTestQuery" value="SELECT 1"/> </bean> <bean id="dataSourceMgo" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialPoolSize" value="10"/> <property name="maxPoolSize" value="20"/> <property name="minPoolSize" value="10"/> <property name="maxIdleTime" value="1800"/> <property name="acquireRetryAttempts" value="3"/> <property name="acquireRetryDelay" value="300"/> <property name="autoCommitOnClose" value="false"/> <property name="idleConnectionTestPeriod" value="60"/> <property name="preferredTestQuery" value="SELECT 1"/> </bean> <!-- 使用自定义的路径选择器 --> <bean id="dataSource" class="cn.mobilemart.payment.configer.DynamicDataSource"> <property name="defaultTargetDataSource" ref="dataSourceMgo"/> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="dataSourceMgo" value-ref="dataSourceMgo"></entry> <entry key="dataSourceCar" value-ref="dataSourceCar"></entry> </map> </property> </bean>
2、新建DataSourceContextHolder
package cn.mobilemart.payment.configer;// // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\____ // . ' \\| |// `. // / \\||| : |||// \ // / _||||| -:- |||||- \ // | | \\\ - /// | | // | \_| ''\---/'' | | // \ .-\__ `-` ___/-. / // ___`. .' /--.--\ `. . __ // ."" '< `.___\_<|>_/___.' >'"". // | | : `- \`.;`\ _ /`;.`/ - ` : | | // \ \ `-. \_ __\ /__ _/ .-` / / // ======`-.____`-.___\_____/___.-`____.-'====== // `=---=' // // ............................................. // 佛祖镇楼 BUG辟易 /** * * @author suzhijie * @date 2018/4/19/上午11:22 */ 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(); } }
3.新建DynamicDataSource多数据源切换
package cn.mobilemart.payment.configer;// // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\____ // . ' \\| |// `. // / \\||| : |||// \ // / _||||| -:- |||||- \ // | | \\\ - /// | | // | \_| ''\---/'' | | // \ .-\__ `-` ___/-. / // ___`. .' /--.--\ `. . __ // ."" '< `.___\_<|>_/___.' >'"". // | | : `- \`.;`\ _ /`;.`/ - ` : | | // \ \ `-. \_ __\ /__ _/ .-` / / // ======`-.____`-.___\_____/___.-`____.-'====== // `=---=' // // ............................................. // 佛祖镇楼 BUG辟易 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * 多数据源切换 * * @author suzhijie * @date 2018/4/19/上午11:11 */ public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDBType(); } }
4.ServiceImpl使用前添加
DataSourceContextHolder.setDBType("dataSourceCar");
DataSourceContextHolder.setDBType("dataSourceMgo");
切换数据源