此博客是本人从学生时代开始做笔记所用, 部分是工作所遇问题,做填坑笔记,部分闲来查阅资料,加上自己的理解所总结的学习笔记, 常忙得不可开交,若漏了资料来源,望通知~ 前路漫漫,写点东西告诉自己正在一点点进步,而不要迷失于繁忙。

Mybatis的executor

前提:一级缓存与二级缓存,可见: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);

}
View Code

 

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

posted @ 2019-04-01 14:27  炎泽  阅读(350)  评论(0编辑  收藏  举报