JavaWEB項(xiàng)目之如何配置動(dòng)態(tài)數(shù)據(jù)源
JavaWEB項(xiàng)目配置動(dòng)態(tài)數(shù)據(jù)源
說明
項(xiàng)目中如果需要連接多個(gè)數(shù)據(jù)庫,則需要配置動(dòng)態(tài)數(shù)據(jù)源,對于使用Spring+MyBatis框架的項(xiàng)目來說配置動(dòng)態(tài)數(shù)據(jù)源一般需要在SqlMapConfig.xml中配置
接下來是配置步驟:
步驟
在配置JDBC連接的文件中配置動(dòng)態(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>();
//添加動(dòng)態(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í)候自動(dòng)切換到不同的數(shù)據(jù)源,有一個(gè)種是在攔截器里面根據(jù)不同的業(yè)務(wù)現(xiàn)切換到不同的datasource; 有的會(huì)在業(yè)務(wù)層根據(jù)業(yè)務(wù)來自動(dòng)切換。
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(); ?
? ? } ?
}(三) 建立動(dòng)態(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> ?? ?<!-- 配置動(dòng)態(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)文章
在idea中g(shù)it實(shí)現(xiàn)里查看歷史代碼方式
這篇文章主要介紹了在idea中g(shù)it里查看歷史代碼的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Java線程方法之從線程角色到實(shí)戰(zhàn)避坑指南
Java語言支持多線程編程,它允許在同一個(gè)程序中同時(shí)運(yùn)行多個(gè)線程來執(zhí)行不同的任務(wù),這有助于提高程序的執(zhí)行效率和響應(yīng)速度,這篇文章主要介紹了Java線程方法之從線程角色到實(shí)戰(zhàn)避坑指南的相關(guān)資料,需要的朋友可以參考下2025-12-12
Java synchronized 鎖的 8 個(gè)經(jīng)典問題小結(jié)
本文通過8個(gè)實(shí)驗(yàn)探討Java中synchronized關(guān)鍵字的不同鎖機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-02-02
MyBatis的mapper.xml文件熱加載功能實(shí)現(xiàn)方案
文章分析了Arthas無法直接熱替換MyBatis的mapper.xml文件的限制,并提出了多種變通方法來實(shí)現(xiàn)類似效果,這些方法包括結(jié)合MyBatis的熱加載功能、使用Arthas和類重加載、動(dòng)態(tài)刷新MappedStatement等,文章還建議在不同環(huán)境中使用不同的解決方案,需要的朋友可以參考下2026-02-02
IntelliJ IDEA下SpringBoot如何指定某一個(gè)配置文件啟動(dòng)項(xiàng)目
這篇文章主要介紹了IntelliJ IDEA下SpringBoot如何指定某一個(gè)配置文件啟動(dòng)項(xiàng)目問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解
這篇文章主要介紹了SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Java正則表達(dá)式之替換匹配文本和查找所有匹配項(xiàng)方式
本文將深入淺出地介紹兩個(gè)常見操作:替換匹配文本和查找所有匹配項(xiàng),通過實(shí)際示例和最佳實(shí)踐,讀者可以輕松掌握這些技術(shù),適用于從簡單文本處理到復(fù)雜日志分析的各種場景2025-05-05
如何在Java?Web項(xiàng)目中優(yōu)雅地實(shí)現(xiàn)驗(yàn)證碼攔截與校驗(yàn)
驗(yàn)證碼代碼用于防止機(jī)器人提交表單或訪問受限頁面,它是一個(gè)圖像或文本序列,需要輸入才能驗(yàn)證身份,這篇文章主要介紹了如何在Java?Web項(xiàng)目中優(yōu)雅地實(shí)現(xiàn)驗(yàn)證碼攔截與校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2025-09-09

