亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

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

1.主從

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

2.分庫

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

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

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

Mapper分包

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

主從的情況下,同一個(gè)Mapper會(huì)同時(shí)存在讀寫的情況,創(chuàng)建兩個(gè)并不合適,使用同一個(gè)即可。但是這種情況下需要注意,Spring對Mapper自動(dòng)生成的名字是相同的,而且類型也相同,這是就不能直接注入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>

這個(gè)文件,主要是引入了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"/>是只讀的。如果不是從庫,可以按主庫進(jìn)行配置。

在下面代碼中:

<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

這里是針對主從的情況進(jìn)行設(shè)置的,兩個(gè)配置掃描的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);
  }
}

因?yàn)閟qlSession能通過name區(qū)分開,所以這里從sqlSession獲取Mapper。

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

以上是主從的情況。

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

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

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

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

相關(guān)文章

  • SpringBoot中引入MyBatisPlus的常規(guī)操作

    SpringBoot中引入MyBatisPlus的常規(guī)操作

    這篇文章主要介紹了SpringBoot中引入MyBatisPlus的常規(guī)操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Maven項(xiàng)目部署到服務(wù)器設(shè)置訪問路徑以及配置虛擬目錄的方法

    Maven項(xiàng)目部署到服務(wù)器設(shè)置訪問路徑以及配置虛擬目錄的方法

    今天小編就為大家分享一篇關(guān)于Maven項(xiàng)目部署到服務(wù)器設(shè)置訪問路徑以及配置虛擬目錄的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • SpringBoot中各種Controller的寫法

    SpringBoot中各種Controller的寫法

    這篇文章主要介紹了SpringBoot中各種Controller的寫法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Mybatis實(shí)體類屬性與數(shù)據(jù)庫不一致解決方案

    Mybatis實(shí)體類屬性與數(shù)據(jù)庫不一致解決方案

    這篇文章主要介紹了Mybatis實(shí)體類屬性與數(shù)據(jù)庫不一致解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • IDEA多線程文件下載插件開發(fā)的步驟詳解

    IDEA多線程文件下載插件開發(fā)的步驟詳解

    這篇文章主要介紹了IDEA多線程文件下載插件開發(fā)的步驟詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 詳解Java 中的 AutoCloseable 接口

    詳解Java 中的 AutoCloseable 接口

    本文對 try-with-resources 語法進(jìn)行了較為深入的剖析,驗(yàn)證了其為一種語法糖,同時(shí)給出了其實(shí)際的實(shí)現(xiàn)方式的反編譯結(jié)果,相信你在看完本文后,關(guān)于 AutoCloseable 的使用你會(huì)有新的收獲。
    2020-11-11
  • 淺談JDK8中的Duration Period和ChronoUnit

    淺談JDK8中的Duration Period和ChronoUnit

    在JDK8中,引入了三個(gè)非常有用的時(shí)間相關(guān)的API:Duration,Period和ChronoUnit。他們都是用來對時(shí)間進(jìn)行統(tǒng)計(jì)的,本文將會(huì)詳細(xì)講解一下這三個(gè)API的使用
    2021-06-06
  • IDEA啟動(dòng)服務(wù)提示端口被占用,Web?server?failed?to?start.Port?was?already?in?use.

    IDEA啟動(dòng)服務(wù)提示端口被占用,Web?server?failed?to?start.Port?was?al

    這篇文章主要介紹了IDEA啟動(dòng)服務(wù)提示端口被占用,Web?server?failed?to?start.Port?was?already?in?use.,本文給大家分享解決方案,分為linux系統(tǒng)和windows系統(tǒng)解決方案,需要的朋友可以參考下
    2023-07-07
  • mybatis報(bào)Query?was?Empty異常的問題

    mybatis報(bào)Query?was?Empty異常的問題

    這篇文章主要介紹了mybatis報(bào)Query?was?Empty異常的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java多線程場景解析volatile和AtomicLong區(qū)別原理

    Java多線程場景解析volatile和AtomicLong區(qū)別原理

    這篇文章主要為大家介紹了Java中volatile和AtomicLong的區(qū)別原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09

最新評論