最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring  mybatis多數(shù)據(jù)源實例詳解

 更新時間:2016年07月20日 10:17:00   投稿:lqh  
本文主要介紹sping mybatis多數(shù)據(jù)源處理,在開發(fā)過程中經(jīng)常會遇到多個數(shù)據(jù)庫,這里給大家舉例說明如何處理,希望能幫助有需要的小伙伴

同一個項目有時會涉及到多個數(shù)據(jù)庫,也就是多數(shù)據(jù)源。多數(shù)據(jù)源又可以分為兩種情況:

1)兩個或多個數(shù)據(jù)庫沒有相關(guān)性,各自獨立,其實這種可以作為兩個項目來開發(fā)。比如在游戲開發(fā)中一個數(shù)據(jù)庫是平臺數(shù)據(jù)庫,其它還有平臺下的游戲?qū)?yīng)的數(shù)據(jù)庫;

2)兩個或多個數(shù)據(jù)庫是master-slave的關(guān)系,比如有mysql搭建一個 master-master,其后又帶有多個slave;或者采用MHA搭建的master-slave復(fù)制;

目前我所知道的 Spring 多數(shù)據(jù)源的搭建大概有兩種方式,可以根據(jù)多數(shù)據(jù)源的情況進行選擇。

1. 采用spring配置文件直接配置多個數(shù)據(jù)源

比如針對兩個數(shù)據(jù)庫沒有相關(guān)性的情況,可以采用直接在spring的配置文件中配置多個數(shù)據(jù)源,然后分別進行事務(wù)的配置,如下所示:

<context:component-scan base-package="net.aazj.service,net.aazj.aop" />
<context:component-scan base-package="net.aazj.aop" />
<!-- 引入屬性文件 -->
<context:property-placeholder location="classpath:config/db.properties" />
 
<!-- 配置數(shù)據(jù)源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <property name="url" value="${jdbc_url}" />
  <property name="username" value="${jdbc_username}" />
  <property name="password" value="${jdbc_password}" />
  <!-- 初始化連接大小 -->
  <property name="initialSize" value="0" />
  <!-- 連接池最大使用連接數(shù)量 -->
  <property name="maxActive" value="20" />
  <!-- 連接池最大空閑 -->
  <property name="maxIdle" value="20" />
  <!-- 連接池最小空閑 -->
  <property name="minIdle" value="0" />
  <!-- 獲取連接最大等待時間 -->
  <property name="maxWait" value="60000" />
</bean>
 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <property name="configLocation" value="classpath:config/mybatis-config.xml" />
 <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" />
</bean>
 
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>
 
<!-- 使用annotation定義事務(wù) -->
<tx:annotation-driven transaction-manager="transactionManager" /> 
 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="net.aazj.mapper" />
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
 
<!-- Enables the use of the @AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy/>

第二個數(shù)據(jù)源的配置

<bean name="dataSource_2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <property name="url" value="${jdbc_url_2}" />
  <property name="username" value="${jdbc_username_2}" />
  <property name="password" value="${jdbc_password_2}" />
  <!-- 初始化連接大小 -->
  <property name="initialSize" value="0" />
  <!-- 連接池最大使用連接數(shù)量 -->
  <property name="maxActive" value="20" />
  <!-- 連接池最大空閑 -->
  <property name="maxIdle" value="20" />
  <!-- 連接池最小空閑 -->
  <property name="minIdle" value="0" />
  <!-- 獲取連接最大等待時間 -->
  <property name="maxWait" value="60000" />
</bean>
 
<bean id="sqlSessionFactory_slave" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource_2" />
 <property name="configLocation" value="classpath:config/mybatis-config-2.xml" />
 <property name="mapperLocations" value="classpath*:config/mappers2/**/*.xml" />
</bean>
 
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager_2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource_2" />
</bean>
 
<!-- 使用annotation定義事務(wù) -->
<tx:annotation-driven transaction-manager="transactionManager_2" /> 
 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="net.aazj.mapper2" />
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_2"/>
</bean>

如上所示,我們分別配置了兩個 dataSource,兩個sqlSessionFactory,兩個transactionManager,以及關(guān)鍵的地方在于 MapperScannerConfigurer 的配置——使用sqlSessionFactoryBeanName屬性,注入不同的sqlSessionFactory的名稱,這樣的話,就為不同的數(shù) 據(jù)庫對應(yīng)的 mapper 接口注入了對應(yīng)的 sqlSessionFactory。

需要注意的是,多個數(shù)據(jù)庫的這種配置是不支持分布式事務(wù)的,也就是同一個事務(wù)中,不能操作多個數(shù)據(jù)庫。這種配置方式的優(yōu)點是很簡單,但是卻不靈 活。對于master-slave類型的多數(shù)據(jù)源配置而言不太適應(yīng),master-slave性的多數(shù)據(jù)源的配置,需要特別靈活,需要根據(jù)業(yè)務(wù)的類型進行 細致的配置。比如對于一些耗時特別大的select語句,我們希望放到slave上執(zhí)行,而對于update,delete等操作肯定是只能在 master上執(zhí)行的,另外對于一些實時性要求很高的select語句,我們也可能需要放到master上執(zhí)行——比如一個場景是我去商城購買一件兵器, 購買操作的很定是master,同時購買完成之后,需要重新查詢出我所擁有的兵器和金幣,那么這個查詢可能也需要防止master上執(zhí)行,而不能放在 slave上去執(zhí)行,因為slave上可能存在延時,我們可不希望玩家發(fā)現(xiàn)購買成功之后,在背包中卻找不到兵器的情況出現(xiàn)。

所以對于master-slave類型的多數(shù)據(jù)源的配置,需要根據(jù)業(yè)務(wù)來進行靈活的配置,哪些select可以放到slave上,哪些select不能放到slave上。所以上面的那種所數(shù)據(jù)源的配置就不太適應(yīng)了。

2. 基于 AbstractRoutingDataSource 和 AOP 的多數(shù)據(jù)源的配置

基本原理是,我們自己定義一個DataSource類ThreadLocalRountingDataSource,來繼承 AbstractRoutingDataSource,然后在配置文件中向ThreadLocalRountingDataSource注入 master 和 slave 的數(shù)據(jù)源,然后通過 AOP 來靈活配置,在哪些地方選擇  master 數(shù)據(jù)源,在哪些地方需要選擇 slave數(shù)據(jù)源。下面看代碼實現(xiàn):

1)先定義一個enum來表示不同的數(shù)據(jù)源:

package net.aazj.enums;
 
/**
 * 數(shù)據(jù)源的類別:master/slave
 */
public enum DataSources {
  MASTER, SLAVE
}
 

2)通過 TheadLocal 來保存每個線程選擇哪個數(shù)據(jù)源的標志(key):

package net.aazj.util;
 
import net.aazj.enums.DataSources;
 
public class DataSourceTypeManager {
  private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){
    @Override
    protected DataSources initialValue(){
      return DataSources.MASTER;
    }
  };
   
  public static DataSources get(){
    return dataSourceTypes.get();
  }
   
  public static void set(DataSources dataSourceType){
    dataSourceTypes.set(dataSourceType);
  }
   
  public static void reset(){
    dataSourceTypes.set(DataSources.MASTER0);
  }
}
 

3)定義 ThreadLocalRountingDataSource,繼承AbstractRoutingDataSource:

package net.aazj.util;
 
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 
public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {
  @Override
  protected Object determineCurrentLookupKey() {
    return DataSourceTypeManager.get();
  }
}

4)在配置文件中向 ThreadLocalRountingDataSource 注入 master 和 slave 的數(shù)據(jù)源:

<context:component-scan base-package="net.aazj.service,net.aazj.aop" />
<context:component-scan base-package="net.aazj.aop" />
<!-- 引入屬性文件 -->
<context:property-placeholder location="classpath:config/db.properties" />  
<!-- 配置數(shù)據(jù)源Master -->
<bean name="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <property name="url" value="${jdbc_url}" />
  <property name="username" value="${jdbc_username}" />
  <property name="password" value="${jdbc_password}" />
  <!-- 初始化連接大小 -->
  <property name="initialSize" value="0" />
  <!-- 連接池最大使用連接數(shù)量 -->
  <property name="maxActive" value="20" />
  <!-- 連接池最大空閑 -->
  <property name="maxIdle" value="20" />
  <!-- 連接池最小空閑 -->
  <property name="minIdle" value="0" />
  <!-- 獲取連接最大等待時間 -->
  <property name="maxWait" value="60000" />
</bean>  
<!-- 配置數(shù)據(jù)源Slave -->
<bean name="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <property name="url" value="${jdbc_url_slave}" />
  <property name="username" value="${jdbc_username_slave}" />
  <property name="password" value="${jdbc_password_slave}" />
  <!-- 初始化連接大小 -->
  <property name="initialSize" value="0" />
  <!-- 連接池最大使用連接數(shù)量 -->
  <property name="maxActive" value="20" />
  <!-- 連接池最大空閑 -->
  <property name="maxIdle" value="20" />
  <!-- 連接池最小空閑 -->
  <property name="minIdle" value="0" />
  <!-- 獲取連接最大等待時間 -->
  <property name="maxWait" value="60000" />
</bean>  
<bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource">
  <property name="defaultTargetDataSource" ref="dataSourceMaster" />
  <property name="targetDataSources">
    <map key-type="net.aazj.enums.DataSources">
      <entry key="MASTER" value-ref="dataSourceMaster"/>
      <entry key="SLAVE" value-ref="dataSourceSlave"/>
      <!-- 這里還可以加多個dataSource -->
    </map>
  </property>
</bean>  
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <property name="configLocation" value="classpath:config/mybatis-config.xml" />
 <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" />
</bean>  
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>  
<!-- 使用annotation定義事務(wù) -->
<tx:annotation-driven transaction-manager="transactionManager" /> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="net.aazj.mapper" />
 <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
</bean>
  

 上面spring的配置文件中,我們針對master數(shù)據(jù)庫和slave數(shù)據(jù)庫分別定義了dataSourceMaster和 dataSourceSlave兩個dataSource,然后注入到<bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource"> 中,這樣我們的dataSource就可以來根據(jù) key 的不同來選擇dataSourceMaster和 dataSourceSlave了。

 5)使用Spring AOP 來指定 dataSource 的 key ,從而dataSource會根據(jù)key選擇 dataSourceMaster 和 dataSourceSlave:

package net.aazj.aop;
 
import net.aazj.enums.DataSources;
import net.aazj.util.DataSourceTypeManager;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
@Aspect  // for aop
@Component // for auto scan
public class DataSourceInterceptor {  
  @Pointcut("execution(public * net.aazj.service..*.getUser(..))")
  public void dataSourceSlave(){};
   
  @Before("dataSourceSlave()")
  public void before(JoinPoint jp) {
    DataSourceTypeManager.set(DataSources.SLAVE);
  }
  // ... ...
}

 這里我們定義了一個 Aspect 類,我們使用 @Before 來在符合 @Pointcut("execution(public * net.aazj.service..*.getUser(..))") 中的方法被調(diào)用之前,調(diào)用 DataSourceTypeManager.set(DataSources.SLAVE) 設(shè)置了 key 的類型為 DataSources.SLAVE,所以 dataSource 會根據(jù)key=DataSources.SLAVE 選擇 dataSourceSlave 這個dataSource。所以該方法對于的sql語句會在slave數(shù)據(jù)庫上執(zhí)行。

我們可以不斷的擴充 DataSourceInterceptor  這個 Aspect,在中進行各種各樣的定義,來為某個service的某個方法指定合適的數(shù)據(jù)源對應(yīng)的dataSource。

這樣我們就可以使用 Spring AOP 的強大功能來,十分靈活進行配置了。

 6)AbstractRoutingDataSource原理剖析

ThreadLocalRountingDataSource   繼承了   AbstractRoutingDataSource,    實現(xiàn)其抽象方法 protected abstract Object determineCurrentLookupKey(); 從而實現(xiàn)對不同數(shù)據(jù)源的路由功能。我們從源碼入手分析下其中原理:

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean
AbstractRoutingDataSource 實現(xiàn)了 InitializingBean 那么spring在初始化該bean時,會調(diào)用InitializingBean的接口
void afterPropertiesSet() throws Exception; 我們看下AbstractRoutingDataSource是如何實現(xiàn)這個接口的:
 
  @Override
  public void afterPropertiesSet() {
    if (this.targetDataSources == null) {
      throw new IllegalArgumentException("Property 'targetDataSources' is required");
    }
    this.resolvedDataSources = new HashMap<Object, DataSource>(this.targetDataSources.size());
    for (Map.Entry<Object, Object> entry : this.targetDataSources.entrySet()) {
      Object lookupKey = resolveSpecifiedLookupKey(entry.getKey());
      DataSource dataSource = resolveSpecifiedDataSource(entry.getValue());
      this.resolvedDataSources.put(lookupKey, dataSource);
    }
    if (this.defaultTargetDataSource != null) {
      this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
    }
  }
 

targetDataSources 是我們在xml配置文件中注入的 dataSourceMaster 和 dataSourceSlave. afterPropertiesSet方法就是使用注入的。

dataSourceMaster 和 dataSourceSlave來構(gòu)造一個HashMap——resolvedDataSources。方便后面根據(jù) key 從該map 中取得對應(yīng)的dataSource。

我們在看下 AbstractDataSource 接口中的 Connection getConnection() throws SQLException; 是如何實現(xiàn)的:

@Override
  public Connection getConnection() throws SQLException {
    return determineTargetDataSource().getConnection();
  }


關(guān)鍵在于 determineTargetDataSource(),根據(jù)方法名就可以看出,應(yīng)該此處就決定了使用哪個 dataSource :

protected DataSource determineTargetDataSource() {
  Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
  Object lookupKey = determineCurrentLookupKey();
  DataSource dataSource = this.resolvedDataSources.get(lookupKey);
  if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
    dataSource = this.resolvedDefaultDataSource;
  }
  if (dataSource == null) {
    throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
  }
  return dataSource;
}

 Object lookupKey = determineCurrentLookupKey(); 該方法是我們實現(xiàn)的,在其中獲取ThreadLocal中保存的 key 值。獲得了key之后,在從afterPropertiesSet()中初始化好了的resolvedDataSources這個map中獲得key對應(yīng)的dataSource。而ThreadLocal中保存的 key 值 是通過AOP的方式在調(diào)用service中相關(guān)方法之前設(shè)置好的。OK,到此搞定!

3. 總結(jié)

從本文中我們可以體會到AOP的強大和靈活。

以上就是sping,mybatis 多數(shù)據(jù)源處理的資料整理,希望能幫助有需要的朋友

相關(guān)文章

  • HashSet和TreeSet使用方法的區(qū)別解析

    HashSet和TreeSet使用方法的區(qū)別解析

    這篇文章主要介紹了HashSet和TreeSet使用方法的區(qū)別解析,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 聊聊Java中是什么方法導(dǎo)致的線程阻塞

    聊聊Java中是什么方法導(dǎo)致的線程阻塞

    這篇文章主要介紹了聊聊Java中是什么方法導(dǎo)致的線程阻塞,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Mybatis查詢時,區(qū)分大小寫操作

    Mybatis查詢時,區(qū)分大小寫操作

    這篇文章主要介紹了Mybatis查詢時,區(qū)分大小寫操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Spring用三級緩存處理循環(huán)依賴的方法詳解

    Spring用三級緩存處理循環(huán)依賴的方法詳解

    這篇文章主要介紹了Spring用三級緩存處理循環(huán)依賴的方法,在Spring?框架中,依賴注入是其核心特性之一,它允許對象之間的依賴關(guān)系在運行時動態(tài)注入,然而,當多個Bean之間的依賴關(guān)系形成一個閉環(huán)時,就會出現(xiàn)循環(huán)依賴問題,本文就為解決此問題,需要的朋友可以參考下
    2025-02-02
  • SpringBoot中使用websocket出現(xiàn)404的解決方法

    SpringBoot中使用websocket出現(xiàn)404的解決方法

    在Springboot中使用websocket時,本地開發(fā)環(huán)境可以正常運行,但部署到服務(wù)器環(huán)境出現(xiàn)404問題,所以本文小編講給大家詳細介紹一下SpringBoot中使用websocket出現(xiàn)404的解決方法,需要的朋友可以參考下
    2023-09-09
  • Netty分布式pipeline管道創(chuàng)建方法跟蹤解析

    Netty分布式pipeline管道創(chuàng)建方法跟蹤解析

    這篇文章主要為大家介紹了Netty分布式pipeline管道創(chuàng)建方法跟蹤解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • Java如何實現(xiàn)將類文件打包為jar包

    Java如何實現(xiàn)將類文件打包為jar包

    這篇文章主要介紹了Java如何實現(xiàn)將類文件打包為jar包,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實現(xiàn)

    在二叉樹的結(jié)點上加上線索的二叉樹稱為線索二叉樹,對二叉樹以某種遍歷方式進行遍歷,使其變?yōu)榫€索二叉樹的過程稱為對二叉樹進行線索化。本文將詳解如何實現(xiàn)線索化二叉樹,需要的可以參考一下
    2022-05-05
  • public?static?void?main(String[]?args)使用解讀

    public?static?void?main(String[]?args)使用解讀

    這篇文章主要介紹了public?static?void?main(String[]?args)的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • springboot自定義starter示例代碼

    springboot自定義starter示例代碼

    SpringBoot自定義Starter項目的命名建議遵循xxx-spring-boot-starter格式,以避免與官方或第三方Starter產(chǎn)生沖突,核心規(guī)范包括自動裝配文件的配置,旨在通過spring.factories或spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
    2024-11-11

最新評論

石渠县| 青州市| 右玉县| 嘉祥县| 吉首市| 板桥市| 正定县| 聊城市| 唐河县| 那曲县| 新昌县| 西乌| 大安市| 西乌珠穆沁旗| 筠连县| 乌鲁木齐县| 七台河市| 化德县| 方正县| 高清| 集安市| 沐川县| 新邵县| 临朐县| 张家港市| 莱州市| 隆昌县| 桂阳县| 乾安县| 新绛县| 虞城县| 安溪县| 湘乡市| 九江县| 新密市| 晋江市| 中卫市| 松溪县| 蒙城县| 易门县| 郑州市|