From 13e4cd30e4b28729503dd472824cc38701f4e538 Mon Sep 17 00:00:00 2001 From: quanhengf Date: Wed, 18 Dec 2019 20:49:36 +0800 Subject: [PATCH] =?UTF-8?q?SimpleExecutor=E6=BA=90=E7=A0=81=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../核心处理层/5、Executor组件.md | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/docs/Mybatis/核心处理层/5、Executor组件.md b/docs/Mybatis/核心处理层/5、Executor组件.md index ed9c9c9..99611c7 100644 --- a/docs/Mybatis/核心处理层/5、Executor组件.md +++ b/docs/Mybatis/核心处理层/5、Executor组件.md @@ -276,8 +276,76 @@ public abstract class BaseExecutor implements Executor { } ``` ## 2 SimpleExecutor +SimpleExecutor继承了BaseExecutor抽象类,它是最简单的Executor接口实现。Executor组件使用了模板方法模式,一级缓存等固定不变的操作都封装到了BaseExecutor中,在SimpleExecutor中就不必再关心一级缓存等操作,只需要专注实现4 个基本方法的实现即可。 +```java +public class SimpleExecutor extends BaseExecutor { + + public SimpleExecutor(Configuration configuration, Transaction transaction) { + super(configuration, transaction); + } + + @Override + public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { + Statement stmt = null; + try { + // 获取配置对象 + Configuration configuration = ms.getConfiguration(); + // 创建StatementHandler对象 + StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); + // 完成Statement的创建和初始化,该方法首先会调用StatementHandler的prepare()方法 + // 创建Statement对象,然后调用StatementHandler的parameterize()方法处理占位符 + stmt = prepareStatement(handler, ms.getStatementLog()); + // 调用StatementHandler的query()方法,执行sql语句,并通过ResultSetHandler + // 完成结果集的映射 + return handler.query(stmt, resultHandler); + } finally { + // 关闭Statement对象 + closeStatement(stmt); + } + } + + private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { + Statement stmt; + Connection connection = getConnection(statementLog); + // 创建Statement对象 + stmt = handler.prepare(connection, transaction.getTimeout()); + // 处理占位符 + handler.parameterize(stmt); + return stmt; + } + + /** + * 与前面doQuery()方法的实现非常类似 + */ + @Override + public int doUpdate(MappedStatement ms, Object parameter) throws SQLException { + Statement stmt = null; + try { + Configuration configuration = ms.getConfiguration(); + StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null); + stmt = prepareStatement(handler, ms.getStatementLog()); + return handler.update(stmt); + } finally { + closeStatement(stmt); + } + } + + @Override + protected Cursor doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException { + Configuration configuration = ms.getConfiguration(); + StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql); + Statement stmt = prepareStatement(handler, ms.getStatementLog()); + return handler.queryCursor(stmt); + } + @Override + public List doFlushStatements(boolean isRollback) throws SQLException { + // SimpleExecutor不提供sql语句批处理,所以直接返回空集合 + return Collections.emptyList(); + } +} +``` ## 3 ReuseExecutor