基于mybatis利用spring aop进行数据源的自动切换与本地测试

项目中有时候需要用的数据源不止一个,这个时候需要对数据源进行切换,一种方法只在xml文件中进行配置,将mybatis对应的配置注入成需要使用的数据源,这种方式的弊端是sqlsession,事务都要配置多次,另外一种方法是用spring的aop特性来完成。

spring-context-db的配置xml



    
        
        
        
        
        
        
        
        
    

    #定义第二个数据源
    
        
        
        
        
        
        
        
        
    

    #注入一个用于控制两个数据源的multipleDataSource
    
        
        
             ###根据类型注入实际使用的数据源
            
                
                
            
        
    

    
        
    

    

    
        
        
            
                classpath*:mapper/*Mapper.xml
            
        
        
    

    
        
        
    

db.properties

#cloudbill mysql database
mysql.url=jdbc:mysql://{ip}:4365/dbname1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
mysql.username=db1Username
mysql.password=db1Password

#cloudbillLog mysql database
seperate.mysql.url=jdbc:mysql://{ip}:4365/dbname2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
seperate.mysql.username=db1Username
seperate.mysql.password=db2Password

#jdbc common properties
jdbc.initialPoolSize=10
jdbc.maxPoolSize=100
jdbc.testConnectionOnCheckout=true
jdbs.preferredTestQuery=SELECT 1

定义多数据源,继承AbstractRoutingDataSource,重写determineCurrentLookupKey

package com.xxx.xxx.xxx.shared;

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

public class MultipleDataSource extends AbstractRoutingDataSource {

    private static final ThreadLocal dataSourceKey = new InheritableThreadLocal() {

        @Override
        protected DataSources initialValue() {
            return DataSources.Global;
        }
    };

    public static void setDataSourceKey(DataSources dataSource) {
        dataSourceKey.set(dataSource);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return dataSourceKey.get();
    }
}

附上AbstractRoutingDataSource的源码

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {

    private Map targetDataSources;

    private Object defaultTargetDataSource;

    
    /**
     * Retrieve the current target DataSource. Determines the
     * {@link #determineCurrentLookupKey() current lookup key}, performs
     * a lookup in the {@link #setTargetDataSources targetDataSources} map,
     * falls back to the specified
     * {@link #setDefaultTargetDataSource default target DataSource} if necessary.
     * @see #determineCurrentLookupKey()
     */
    protected DataSource determineTargetDataSource() {
        Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
        Object lookupKey = determineCurrentLookupKey();
        DataSource dataSource = this.resolvedDataSources.get(lookupKey);
        if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
            dataSource = this.resolvedDefaultDataSource;
        }
        if (dataSource == null) {
            throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
        }
        return dataSource;
    }

    /**
     * Determine the current lookup key. This will typically be
     * implemented to check a thread-bound transaction context.
     * 

Allows for arbitrary keys. The returned key needs * to match the stored lookup key type, as resolved by the * {@link #resolveSpecifiedLookupKey} method. */ protected abstract Object determineCurrentLookupKey(); ...... }

定一个数据源的枚举类

package com.xxx.xxx.xxx.shared;

public enum DataSources {

    Global,Region;
}

定一个切面,在xxxMapper中的任意方法设置连接点,利用@Before和@After来控制数据源的切换。

package com.xxx.xxx.xxx;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.xxx.xxx.xxx.shared.DataSources;
import com.xxx.xxx.xxx.shared.MultipleDataSource;

@Component
@Aspect
public class MultipleDataSourceInterptor {

    @Pointcut("execution(* com.xxx.xxx.xxx.dao.mapper.xxxMapper.*(..))")
    public void aspectPoint() {
    }

    @Before("aspectPoint()")
    public void advice(JoinPoint jp) throws Throwable {
        MultipleDataSource.setDataSourceKey(DataSources.Region);
    }

    @After("aspectPoint()")
    public void adviceAfterReturn(JoinPoint jp) {
        MultipleDataSource.setDataSourceKey(DataSources.Global);
    }
}

最后spring-context的配置文件中还需要加入

xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=“http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd”


开启aop的功能。
以上就可以实现数据源的自动切换了。关于本地测试的方法,利用单元测试没法测,只能在contoller中加接口进行测试了,在接口中使用两个数据源分别对应的DAO,开启debug模式,通过查看数据库的数据和mybatis框架的debug日志,查看数据源的切换情况,也可以在aop中加日志追踪。

你可能感兴趣的:(基于mybatis利用spring aop进行数据源的自动切换与本地测试)