Executor是MyBatis执行器的核心接口:

发布时间:2026/7/1 1:34:14

Executor是MyBatis执行器的核心接口: public interface Executor { // 查询操作 E ListE query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException; E ListE query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException; E CursorE queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException; // 更新操作 int update(MappedStatement ms, Object parameter) 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); // 批量操作 ListBatchResult flushStatements() throws SQLException; // 资源管理 Transaction getTransaction(); void close(boolean forceRollback); boolean isClosed(); }6.2 BaseExecutor抽象基类BaseExecutor是Executor的抽象基类实现了模板方法模式public abstract class BaseExecutor implements Executor { protected Transaction transaction; protected Executor wrapper; protected PerpetualCache localCache; protected PerpetualCache localOutputParameterCache; protected Configuration configuration; protected int queryStack; private boolean closed; // 模板方法查询操作 Override public E ListE query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { BoundSql boundSql ms.getBoundSql(parameter); CacheKey key createCacheKey(ms, parameter, rowBounds, boundSql); return query(ms, parameter, rowBounds, resultHandler, key, boundSql); } Override public E ListE query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException { ErrorContext.instance().resource(ms.getResource()).activity(executing a query).object(ms.getId()); if (closed) { throw new ExecutorException(Executor was closed.); } if (queryStack 0 ms.isFlushCacheRequired()) { clearLocalCache(); } ListE list; try { queryStack; list resultHandler null ? (ListE) localCache.getObject(cacheKey) : null; if (list ! null) { handleLocallyCachedOutputParameters(ms, cacheKey, parameter, boundSql); } else { list queryFromDatabase(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql); } } finally { queryStack--; } if (queryStack 0) { for (DeferredLoad deferredLoad : deferredLoads) { deferredLoad.load(); } deferredLoads.clear(); if (configuration.getLocalCacheScope() LocalCacheScope.STATEMENT) { clearLocalCache(); } } return list; } // 抽象方法子类实现具体的数据库查询 protected abstract E ListE doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException; // 模板方法更新操作 Override public int update(MappedStatement ms, Object parameter) throws SQLException { ErrorContext.instance().resource(ms.getResource()).activity(executing an update).object(ms.getId()); if (closed) { throw new ExecutorException(Executor was closed.); } clearLocalCache(); return doUpdate(ms, parameter); } // 抽象方法子类实现具体的数据库更新 protected abstract int doUpdate(MappedStatement ms, Object parameter) throws SQLException; }6.3 具体执行器实现6.3.1 SimpleExecutorpublic class SimpleExecutor extends BaseExecutor { public SimpleExecutor(Configuration configuration, Transaction transaction) { super(configuration, transaction); } Override public E ListE doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { Statement stmt null; try { Configuration configuration ms.getConfiguration(); StatementHandler handler configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); stmt prepareStatement(handler, ms.getStatementLog()); return handler.query(stmt, resultHandler); } finally { closeStatement(stmt); } } 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); } } private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Statement stmt; Connection connection getConnection(statementLog); stmt handler.prepare(connection, transaction.getTimeout()); handler.parameterize(stmt); return stmt; } }特点简单实现每次执行都创建新的Statement资源管理及时关闭Statement和Connection性能考虑适合单次执行场景6.3.2 ReuseExecutorpublic class ReuseExecutor extends BaseExecutor { private final MapString, Statement statementMap new HashMap(); Override public E ListE doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { Configuration configuration ms.getConfiguration(); StatementHandler handler configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); Statement stmt prepareStatement(handler, ms.getStatementLog()); return handler.query(stmt, resultHandler); } private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Statement stmt; BoundSql boundSql handler.getBoundSql(); String sql boundSql.getSql(); if (hasStatementFor(sql)) { stmt getStatement(sql); applyTransactionTimeout(stmt); } else { Connection connection getConnection(statementLog); stmt handler.prepare(connection, transaction.getTimeout()); putStatement(sql, stmt); } handler.parameterize(stmt); return stmt; } private boolean hasStatementFor(String sql) { return statementMap.containsKey(sql); } private Statement getStatement(String sql) { return statementMap.get(sql); } private void putStatement(String sql, Statement stmt) { statementMap.put(sql, stmt); } }特点Statement重用相同SQL重用Statement对象性能优化减少Statement创建开销内存管理需要管理Statement缓存6.3.3 BatchExecutor

相关新闻