sql执行器,其对应的类全路径:org.apache.ibatis.executor.Executor。java
Executor 执行器根据接口,定义update(更新或插入)、query(查询)、commit(提交事务)、rollback(回滚事务)。接下来简单介绍几个重要方法:sql
CachingExecutor 支持结果缓存的SQL执行器,注意其设计模式的应用,该类中,会持有Executor的一个委托对象,CachingExecutor关注与缓存特定的逻辑,其最终的SQL执行由其委托对象来实现,即其内部的委托对象为BaseExecutor的实现类。apache
BaseExecutor Executor的基础实现类,该类为抽象类,关于查询、更新具体的实现由其子类来实现,下面4个都是其子类。设计模式
SimpleExecutor 简单的Executor执行器。缓存
BatchExecutor 支持批量执行的Executor执行器。并发
ClosedExecutor 表示一个已关闭的Executor。app
ReuseExecutor 支持重复使用Statement,以SQL为键,缓存Statement对象。源码分析
在Mybatis中,Executor的建立由Configuration对象来建立,具体的代码以下:学习
public Executor newExecutor(Transaction transaction) {
return newExecutor(transaction, defaultExecutorType); // @1
}
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
if (ExecutorType.BATCH == executorType) { // @2
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) { // @3
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor); // @4
return executor;
}
复制代码
从上面的代码能够看出,Executor的建立由以下三个关键点: 代码@1:默认的ExecutorType为ExecutorType.SIMPLE,即默认建立的Executory为SimpleExecutor。 代码@2:根据executorType的值建立对应的Executory。 代码@3:若是cacheEnabled为true,则建立CachingExecutory,而后在其内部持有上面建立的Executor,cacheEnabled默认为true,则默认建立的Executor为CachingExecutor,而且其内部包裹着SimpleExecutor。 代码@4:使用InterceptorChain.pluginAll为executor建立代理对象,即Mybatis的拆件机制,将在该系列文章中详细介绍。this
在学习StatementHandler以前,咱们先来回顾一下JDBC相关的知识。JDBC与语句执行的两大主流对象:java.sql.Statement、java.sql.PrepareStatement对象你们应该不会陌生,该对象的execute方法就是执行SQL语句的入口,经过java.sql.Connection对象建立Statement对象。Mybatis的StatementHandler,是Mybatis建立Statement对象的处理器,即StatementHandler会接管Statement对象的建立。
StatementHandler 根接口,咱们重点关注一下其定义的方法:
BaseStatementHandler StatementHandler的抽象实现类,SimpleStatementHandler、PrepareStatementHandler、CallableStatementHandler是其子类。 咱们来一一看一下其示例变量:
SimpleStatementHandler 具体的StatementHandler实现器,java.sql.Statement对象建立处理器。
PrepareStatementHandler java.sql.PrepareStatement对象的建立处理器。
CallableStatementHandler java.sql.CallableStatement对象的建立处理器,可用来执行存储过程调用的Statement。
RoutingStatementHandler StatementHandler路由器,咱们看一下其构造方法后,就会对该类了然于胸。
public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
switch (ms.getStatementType()) { // @1
case STATEMENT:
delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
case PREPARED:
delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
case CALLABLE:
delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
default:
throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
}
}
复制代码
原来是会根据MappedStatement对象的statementType建立对应的StatementHandler。
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql); // @1
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler); // @2
return statementHandler;
}
复制代码
该方法的两个关键点以下: 代码@1:建立RoutingStatementHandler对象,在其内部再根据SQL语句的类型,建立对应的StatementHandler对象。 代码@2:对StatementHandler引入拆件机制,该部分将在该专题的后续文章中会详细介绍,这里暂时跳过。
参数处理器。一样咱们先来看一下其类图。
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler); // @1
return parameterHandler;
}
复制代码
一样该接口也支持插件化机制。
处理结果的Handler。咱们一样看一下其类图。
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
return resultSetHandler;
}
复制代码
一样支持插件化机制,咱们也稍微再看一下其调用链:
本文做为下一篇《源码分析Mybatis整合ShardingJdbc SQL执行流程》的前置篇,重点介绍Executor、StatementHandler、ParameterHandler、ResultSetHandler的具体职责,以类图为基础并详细介绍其核心方法的做用,而后详细介绍了这些对象是如何建立,并引出Mybatis拆件机制。
做者介绍:《RocketMQ技术内幕》做者,维护公众号:中间件兴趣圈,目前主要发表了源码阅读java集合、JUC(java并发包)、Netty、ElasticJob、Mycat、Dubbo、RocketMQ、mybaits等系列源码。