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

詳解MyBatis多數(shù)據(jù)源配置(讀寫分離)

 更新時間:2017年01月03日 09:23:04   作者:isea533  
這篇文章主要介紹了詳解MyBatis多數(shù)據(jù)源配置(讀寫分離),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

MyBatis多數(shù)據(jù)源配置(讀寫分離)

首先說明,本文的配置使用的最直接的方式,實際用起來可能會很麻煩。

實際應(yīng)用中可能存在多種結(jié)合的情況,你可以理解本文的含義,不要死板的使用。

多數(shù)據(jù)源的可能情況

1.主從

通常是MySQL一主多從的情況,本文的例子就是主從的情況,但是只有兩個數(shù)據(jù)源,所以采用直接配置不會太麻煩,但是不利于后續(xù)擴展,主要是作為一個例子來說明,實際操作請慎重考慮。

2.分庫

當(dāng)業(yè)務(wù)獨立性強,數(shù)據(jù)量大的時候的,為了提高并發(fā),可能會對表進行分庫,分庫后,每一個數(shù)據(jù)庫都需要配置一個數(shù)據(jù)源。

這種情況可以參考本文,但是需要注意每一個數(shù)據(jù)庫對應(yīng)的Mapper要在不同的包下方便區(qū)分和配置。

另外分庫的情況下也會存在主從的情況,如果你的數(shù)據(jù)庫從庫過多,就參考上面提供的方法,或者尋找其他方式解決。

Mapper分包

分庫的情況下,不同的數(shù)據(jù)庫的Mapper一定放在不同的包下。

主從的情況下,同一個Mapper會同時存在讀寫的情況,創(chuàng)建兩個并不合適,使用同一個即可。但是這種情況下需要注意,Spring對Mapper自動生成的名字是相同的,而且類型也相同,這是就不能直接注入Mapper接口。需要通過SqlSession來解決。

Spring基礎(chǔ)配置

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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">

  <context:component-scan base-package="com.isea533.mybatis.service"/>
  <context:property-placeholder location="classpath:config.properties"/>
  <aop:aspectj-autoproxy/>

  <import resource="spring-datasource-master.xml"/>
  <import resource="spring-datasource-slave.xml"/>
</beans>

這個文件,主要是引入了spring-datasource-master.xml和spring-datasource-slave.xml。

spring-datasource-master.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">

  <bean id="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" 
    init-method="init" destroy-method="close">
    <property name="driverClassName" value="${master.jdbc.driverClass}"/>
    <property name="url" value="${master.jdbc.url}"/>
    <property name="username" value="${master.jdbc.user}"/>
    <property name="password" value="${master.jdbc.password}"/>

    <property name="filters" value="stat"/>

    <property name="maxActive" value="20"/>
    <property name="initialSize" value="1"/>
    <property name="maxWait" value="60000"/>
    <property name="minIdle" value="1"/>

    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <property name="minEvictableIdleTimeMillis" value="300000"/>

    <property name="validationQuery" value="SELECT 'x'"/>
    <property name="testWhileIdle" value="true"/>
    <property name="testOnBorrow" value="false"/>
    <property name="testOnReturn" value="false"/>
  </bean>

  <bean id="sqlSessionFactory1" 
    class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSourceMaster"/>
    <property name="mapperLocations">
      <array>
        <value>classpath:mapper/*.xml</value>
      </array>
    </property>
  </bean>

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.isea533.mybatis.mapper"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>
  </bean>

  <bean id="sqlSessionMaster" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
    <constructor-arg index="0" ref="sqlSessionFactory1"/>
  </bean>

  <aop:config>
    <aop:pointcut id="appService" 
      expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>
    <aop:advisor advice-ref="txAdvice1" pointcut-ref="appService"/>
  </aop:config>

  <tx:advice id="txAdvice1" transaction-manager="transactionManager1">
    <tx:attributes>
      <tx:method name="select*" read-only="true"/>
      <tx:method name="find*" read-only="true"/>
      <tx:method name="get*" read-only="true"/>
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>

  <bean id="transactionManager1" 
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceMaster"/>
  </bean>
</beans>

spring-datasource-slave.xml

和master區(qū)別不大,主要是id名字和數(shù)據(jù)源配置有區(qū)別。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

  <bean id="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource" 
    init-method="init" destroy-method="close">
    <property name="driverClassName" value="${slave.jdbc.driverClass}"/>
    <property name="url" value="${slave.jdbc.url}"/>
    <property name="username" value="${slave.jdbc.user}"/>
    <property name="password" value="${slave.jdbc.password}"/>

    <property name="filters" value="stat"/>

    <property name="maxActive" value="20"/>
    <property name="initialSize" value="1"/>
    <property name="maxWait" value="60000"/>
    <property name="minIdle" value="1"/>

    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <property name="minEvictableIdleTimeMillis" value="300000"/>

    <property name="validationQuery" value="SELECT 'x'"/>
    <property name="testWhileIdle" value="true"/>
    <property name="testOnBorrow" value="false"/>
    <property name="testOnReturn" value="false"/>
  </bean>

  <bean id="sqlSessionFactory2" 
    class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSourceSlave"/>
    <property name="mapperLocations">
      <array>
        <value>classpath:mapper/*.xml</value>
      </array>
    </property>
  </bean>

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.isea533.mybatis.mapper"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
  </bean>

  <bean id="sqlSessionSlave" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
    <constructor-arg index="0" ref="sqlSessionFactory2"/>
  </bean>


  <aop:config>
    <aop:pointcut id="appService" 
      expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>
    <aop:advisor advice-ref="txAdvice2" pointcut-ref="appService"/>
  </aop:config>

  <tx:advice id="txAdvice2" transaction-manager="transactionManager2">
    <tx:attributes>
      <tx:method name="*" read-only="true"/>
    </tx:attributes>
  </tx:advice>

  <bean id="transactionManager2" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceSlave"/>
  </bean>
</beans>

這里需要注意<tx:method name="*" read-only="true"/>是只讀的。如果不是從庫,可以按主庫進行配置。

在下面代碼中:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.isea533.mybatis.mapper"/>
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
</bean>

必須通過sqlSessionFactoryBeanName來指定不同的sqlSessionFactory。

config.properties

# 數(shù)據(jù)庫配置 - Master
master.jdbc.driverClass = com.mysql.jdbc.Driver
master.jdbc.url = jdbc:mysql://192.168.1.11:3306/test
master.jdbc.user = root
master.jdbc.password = jj

# - Slave
slave.jdbc.driverClass = com.mysql.jdbc.Driver
slave.jdbc.url = jdbc:mysql://192.168.1.22:3306/test
slave.jdbc.user = root
slave.jdbc.password = jj

使用Mapper

這里是針對主從的情況進行設(shè)置的,兩個配置掃描的Mapper是一樣的,所以沒法直接注入,需要通過下面的麻煩方式注入。

@Service
public class DemoService {
  private CountryMapper writeMapper;
  private CountryMapper readMapper;

  @Resource(name = "sqlSessionMaster")
  public void setWriteMapper(SqlSession sqlSession) {
    this.writeMapper = sqlSession.getMapper(CountryMapper.class);
  }
  @Resource(name = "sqlSessionSlave")
  public void setReadMapper(SqlSession sqlSession) {
    this.readMapper = sqlSession.getMapper(CountryMapper.class);
  }

  public int save(Country country){
    return writeMapper.insert(country);
  }

  public List<Country> selectPage(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    return readMapper.select(null);
  }
}

因為sqlSession能通過name區(qū)分開,所以這里從sqlSession獲取Mapper。

另外如果需要考慮在同一個事務(wù)中寫讀的時候,需要使用相同的writeMapper,這樣在讀的時候,才能獲取事務(wù)中的最新數(shù)據(jù)。

以上是主從的情況。

在分庫的情況時,由于不同Mapper在不同的包下,所以可以直接使用@Resource或者@Autowired注入Mapper,不需要通過sqlSession獲取。

本篇文章,只是一個多數(shù)據(jù)源的參考,實際應(yīng)用時,請根據(jù)自己的情況進行考慮。

后續(xù),我會利用業(yè)余時間,在本文和上面兩個相關(guān)鏈接的基礎(chǔ)上,針對MySql多數(shù)據(jù)源,嘗試開發(fā)可以自動切換數(shù)據(jù)源的插件,因為我對這方面的實際應(yīng)用不是很熟,所以歡迎大家留言分享自己的解決方案,對這些了解的越多,就越有可能開發(fā)出通用的數(shù)據(jù)源切換插件。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringMVC事件監(jiān)聽ApplicationListener實例解析

    SpringMVC事件監(jiān)聽ApplicationListener實例解析

    這篇文章主要介紹了SpringMVC事件監(jiān)聽ApplicationListener實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • MyBatis配置與CRUD超詳細講解

    MyBatis配置與CRUD超詳細講解

    這篇文章主要介紹了MyBatis配置與CRUD,CRUD是指在做計算處理時的增加(Create)、讀取(Read)、更新(Update)和刪除(Delete)幾個單詞的首字母簡寫。CRUD主要被用在描述軟件系統(tǒng)中數(shù)據(jù)庫或者持久層的基本操作功能
    2023-02-02
  • Spring 源碼解析CommonAnnotationBeanPostProcessor

    Spring 源碼解析CommonAnnotationBeanPostProcessor

    這篇文章主要為大家介紹了Spring 源碼解析CommonAnnotationBeanPostProcessor示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • SpringCloud動態(tài)配置注解@RefreshScope與@Component的深度解析

    SpringCloud動態(tài)配置注解@RefreshScope與@Component的深度解析

    在現(xiàn)代微服務(wù)架構(gòu)中,動態(tài)配置管理是一個關(guān)鍵需求,本文將為大家介紹Spring Cloud中相關(guān)的注解@RefreshScope與@Component的使用,需要的小伙伴可以參考下
    2025-04-04
  • 探究Java中Integer緩沖區(qū)底層原理

    探究Java中Integer緩沖區(qū)底層原理

    本文將會給大家講一講Integer這個包裝類的底層原理。在現(xiàn)在的就業(yè)環(huán)境下,我們需要知其然,還要知其所以然,才能更好地滿足就業(yè)需求,感興趣的小伙伴可以參考閱讀
    2023-05-05
  • Java包裝類之自動裝箱與拆箱

    Java包裝類之自動裝箱與拆箱

    這篇文章主要介紹了Java包裝類之自動裝箱與拆箱,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Java中fail-fast和fail-safe的使用

    Java中fail-fast和fail-safe的使用

    fail-fast和fail-safe是兩種不同的迭代器行為,特別是在遍歷集合時遇到并發(fā)修改的情況,本文主要介紹了Java中fail-fast和fail-safe的使用,感興趣的可以了解一下
    2024-08-08
  • MyBatis的五種批量插入詳解

    MyBatis的五種批量插入詳解

    這篇文章主要介紹了MyBatis的五種批量插入詳解,MyBatis是一款半自動的ORM持久層框架,具有較高的SQL靈活性,支持高級映射(一對一,一對多),動態(tài)SQL,延遲加載和緩存等特性,但它的數(shù)據(jù)庫無關(guān)性較低,需要的朋友可以參考下
    2023-08-08
  • java基礎(chǔ)-數(shù)組擴容詳解

    java基礎(chǔ)-數(shù)組擴容詳解

    這篇文章主要介紹了Java數(shù)組擴容實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-08-08
  • Java深入了解數(shù)據(jù)結(jié)構(gòu)之棧與隊列的詳解

    Java深入了解數(shù)據(jù)結(jié)構(gòu)之棧與隊列的詳解

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)中的棧與隊列,在Java的時候,對于棧與隊列的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時候能夠有扎實的基礎(chǔ)能力。本文小編就來詳細說說Java中的棧與隊列,需要的朋友可以參考一下
    2022-01-01

最新評論

崇文区| 顺义区| 前郭尔| 桃园县| 南京市| 临湘市| 清流县| 蕉岭县| 韶关市| 绥芬河市| 射洪县| 藁城市| 洛南县| 保靖县| 新乡市| 集安市| 葫芦岛市| 台湾省| 罗城| 桐庐县| 彩票| 通山县| 松江区| 吉安县| 砀山县| 九龙县| 腾冲县| 桑日县| 朔州市| 新野县| 蛟河市| 错那县| 涞源县| 化州市| 郁南县| 苍溪县| 永州市| 嘉祥县| 彰化市| 钟山县| 平阴县|