>舒适提示:本篇是源码分析Mybatis ShardingJdbc SQL语句执行的前置篇。java
sql执行器,其对应的类全路径:org.apache.ibatis.executor.Executor。sql
Executorapache
执行器根据接口,定义update(更新或插入)、query(查询)、commit(提交事务)、rollback(回滚事务)。接下来简单介绍几个重要方法:设计模式
int update(MappedStatement ms, Object parameter) throws SQLException 更新或插入方法,其参数含义以下:缓存
1)MappedStatement ms:SQL映射语句(Mapper.xml文件每个方法对应一个MappedStatement对象)并发
2)Object parameter:参数,一般是List集合。app
< E> List< E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler)源码分析
查询方法,其参数含义以下:学习
1)RowBounds:行边界,主要值分页参数limit、offset。this
2)ResultHandler resultHandler:结果处理器。
CacheKey createCacheKey(MappedStatement ms, Object parameterObj, RowBounds bounds, BoundSql bSql)
建立缓存Key,Mybatis一二级缓存的缓存Key,能够看出Key由上述4个参数来决定。
1)BoundSql boundSql:能够经过该对象获取SQL语句。
CachingExecutor 支持结果缓存的SQL执行器,注意其设计模式的应用,该类中,会持有Executor的一个委托对象,CachingExecutor关注与缓存特定的逻辑,其最终的SQL执行由其委托对象来实现,即其内部的委托对象为BaseExecutor的实现类。
BaseExecutor Executor的基础实现类,该类为抽象类,关于查询、更新具体的实现由其子类来实现,下面4个都是其子类。
SimpleExecutor 简单的Executor执行器。
BatchExecutor 支持批量执行的Executor执行器。
ClosedExecutor 表示一个已关闭的Executor。
ReuseExecutor 支持重复使用Statement,以SQL为键,缓存Statement对象。
在Mybatis中,Executor的建立由Configuration对象来建立,具体的代码以下:
public Executor newExecutor(Transaction transaction) { return newExecutor(transaction, defaultExecutorType); // [@1](https://my.oschina.net/u/1198) } 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](https://my.oschina.net/u/2648711) 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的拆件机制,将在该系列文章中详细介绍。
在学习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引入拆件机制,该部分将在该专题的后续文章中会详细介绍,这里暂时跳过。
参数处理器。一样咱们先来看一下其类图。
这个比较简单,就是处理PreparedStatemet接口的参数化处理,也能够顺便看一下其调用链(该部分会在下一篇中详细介绍)。
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。咱们一样看一下其类图。
处理Jdbc ResultSet的处理器。
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; }
一样支持插件化机制,咱们也稍微再看一下其调用链:
能够看出其调用的入口为SQL执行时。
本文做为下一篇《源码分析Mybatis整合ShardingJdbc SQL执行流程》的前置篇,重点介绍Executor、StatementHandler、ParameterHandler、ResultSetHandler的具体职责,以类图为基础并详细介绍其核心方法的做用,而后详细介绍了这些对象是如何建立,并引出Mybatis拆件机制。
做者介绍:丁威,《RocketMQ技术内幕》做者,RocketMQ 社区布道师,公众号:中间件兴趣圈 维护者,目前已陆续发表源码分析Java集合、Java 并发包(JUC)、Netty、Mycat、Dubbo、RocketMQ、Mybatis等源码专栏。