• Mybatis执行器BatchExecutor、ReuseExecutor、SimpleExecutor介绍


    一、关系

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

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

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

    public class BatchExecutor extends BaseExecutor {}//批量执行器BatchExecutor
    public class ReuseExecutor extends BaseExecutor {}//复用执行器ReuseExecutor
    public class SimpleExecutor extends BaseExecutor {}//简单执行器SimpleExecutor
    
    • 1
    • 2
    • 3

    2、CachingExecutor

    CachingExecutor是对执行器和缓存的一个封装,Caching [ko fin]

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    下面贴一段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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    二、执行流程

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

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    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);//走数据库查询
    }
    
    • 1
    • 2
    • 3
    • 4

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

  • 相关阅读:
    宠物医院信息展示预约小程序的效果如何
    ES6 - 剩余参数,Array的扩展方法,String的扩展方法
    【python基础4】
    C++ PCL点云曲率分割颜色标识
    GitHub上标星23K+的Redis进阶笔记(应用+原理+集群+拓展+源码)
    [springboot专栏]RedisLockRegistry实现分布式锁
    操作系统实验——进程与线程
    基于昇腾AI | Yolov7模型迁移到昇腾平台EA500I边缘计算盒子的实操指南
    Linux——进程间通信大汇总!(概述、管道通信、消息队列、共享内存、信号、信号量)
    JVM HotSpot 之 内存结构演进过程
  • 原文地址:https://blog.csdn.net/weixin_37862824/article/details/133900034