一、自動切換數據源
數據源類型常量類(也可以是枚舉類),具體代碼如下:
/**
* 數據源名稱
*
* @author hrc
* @date 2018年10月9日
*/
public class DataSourceDialect {
public static final String ORACLE="oracle";
public static final String MYSQL="mysql";
}
數據源切換類,構建一個數據源類型容器,並提供了向其中設置、獲取和清空數據源類型的方法,具體代碼如下:
/**
* 數據源類型容器
*
* @author hrc
* @date 2018年10月9日
*/
public class DataSourceSwitch {
/**
* 保存數據源類型線程安全容器
*/
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
/**
* 設置數據源類型
* @param dataSourceType 數據源類型
*/
public static void setDataSourceType (String dataSourceType) {
contextHolder.set(dataSourceType);
}
/**
* 獲取數據源類型
* @return
*/
public static String getDataSourceType(){
return (String) contextHolder.get();
}
/**
* 清空數據源類型
*/
public static void clearDataSourceType(){
contextHolder.remove();
}
}
動態切換數據源類,該類需要繼承AbstractRoutingDataSource類,並重寫determineCurrentLookupKey()方法,從數據源類型容器中獲取當前線程的數據源類型。相關代碼如下:
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DataSourceSwitch.getDataSourceType();
}
}
二、數據源信息配置文件修改
jdbc.properties |
|
三、Spring-Mybatis配置文件修改
spring-mybatis.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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!--1 加載數據資源屬性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean>
<!-- 配置數據源 ,dbcp --> <bean id="oracleDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.oracle.diverClassName}" /> <property name="url" value="${jdbc.oracle.url}" /> <property name="username" value="${jdbc.oracle.username}" /> <property name="password" value="${jdbc.oracle.password}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${jdbc.initialSize}" /> <!-- 連接池最大數量 --> <property name="maxActive" value="${jdbc.maxActive}" /> <!-- 連接池最大空閒 --> <!-- <property name="maxIdle" value="${maxIdle}" /> --> <!-- 連接池最小空閒 --> <property name="minIdle" value="${jdbc.minIdle}" /> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${jdbc.maxWait}" /> </bean>[A1] <!-- 配置數據源 ,dbcp --> <bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.mysql.diverClassName}" /> <property name="url" value="${jdbc.mysql.url}" /> <property name="username" value="${jdbc.mysql.username}" /> <property name="password" value="${jdbc.mysql.password}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${jdbc.initialSize}" /> <!-- 連接池最大數量 --> <property name="maxActive" value="${jdbc.maxActive}" /> <!-- 連接池最大空閒 --> <!-- <property name="maxIdle" value="${maxIdle}" /> --> <!-- 連接池最小空閒 --> <property name="minIdle" value="${jdbc.minIdle}" /> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${jdbc.maxWait}" /> <!-- <property name="validationQuery" value="select 1 from dual" /> --> <property name="testOnBorrow" value="true"/> </bean>[A2] <bean id="dataSource" class="com.zldc.common.datasource. DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="ORACLE" value-ref="oracleDataSource"></entry> <entry key="MYSQL" value-ref="mysqlDataSource"></entry> </map> </property> <!-- 這裏可以指定默認的數據源 --> <property name="defaultTargetDataSource" ref="oracleDataSource" /> </bean>[A3]
<!-- 3 配置sessionfactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 加載mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis.xml" /> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/zldc/*/mapper/*.xml," /> </bean> <!-- 4 裝配dao接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zldc.**.mapper" /> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 5、聲明式事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 6、註解事務切面 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>[A1]配置數據源一信息:oracle [A2]配置數據源二信息:MySQL [A3]使用動態數據源類創建數據源 |
四、數據源切換代碼
try {
DataSourceSwitch.setDataSourceType(DataSourceDialect.MYSQL);
/*
* 代碼
*/
} finally {
DataSourceSwitch.clearDataSourceType();
}