1. 程式人生 > >一個程式配置多個數據源,並進行資料來源切換

一個程式配置多個數據源,並進行資料來源切換

1>.在資料庫接配置檔案中配置,資料庫連線資訊,資料來源選擇器,多個數據源資訊,sql會話工廠

<!-- 在applicationContext-dao.xml引入資料庫資訊配置檔案db.properties -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:db.properties" />
</bean>


<bean id="dataSource" class="cn.yingguo.utils.MultipleRoutingDataSource">
    <property name="targetDataSources">
	<map key-type="java.lang.String">
	    <!-- 指定lookupKey和與之對應的資料來源 -->
            <entry key="dataSource01" value-ref="dataSource01"></entry>
	    <entry key="dataSource02" value-ref="dataSource02"></entry>
	</map>
    </property>
    <!-- 這裡可以指定預設的資料來源 -->
    <property name="defaultTargetDataSource" ref="dataSource01" />
</bean>


<!-- 配置第一個資料來源 -->
<bean id="dataSource01" class="com.alibaba.druid.pool.DruidDataSource"
    destroy-method="close">
	<property name="driverClassName" value="${lxdb.driver}" />
		<property name="url" value="${lxdb.url}" />
		<property name="username" value="${lxdb.username}" />
		<property name="password" value="${lxdb.password}" />
</bean>


<!-- 配置第二個資料來源 -->
<bean id="XWLdataSource" class="com.alibaba.druid.pool.DruidDataSource"
    destroy-method="close">
	<property name="driverClassName" value="${xwldb.driver}" />
	<property name="url" value="${xwldb.url}" />
	<property name="username" value="${xwldb.username}" />
	<property name="password" value="${xwldb.password}" />
</bean>


<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 設定MyBatis核心配置檔案 -->
	<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	<!-- 設定資料來源 -->
	<property name="dataSource" ref="dataSource" />
</bean>

2>.下面是資料來源配置資訊db.properties檔案,我寫了兩個資料來源,分別在兩臺伺服器上的mysql和sqlServer資料庫

#第一個資料來源資訊SQLServer的
lxdb.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
lxdb.url=jdbc:sqlserver://xxx.xxx.xxx.xxx:1033;databaseName=資料庫名
lxdb.username=資料庫賬號
lxdb.password=資料庫密碼

#第二個資料來源資訊MySQL的
xwldb.driver = com.mysql.jdbc.Driver
xwldb.url = jdbc:mysql://xxx.xxx.xxx.xxx:3306/資料庫名?useUnicode=true&                                                                 characterEncoding=utf8
xwldb.username = 賬號
xwldb.password = 密碼

3>.資料來源連線選擇的路由

public class MultipleRoutingDataSource extends AbstractRoutingDataSource {
	@Override
	protected Object determineCurrentLookupKey() {
            //DataSourceContextHolderUtil是資料來源選擇的依據工具類,在裡面配置了字元對應的資料來源
	    String type = DataSourceContextHolderUtil.getDataSourceType();
	    //資料來源連線分配
	    return type;
	}
}

4>.

public class DataSourceContextHolderUtil {
	//對資料來源選擇分配的工具類
	public final static String DATA_SOURCE_LX = "dataSource01";
	public final static String DATA_SOURCE_XWL = "dataSource02";
    //對資料來源名,執行緒進行隔離
	private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
        //設定資料來源的方法,在使用的時候也是首先呼叫這個方法先進行設定指定的資料來源就可以對這個資料來源進行操作了,在使用完畢之後要記得清除資料來源資訊
	public static void setDataSourceType(String dataSource){
            contextHolder.set(dataSource);
	}
	//獲取資料來源
	public static String getDataSourceType(){
		return contextHolder.get();
	}
	//清除資料來源,每次使用完畢之後需要清除,以免訪問不同的資料來源出問題
	public static void clearDataSourceType(){
		contextHolder.remove();
	}
}