說明
項目中如果需要連接多個數據庫,則需要配置動態數據源,對於使用Spring+MyBatis框架的項目來說配置動態數據源一般需要在SqlMapConfig.xml中配置
接下來是配置步驟:
步驟
- 在配置JDBC連接的文件中配置動態數據源
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd"
>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 引入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<!--數據源一-->
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc1.driver}"/>
<!-- 基本屬性 url、user、password -->
<property name="url" value="${jdbc1.url}"/>
<property name="username" value="${jdbc1.username}"/>
<property name="password" value="${jdbc1.password}"/>
</bean>
<!--數據源二-->
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc1.driver}"/>
<!-- 基本屬性 url、user、password -->
<property name="url" value="${jdbc1.url}"/>
<property name="username" value="${jdbc1.username}"/>
<property name="password" value="${jdbc1.password}"/>
</bean>
<!--這裏配置數據源管理工具類,自定義一個管理類用於設置以及獲取當前數據源名稱等操作,該類需要繼承AbstractRoutingDataSource類並實現其中的抽象方法-->
<bean id="dataSource" class="自定義DbcontrxtHolder類的全線名稱(包名.類名)">
<!-- 設置默認數據源 -->
<property name="defaultTargetDataSource" ref="dataSource1"/>
<property name="targetDataSources">
<map>
<!-- 配置數據源列表 key爲切換數據源時所用的名稱,value-ref爲配置datasource的bean的id值 -->
<entry key="dataSource1" value-ref="dataSource1"/>
<entry key="dataSource2" value-ref="dataSource2"/>
</map>
</property>
</bean>
<!--後面配置事務等其他項,這裏不再列出-->!
</beans>
- 新建數據源管理工具DbcontrxtHolder類
package com.framework;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DbcontextHolder extends AbstractRoutingDataSource{
public static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
//添加動態數據源時指定名稱,用於切換數據源時獲取連接對象 其變量值爲 targetDataSources鍵值
public static final String DATASOURCE1 = "dataSource1";
public static final String DATASOURCE2 = "dataSource2";
/**
* 設置當前數據源
* @param dbType
*/
public static void setDbType(String dbType){
contextHolder.set(dbType);
}
/**
* 獲得當前數據源
* @return
*/
public static String getDbType(){
String dbType = (String)contextHolder.get();
return dbType;
}
/**
*清除上下文
*
*/
public static void clearContext(){
contextHolder.remove();
}
@Override
protected Object determineCurrentLookupKey() {
return DbcontextHolder.getDbType();
}
}
- 在業務中切換數據源
public void test(){
...業務代碼
DbcontextHolder.setDbType(DbcontextHolder.DATASOURCE2);//切換數據源2
//保存信息當前數據源
if(!saveSendTaskSimple(sendTask)){
return false;
}
//清理上下文信息,恢復到默認數據源
DbcontextHolder.clearContext();
...業務代碼
}
配置並切換數據源的操作基本完成