JavaWEB項目之如何配置動態(tài)數(shù)據(jù)源
JavaWEB項目配置動態(tài)數(shù)據(jù)源
說明
項目中如果需要連接多個數(shù)據(jù)庫,則需要配置動態(tài)數(shù)據(jù)源,對于使用Spring+MyBatis框架的項目來說配置動態(tài)數(shù)據(jù)源一般需要在SqlMapConfig.xml中配置
接下來是配置步驟:
步驟
在配置JDBC連接的文件中配置動態(tài)數(shù)據(jù)源
<?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>
<!--數(shù)據(jù)源一-->
<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>
<!--數(shù)據(jù)源二-->
<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>
<!--這里配置數(shù)據(jù)源管理工具類,自定義一個管理類用于設置以及獲取當前數(shù)據(jù)源名稱等操作,該類需要繼承AbstractRoutingDataSource類并實現(xiàn)其中的抽象方法-->
<bean id="dataSource" class="自定義DbcontrxtHolder類的全線名稱(包名.類名)">
<!-- 設置默認數(shù)據(jù)源 -->
<property name="defaultTargetDataSource" ref="dataSource1"/>
<property name="targetDataSources">
<map>
<!-- 配置數(shù)據(jù)源列表 key為切換數(shù)據(jù)源時所用的名稱,value-ref為配置datasource的bean的id值 -->
<entry key="dataSource1" value-ref="dataSource1"/>
<entry key="dataSource2" value-ref="dataSource2"/>
</map>
</property>
</bean>
<!--后面配置事務等其他項,這里不再列出-->!
</beans> 新建數(shù)據(jù)源管理工具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>();
//添加動態(tài)數(shù)據(jù)源時指定名稱,用于切換數(shù)據(jù)源時獲取連接對象 其變量值為 targetDataSources鍵值
public static final String DATASOURCE1 = "dataSource1";
public static final String DATASOURCE2 = "dataSource2";
/**
* 設置當前數(shù)據(jù)源
* @param dbType
*/
public static void setDbType(String dbType){
contextHolder.set(dbType);
}
/**
* 獲得當前數(shù)據(jù)源
* @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();
}
}在業(yè)務中切換數(shù)據(jù)源
public void test(){
...業(yè)務代碼
DbcontextHolder.setDbType(DbcontextHolder.DATASOURCE2);//切換數(shù)據(jù)源2
? ? ?//保存信息當前數(shù)據(jù)源
? ? ?if(!saveSendTaskSimple(sendTask)){
?? ??? ??? ?return false;
?? ??? ?}
//清理上下文信息,恢復到默認數(shù)據(jù)源?? ??? ?
DbcontextHolder.clearContext();
...業(yè)務代碼
}web項目中配置多個數(shù)據(jù)源
spring + mybatis 多數(shù)據(jù)源配置有兩種解決方案
1、配置多個不同的數(shù)據(jù)源,使用一個sessionFactory,在業(yè)務邏輯使用的時候自動切換到不同的數(shù)據(jù)源,有一個種是在攔截器里面根據(jù)不同的業(yè)務現(xiàn)切換到不同的datasource; 有的會在業(yè)務層根據(jù)業(yè)務來自動切換。
2、在spring項目中配置多個不同的數(shù)據(jù)源datasource,配置多個sqlSessionFactory,每個sqlSessionFactory對應一個datasource,在dao層根據(jù)不同業(yè)務自行選擇使用哪個數(shù)據(jù)源的session來操作。
(一)定義數(shù)據(jù)源名稱常量
public class DataSourceType {
?? ?public ?static final String ?SOURCE_MYSQL = "mysql_dataSource";
?? ?public ?static final String ?SOURCE_POSTGS = "postgs_dataSource";
}(二)創(chuàng)建負責切換數(shù)據(jù)源的類
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(); ?
? ? } ?
}(三) 建立動態(tài)數(shù)據(jù)源類
該類必須繼承AbstractRoutingDataSource,且實現(xiàn)方法determineCurrentLookupKey
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
?? ?@Override
?? ?protected Object determineCurrentLookupKey() {
?? ??? ?return DataSourceContextHolder. getDbType();
?? ?}
}(四)配置xml
<!-- 配置mysql --> ?? ?<bean id="mysql_dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> ? ? ? ? <property name="driverClassName" value="com.mysql.jdbc.Driver" /> ? ? ? ? <property name="url" value="jdbc:mysql://127.0.0.1:3306/colleges" /> ? ? ? ? <property name="username" value="root" /> ? ? ? ? <property name="password" value="root" /> ? ? </bean> ? ? <!-- 配置postgs --> ? ? ?<bean id="postgs_dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> ?? ??? ?<property name="driverClassName" value="org.postgresql.Driver" /> ?? ??? ?<property name="url" value="jdbc:postgresql://114.215.83.3:5432/coges" /> ?? ??? ?<property name="username" value="postgres" /> ?? ??? ?<property name="password" value="postgres" /> ?? ?</bean> ?? ?<!-- 配置動態(tài)數(shù)據(jù)源 --> ?? ? <bean id ="dataSource" class= "com.mote.dc.changedb.DynamicDataSource" > ? ? ? ? ? ? <property name ="targetDataSources"> ? ? ? ? ? ? ? ? ? <map key-type ="java.lang.String"> ? ? ? ? ? ? ? ? ? ? ? ? <entry value-ref ="postgs_dataSource" key= "postgs_dataSource"></entry > ? ? ? ? ? ? ? ? ? ? ? ? <entry value-ref ="mysql_dataSource" key= "mysql_dataSource"></entry > ? ? ? ? ? ? ? ? ? </map > ? ? ? ? ? ? </property> ? ? ? ? ? ? <!-- 默認使用mysql --> ? ? ? ? ? ? <property name ="defaultTargetDataSource" ref= "mysql_dataSource"></property > ? ? ? ? ?</bean>
(五)當需要使用某個數(shù)據(jù)庫的時候
使用下面一行代碼進行切換
//切換數(shù)據(jù)庫 DataSourceContextHolder.setDbType(DataSourceType.SOURCE_POSTGS);
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring boot監(jiān)控Actuator-Admin實現(xiàn)過程詳解
這篇文章主要介紹了Spring boot監(jiān)控Actuator-Admin實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
IDEA中properties與yml文件的轉(zhuǎn)變方式
文章介紹了如何在IntelliJ IDEA 2021.1.1中安裝和使用ConvertYAMLandPropertiesFile插件進行YAML和Properties文件之間的轉(zhuǎn)換,安裝步驟包括導航到設置、安裝插件、找到并安裝插件等,插件支持從Properties文件轉(zhuǎn)換為YAML文件,但轉(zhuǎn)換過程中會丟失注釋2024-12-12
Jmeter的接口測試詳細步驟并實現(xiàn)業(yè)務閉環(huán)
這篇文章主要介紹了Jmeter的接口測試詳細步驟并實現(xiàn)業(yè)務閉環(huán),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
Spring Cloud多個微服務之間調(diào)用代碼實例
這篇文章主要介紹了Spring Cloud多個微服務之間調(diào)用代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12

