Mybatis的源码学习之Executor

1.概述

Executor接口是Mybatis的执行器,是Mybatis的调度中心,是SQL语句的生成和查询缓存的维护。Executor是通过Configuration的newExecutor函数来生成的。

2.Executor结构

下面介绍一下Executor接口的实现类以及实现类的继承类之间关系。

从上面的Executor的结构图,我们得到了Executor接口有两个实现类分别为:CachingExecutor和BaseExecutor.

CachingExecutor类是一个装饰类。

BaseExecutor类是一个模板类。

这里针对不同的执行器(Executor)类型会创建不同的执行器对象。我们来看一看执行器类型枚举类。

package org.apache.ibatis.session;

/**
*枚举类型
*/
public enum ExecutorType {
  SIMPLE, REUSE, BATCH
}

由上面的执行器类型枚举类,我们知道了Mybatis共有三种执行器。分别的作用如下:

SimpleExecutor -- 最简单的执行器,根据对应的sql直接执行即可,不会做一些额外的操作,拼接完SQL之后,直接交                                             给 StatementHandler  去执行。也是默认的执行器。
ReuseExecutor -- 可重用的执行器,重用的对象是Statement,也就是说该执行器会缓存同一个sql的Statement,省去                                               Statement的重新创建,优化性能。

BatchExecutor -- 通过批量操作来优化性能。通常需要注意的是批量更新操作,由于内部有缓存的实现,使用完成后记得调用                                  flushStatements来清除缓存。

3.Executor接口的主要方法

package org.apache.ibatis.executor;

import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.transaction.Transaction;

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;

  List<BatchResult> flushStatements() throws SQLException;

  //提交事务
  void commit(boolean required) throws SQLException;

  //事务回滚
  void rollback(boolean required) throws SQLException;

  //创建缓存Key对象
  CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);

  //判断缓存中是否存在这个Key的结果
  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();

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值