Mybatis执行器BatchExecutor、ReuseExecutor、SimpleExecutor介绍

一、关系

这里说下Executor接口和他的子类之间的关系

//最顶层的接口
public interface Executor {}
//接着是基础BaseExecutor
public abstract class BaseExecutor implements Executor {}

BaseExecutor有三个实现类BatchExecutor、ReuseExecutor、SimpleExecutor是并列关系。

public class BatchExecutor extends BaseExecutor {}//批量执行器BatchExecutor
public class ReuseExecutor extends BaseExecutor {}//复用执行器ReuseExecutor
public class SimpleExecutor extends BaseExecutor {}//简单执行器SimpleExecutor
Mybatis执行器BatchExecutor、ReuseExecutor、SimpleExecutor介绍_第1张图片

2、CachingExecutor

CachingExecutor是对执行器和缓存的一个封装

public class CachingExecutor implements Executor {
	//Executor的实现类对象,可以是BatchExecutor、ReuseExecutor、SimpleExecutor
    private final Executor delegate;
    private final TransactionalCacheManager tcm = new TransactionalCacheManager();//二级缓存
	
    public CachingExecutor(Executor delegate) {//入参是Executor的实现类对象
        this.delegate = delegate;
    }
}

下面贴一段mybatis创建执行器的源码,更好的说明CachingExecutor

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {

    Executor executor;
    if (ExecutorType.BATCH == executorType) {
        executor = new BatchExecutor(this, transaction);//批量执行器
    } else if (ExecutorType.REUSE == executorType) {
        executor = new ReuseExecutor(this, transaction);//复用执行器
    } else {
        executor = new SimpleExecutor(this, transaction);//默认简单执行器
    }

    //是否开启二级缓存
    if (cacheEnabled) {
    	//封装成带有二级缓存功能的执行器,在实现调用的过程中,如果开启了二级缓存,会先从缓存中获取数据,如果没有在调用具体执行器实现类,CachingExecutor也是Executor的实现类。这是什么设计模式?
        executor = new CachingExecutor(executor);
    }
    //将执行器添加到拦截器链中,在整个调用的过程中使用责任链的方式调用
    executor = (Executor) interceptorChain.pluginAll(executor);
    
    return executor;
}

二、执行流程

下面以查询为例,介绍下在开启了二级缓存时,执行器的执行流程,

1、CachingExecutor的query

@Override
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
    //此时executor是CachingExecutor类型
    return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
    .....
}

@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    ...
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);//基于参数生成CacheKey
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) {
    Cache cache = ms.getCache();
    if (cache != null) {
        //1、这里是处理二级缓存逻辑
        List<E> list = (List<E>) tcm.getObject(cache, key);
        if (list == null) {
            list = delegate.<E>query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
            tcm.putObject(cache, key, list); // issue #578 and #116
        }
        return list;
    }
    
    //2、当缓存不存在,走数据库查询,注意此时的query走的是BaseExecutor
    return delegate.<E>query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

2、BaseExecutor的query

public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    //判断一级缓存是否有数据
    list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
    if (list == null) {
        //没有,走数据查询
        list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
    }
    return list;
}

private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    //注意此时的doQuery是被子类重写了,假设使用默认执行器SimpleExecutor,调用SimpleExecutor的doQuery
    list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    localCache.putObject(key, list);//设置一级缓存内容
    return list;
}

3、SimpleExecutor的doQuery

@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    return handler.<E>query(stmt, resultHandler);//走数据库查询
}

总结:一开始进到CachingExecutor的query方法,在调用BaseExecutor的query方法,在进到SimpleExecutor的doQuery方法查询数据库。

你可能感兴趣的:(数据库框架,mybatis,执行器,BatchExecutor,BaseExecutor,ReuseExecutor,SimpleExecutor)