springmvc+mybatis双数据源配置三步走战略

使用Spring提供的AbstractRoutingDataSource类来根据请求路由到不同的数据源。具体做法是先设置两个不同的dataSource代表不同的数据源,再建一个总的dynamicDataSource,根据不同的请求去设置dynamicDataSource。代码如下:

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:property-placeholder location="classpath*:db.properties"/>
    <bean id="dataSourceOne" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

    <bean id="dataSourceTwo" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url2}"></property>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    
    <bean id="dynamicDataSource" class="com.rsb.util.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry value-ref="dataSourceOne" key="dataSourceOne"></entry>
                 <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>
            </map>
        </property>
        <property name="defaultTargetDataSource" ref="dataSourceOne"></property>
    </bean>

     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
         <property name="dataSource" ref="dynamicDataSource"/> 
         <property name="configLocation" value="classpath:mybatis-config.xml" />
      </bean>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dynamicDataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="txManager" />
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config/>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.rsb.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>

创建DataSourceContextHolder.java

package com.rsb.util;

import com.mysql.jdbc.StringUtils;

public class DataSourceContextHolder {

    public static final String DATA_SOURCE_MYSQL = "dataSourceOne";
    public static final String DATA_SOURCE_MSSQL = "dataSourceTwo";
    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.isNullOrEmpty(dataSource)) {
            return DATA_SOURCE_MYSQL;
        }else {
            return dataSource;
        }
    }
    
    public static void clearCustomerType(){
        contextHolder.remove();
    }
}

创建DynamicDataSource.java

package com.rsb.util;

import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource{

    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getCustomerType();
    }

    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

}
配置完成,用法

ModelBizImpl.java

DataSourceContextHolder.setCustomerType(DataSourceContextHolder.DATA_SOURCE_MSSQ);

猜你喜欢

转载自blog.csdn.net/qq_35298784/article/details/81504830