前提:一级缓存与二级缓存,可见:https://www.cnblogs.com/yanze/p/10175017.html

简介:

Executor与SqlSession绑定在一起,每一个SqlSession都拥有一个新的Executor对象,Executor可以认为是SqlSession的核心

Executor类图如下:

Executor

顶层接口,定义基本操作

public interface Executor {

  ResultHandler NO_RESULT_HANDLER = null;
// 更新
int update(MappedStatement ms, Object parameter) throws SQLException;
// 查询,先查缓存,再查数据库
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;
// 查询
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException; <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException; List<BatchResult> flushStatements() throws SQLException;
// 事务提交
void commit(boolean required) throws SQLException;
// 事务回滚
void rollback(boolean required) throws SQLException;
// 创建缓存的键对象
CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);
// 缓存中是否有这个查询的结果
boolean isCached(MappedStatement ms, CacheKey key);
// 清空缓存
void clearLocalCache(); void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType); Transaction getTransaction(); void close(boolean forceRollback); boolean isClosed(); void setExecutorWrapper(Executor executor); }

BaseExecutor

抽像类,实现了Executor接口,实现了执行器的基本功能,在创建时会根据传过来的ExecutorType创建不同的类,

我们可以在配置文件中定义默认类型

<settings>
<!--SIMPLE、REUSE、BATCH-->
<setting name="defaultExecutorType" value="SIMPLE"/>
</settings>

备注:

继承BaseExecutor的Executor默认都支持一级缓存

SimpleExecutor

每执行一次update或select,就开启一个Statement/PrepareStatement对象,用完立刻关闭Statement/PrepareStatement对象。

ReuseExecutor(复用)

每执行一次update或select,以sql作为key查找Statement对象,存在就使用,不存在就创建,用完后,不关闭Statement对象,而是放置于Map<String, Statement>内,供下一次使用。(可以是Statement或PrepareStatement对象)

BatchExecutor(批处理)

执行update时(没有select,JDBC批处理不支持select),将所有sql都添加到批处理中(addBatch()),等待统一执行(executeBatch()),

它缓存了多个Statement对象,每个Statement对象都是addBatch()完毕后,等待逐一执行executeBatch()批处理的;

BatchExecutor相当于维护了多个桶,每个桶里都装了很多属于自己的SQL,就像苹果蓝里装了很多苹果,番茄蓝里装了很多番茄,最后,再统一倒进仓库。(可以是Statement或PrepareStatement对象)
简单来说:攒一波再处理

-------------------------------------------------

CatchingExecutor

查询时先从二级缓存中获取查询结果,存在就返回,不存在,再委托给Executor delegate(委托)去数据库取,delegate可以是上面任一的SimpleExecutor、ReuseExecutor、BatchExecutor

Mybatis的executor的更多相关文章

  1. Mybatis执行Executor(一)

    在DefaultSqlSession中我们可以看到一系列的增删改查操作的其实都是在调用Executor的接口,Mybatis对外统一提供了一个操作接口类Executor,提供的接口方法有update. ...

  2. MyBatis架构(转)

    本文来自http://www.bubuko.com/infodetail-549184.html 如果不太熟悉MyBatis使用的请先参见MyBatis官方文档,这对理解其架构设计和源码分析有很大好处 ...

  3. MyBatis架构设计及源代码分析系列(一):MyBatis架构

    如果不太熟悉MyBatis使用的请先参见MyBatis官方文档,这对理解其架构设计和源码分析有很大好处. 一.概述 MyBatis并不是一个完整的ORM框架,其官方首页是这么介绍自己 The MyBa ...

  4. 《深入理解mybatis原理》 MyBatis的架构设计以及实例分析

    作者博客:http://blog.csdn.net/u010349169/article/category/2309433 MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简 ...

  5. Mybatis执行CachingExecutor(六)

    前面几篇博客我们介绍了Excutor及抽象类BaseExecutor和实现类SimpleExecutor.BatchExecutor和ReuseExecutor: 博客列表: Mybatis执行Exe ...

  6. SSM-MyBatis-06:Mybatis中openSession到底做了什么

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 1.找SqlSesionFactory实现类 Ctrl+H:DefaultSqlSessionFactory: ...

  7. MyBatis的深入原理分析之1-架构设计以及实例分析

    MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个select查询实例, ...

  8. 深入理解mybatis

    MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个select查询实例, ...

  9. mybatis“$”和“#”

    摘要:$ 是直接拼接# 会转义,更安全 类比Mybatis的执行流程和JDBC原有的我们使用的方法就是:Mybatis: Sqlsession -> Executor -> Stateme ...

随机推荐

  1. LeetCode——Lowest Common Ancestor of a Binary Search Tree

    Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given no ...

  2. struts2的s:iterator 标签 详解<转>

    struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo: s:iterator 标签有3个属性:     value:被迭代的集合     id   : ...

  3. Linux学习(四)档案与目录管理

    1. 目录与路径  1.1 相对路径与绝对路径  1.2 目录的相关操作: cd, pwd, mkdir, rmdir  1.3 关于执行文件路径的变量: $PATH2. 档案与目录管理  2.1 档 ...

  4. 索引原理 B tree

    数据库原理之-索引 背景介绍: 用数据库的时候经常有几个疑问: 1:为啥通过加索引就能提升数据的查询料率? 2:为啥加多了索引会导致增删改的效率变低? 3:为啥有的人能用好有的人用不好? 这些问题我们 ...

  5. {sharepoint} More on SharePoint 2010 Application Pools

    More on SharePoint 2010 Application Pools Print | posted on Friday, December 04, 2009 3:26 PM Blimey ...

  6. GOOGLE和百度的长域名

    GOOGLE的变态域名:www.mamashuojiusuannizhucedeyumingzaichanggoogledounengsousuochulai.cn/中文拼音:“妈妈说就算你注册的域名 ...

  7. 【node】---multer模块实现图片上传---【巷子】

    1.安装muterl第三方模块 cnpm install multer --save 2.使用 multer在解析完成后,会向request对象中添加一个body对象和一个file或者files对象( ...

  8. Yii 各种url地址写法

    echo Url::home(); 生成入口地址/yii2test/frontend/web/index.php: echo  Url::base();生成入口文件夹地址:/yii2test/fron ...

  9. Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...

  10. Shape of passed values is (3490, 21), indices imply (3469, 21)

    背景 处理DataFrame数据时,抛了这个错误:Shape of passed values is (3490, 21), indices imply (3469, 21) 解决 数据出现重复,导致 ...