springmvc 、mybatis整合多个数据源(读写分离)

1、extends AbstractRoutingDataSource
此类注释:
Abstract {@link javax.sql.DataSource} implementation that routes {@link #getConnection()}
calls to one of various target DataSources based on a lookup key. The latter is usually
(but not necessarily) determined through some thread-bound transaction context.
大概意思就是getConnection()根据查找lookup key键对不同目标数据源的调用,通常是通过(但不一定)某些线程绑定的事物上下文来实现。

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    public Object determineCurrentLookupKey() {
        // TODO Auto-generated method stub
         return DataSourceContextHolder.getCustomerType();
    }

}

这里写图片描述

2、 上图2个方法分别是setTargetDataSources(注入目标数据源) 和setDefaultTargetDataSource(注入默认的数据源);
可在spring.xml配置

 <bean id="dynamicDataSource" class="com.xx.xx.DateSource.DynamicDataSource">  
     <property name="targetDataSources">  
         <map key-type="java.lang.String">
             <!-- 指定lookupKey和与之对应的数据源 -->
             <entry key="dataSource" value-ref="dataSource1"></entry>  
             <entry key="dataSource2" value-ref="dataSource2"></entry>  
           </map>  
     </property>  
     <!-- 这里可以指定默认的数据源 -->
     <property name="defaultTargetDataSource" ref="dataSource1" />  
 </bean>  

<!-- 数据源配置 -->
    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" 
        init-method="init" destroy-method="close"> 
        <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
        <property name="driverClassName" value="${jdbc.driver}" />

        <!-- 基本属性 url、user、password -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" 
        init-method="init" destroy-method="close"> 

        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.urll}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dynamicDataSource"/>
        <property name="typeAliasesPackage" value="com.xx.xx"/>
        <property name="typeAliasesSuperType"  value="com.xx.xx"/>
        <property name="mapperLocations" value="classpath:/mappings/*.xml"/>
        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
    </bean>
 <!-- 定义事务 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dynamicDataSource" />
    </bean>

3、

`public class DataSourceContextHolder {

  public static final String DATA_SOURCE_MYSQL = "dataSource1";
  public static final String DATA_SOURCE_MSSQL = "dataSource2";

  //设置当前线程使用dataSource
  private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

  public static void setCustomerType(String customerType) {
        contextHolder.set(customerType);
  }
  public static String getCustomerType() {

      String dataSource = contextHolder.get();

      if (StringUtils.isEmpty(dataSource)) {
          return DATA_SOURCE_MYSQL;
      }else {
          return dataSource;
      }
  }
  public static void clearCustomerType() {
     contextHolder.remove();
  }

}`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值