JavaWEB項(xiàng)目之如何配置動態(tài)數(shù)據(jù)源
JavaWEB項(xiàng)目配置動態(tài)數(shù)據(jù)源
說明
項(xiàng)目中如果需要連接多個(gè)數(shù)據(jù)庫,則需要配置動態(tài)數(shù)據(jù)源,對于使用Spring+MyBatis框架的項(xiàng)目來說配置動態(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ù)源管理工具類,自定義一個(gè)管理類用于設(shè)置以及獲取當(dāng)前數(shù)據(jù)源名稱等操作,該類需要繼承AbstractRoutingDataSource類并實(shí)現(xiàn)其中的抽象方法-->
<bean id="dataSource" class="自定義DbcontrxtHolder類的全線名稱(包名.類名)">
<!-- 設(shè)置默認(rèn)數(shù)據(jù)源 -->
<property name="defaultTargetDataSource" ref="dataSource1"/>
<property name="targetDataSources">
<map>
<!-- 配置數(shù)據(jù)源列表 key為切換數(shù)據(jù)源時(shí)所用的名稱,value-ref為配置datasource的bean的id值 -->
<entry key="dataSource1" value-ref="dataSource1"/>
<entry key="dataSource2" value-ref="dataSource2"/>
</map>
</property>
</bean>
<!--后面配置事務(wù)等其他項(xiàng),這里不再列出-->!
</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í)指定名稱,用于切換數(shù)據(jù)源時(shí)獲取連接對象 其變量值為 targetDataSources鍵值
public static final String DATASOURCE1 = "dataSource1";
public static final String DATASOURCE2 = "dataSource2";
/**
* 設(shè)置當(dāng)前數(shù)據(jù)源
* @param dbType
*/
public static void setDbType(String dbType){
contextHolder.set(dbType);
}
/**
* 獲得當(dāng)前數(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è)務(wù)中切換數(shù)據(jù)源
public void test(){
...業(yè)務(wù)代碼
DbcontextHolder.setDbType(DbcontextHolder.DATASOURCE2);//切換數(shù)據(jù)源2
? ? ?//保存信息當(dāng)前數(shù)據(jù)源
? ? ?if(!saveSendTaskSimple(sendTask)){
?? ??? ??? ?return false;
?? ??? ?}
//清理上下文信息,恢復(fù)到默認(rèn)數(shù)據(jù)源?? ??? ?
DbcontextHolder.clearContext();
...業(yè)務(wù)代碼
}web項(xiàng)目中配置多個(gè)數(shù)據(jù)源
spring + mybatis 多數(shù)據(jù)源配置有兩種解決方案
1、配置多個(gè)不同的數(shù)據(jù)源,使用一個(gè)sessionFactory,在業(yè)務(wù)邏輯使用的時(shí)候自動切換到不同的數(shù)據(jù)源,有一個(gè)種是在攔截器里面根據(jù)不同的業(yè)務(wù)現(xiàn)切換到不同的datasource; 有的會在業(yè)務(wù)層根據(jù)業(yè)務(wù)來自動切換。
2、在spring項(xiàng)目中配置多個(gè)不同的數(shù)據(jù)源datasource,配置多個(gè)sqlSessionFactory,每個(gè)sqlSessionFactory對應(yīng)一個(gè)datasource,在dao層根據(jù)不同業(yè)務(wù)自行選擇使用哪個(gè)數(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)建負(fù)責(zé)切換數(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,且實(shí)現(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> ? ? ? ? ? ? <!-- 默認(rèn)使用mysql --> ? ? ? ? ? ? <property name ="defaultTargetDataSource" ref= "mysql_dataSource"></property > ? ? ? ? ?</bean>
(五)當(dāng)需要使用某個(gè)數(shù)據(jù)庫的時(shí)候
使用下面一行代碼進(jìn)行切換
//切換數(shù)據(jù)庫 DataSourceContextHolder.setDbType(DataSourceType.SOURCE_POSTGS);
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven項(xiàng)目分析剔除無用jar引用的方法步驟
這篇文章主要介紹了Maven項(xiàng)目分析剔除無用jar引用的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Spring boot監(jiān)控Actuator-Admin實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring boot監(jiān)控Actuator-Admin實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
IDEA中properties與yml文件的轉(zhuǎn)變方式
文章介紹了如何在IntelliJ IDEA 2021.1.1中安裝和使用ConvertYAMLandPropertiesFile插件進(jìn)行YAML和Properties文件之間的轉(zhuǎn)換,安裝步驟包括導(dǎo)航到設(shè)置、安裝插件、找到并安裝插件等,插件支持從Properties文件轉(zhuǎn)換為YAML文件,但轉(zhuǎn)換過程中會丟失注釋2024-12-12
Jmeter的接口測試詳細(xì)步驟并實(shí)現(xiàn)業(yè)務(wù)閉環(huán)
這篇文章主要介紹了Jmeter的接口測試詳細(xì)步驟并實(shí)現(xiàn)業(yè)務(wù)閉環(huán),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Spring Cloud多個(gè)微服務(wù)之間調(diào)用代碼實(shí)例
這篇文章主要介紹了Spring Cloud多個(gè)微服務(wù)之間調(diào)用代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

