当前位置: 移动技术网 > IT编程>开发语言>Java > 详解MyBatis多数据源配置(读写分离)

详解MyBatis多数据源配置(读写分离)

2019年07月22日  | 移动技术网IT编程  | 我要评论

mybatis多数据源配置(读写分离)

首先说明,本文的配置使用的最直接的方式,实际用起来可能会很麻烦。

实际应用中可能存在多种结合的情况,你可以理解本文的含义,不要死板的使用。

多数据源的可能情况

1.主从

通常是mysql一主多从的情况,本文的例子就是主从的情况,但是只有两个数据源,所以采用直接配置不会太麻烦,但是不利于后续扩展,主要是作为一个例子来说明,实际操作请慎重考虑。

2.分库

当业务独立性强,数据量大的时候的,为了提高并发,可能会对表进行分库,分库后,每一个数据库都需要配置一个数据源。

这种情况可以参考本文,但是需要注意每一个数据库对应的mapper要在不同的包下方便区分和配置。

另外分库的情况下也会存在主从的情况,如果你的数据库从库过多,就参考上面提供的方法,或者寻找其他方式解决。

mapper分包

分库的情况下,不同的数据库的mapper一定放在不同的包下。

主从的情况下,同一个mapper会同时存在读写的情况,创建两个并不合适,使用同一个即可。但是这种情况下需要注意,spring对mapper自动生成的名字是相同的,而且类型也相同,这是就不能直接注入mapper接口。需要通过sqlsession来解决。

spring基础配置

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区别不大,主要是id名字和数据源配置有区别。

<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

# 数据库配置 - 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

这里是针对主从的情况进行设置的,两个配置扫描的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区分开,所以这里从sqlsession获取mapper。

另外如果需要考虑在同一个事务中写读的时候,需要使用相同的writemapper,这样在读的时候,才能获取事务中的最新数据。

以上是主从的情况。

在分库的情况时,由于不同mapper在不同的包下,所以可以直接使用@resource或者@autowired注入mapper,不需要通过sqlsession获取。

本篇文章,只是一个多数据源的参考,实际应用时,请根据自己的情况进行考虑。

后续,我会利用业余时间,在本文和上面两个相关链接的基础上,针对mysql多数据源,尝试开发可以自动切换数据源的插件,因为我对这方面的实际应用不是很熟,所以欢迎大家留言分享自己的解决方案,对这些了解的越多,就越有可能开发出通用的数据源切换插件。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网