詳解利用Spring的AbstractRoutingDataSource解決多數(shù)據(jù)源的問(wèn)題
多數(shù)據(jù)源問(wèn)題很常見(jiàn),例如讀寫(xiě)分離數(shù)據(jù)庫(kù)配置。
原來(lái)的項(xiàng)目出現(xiàn)了新需求,局方要求新增某服務(wù)器用以提供某代碼,涉及到多數(shù)據(jù)源的問(wèn)題。
研究成果如下:
1、首先配置多個(gè)datasource
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url" value="jdbc:jtds:sqlserver://10.82.81.51:1433;databaseName=standards">
</property>
<property name="username" value="youguess"></property>
<property name="password" value="youguess"></property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url" value="jdbc:jtds:sqlserver://10.82.81.52:1433;databaseName=standards">
</property>
<property name="username" value="youguess"></property>
<property name="password" value="youguess"></property>
</bean>
2、寫(xiě)一個(gè)DynamicDataSource類(lèi)繼承AbstractRoutingDataSource,并實(shí)現(xiàn)determineCurrentLookupKey方法
package com.standard.core.util;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return CustomerContextHolder.getCustomerType();
}
}
3、利用ThreadLocal解決線(xiàn)程安全問(wèn)題
package com.standard.core.util;
public class CustomerContextHolder {
public static final String DATA_SOURCE_A = "dataSource";
public static final String DATA_SOURCE_B = "dataSource2";
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
return contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
4、數(shù)據(jù)源配置
<bean id="dynamicDataSource" class="com.standard.core.util.DynamicDataSource" >
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSource" key="dataSource"></entry>
<entry value-ref="dataSource2" key="dataSource2"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource" >
</property>
</bean>
5、在DAOImpl中切換數(shù)據(jù)源
CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_B);
搞定!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring AbstractRoutingDatasource 動(dòng)態(tài)數(shù)據(jù)源的實(shí)例講解
- 淺談利用Spring的AbstractRoutingDataSource解決多數(shù)據(jù)源的問(wèn)題
- Spring(AbstractRoutingDataSource)實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換示例
- 使用Spring的AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源切換示例
- ???????Spring多租戶(hù)數(shù)據(jù)源管理 AbstractRoutingDataSource
相關(guān)文章
關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理
這篇文章主要介紹了關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
解決Springboot項(xiàng)目中很多頁(yè)面出現(xiàn)Whitelabel Error Page(404)的問(wèn)題
最近在接手的前后端項(xiàng)目中發(fā)現(xiàn)其默認(rèn)路徑不是主機(jī)+端口(如:http://localhost:3453/)的形式,很多頁(yè)面的訪(fǎng)問(wèn)是加了一個(gè)層級(jí),只要訪(fǎng)問(wèn)頁(yè)面就會(huì)出現(xiàn)Whitelabel Error Page(404),所以本文給大家提供了解決方案,需要的朋友可以參考下2024-02-02
詳解IntelliJ IDEA 自帶的 HTTP Client 接口調(diào)用插件吊打 Postman
HTTP Client 是 IDEA 自帶的一款簡(jiǎn)潔輕量級(jí)的接口調(diào)用插件,通過(guò)它,我們能在 IDEA 上開(kāi)發(fā),調(diào)試,測(cè)試 RESTful Web 服務(wù),接下來(lái)通過(guò)本文給大家分享IntelliJ IDEA 自帶的 HTTP Client 接口調(diào)用插件吊打 Postman的知識(shí),感興趣的朋友一起看看吧2021-05-05
mybatis.type-aliases-package之巨坑的解決
這篇文章主要介紹了mybatis.type-aliases-package之巨坑的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-09-09
示例解析java面向?qū)ο缶幊谭庋b與訪(fǎng)問(wèn)控制
這篇文章主要為大家介紹了java封裝與訪(fǎng)問(wèn)控制的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
java獲取指定開(kāi)始時(shí)間與結(jié)束時(shí)間之間的所有日期
這篇文章主要為大家詳細(xì)介紹了java獲取指定開(kāi)始時(shí)間與結(jié)束時(shí)間之間的所有日期,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Mybatis Properties 配置優(yōu)先級(jí)詳解
這篇文章主要介紹了Mybatis Properties 配置優(yōu)先級(jí),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

