前面二看到了 sqlSession最终是找到MapperStatement然后委托给Executer执行的 Executer到底做了什么
public interface Executor { ResultHandler NO_RESULT_HANDLER = null; int update(MappedStatement var1, Object var2) throws SQLException; <E> List<E> query(MappedStatement var1, Object var2, RowBounds var3, ResultHandler var4, CacheKey var5, BoundSql var6) throws SQLException; <E> List<E> query(MappedStatement var1, Object var2, RowBounds var3, ResultHandler var4) throws SQLException; <E> Cursor<E> queryCursor(MappedStatement var1, Object var2, RowBounds var3) throws SQLException; List<BatchResult> flushStatements() throws SQLException; void commit(boolean var1) throws SQLException; void rollback(boolean var1) throws SQLException; CacheKey createCacheKey(MappedStatement var1, Object var2, RowBounds var3, BoundSql var4); boolean isCached(MappedStatement var1, CacheKey var2); void clearLocalCache(); void deferLoad(MappedStatement var1, MetaObject var2, String var3, CacheKey var4, Class<?> var5); Transaction getTransaction(); void close(boolean var1); boolean isClosed(); void setExecutorWrapper(Executor var1); }
org.apache.ibatis.executor.loader.ResultLoaderMap 中的一个内部静态类
private static final class ClosedExecutor extends BaseExecutor { public ClosedExecutor() { super((Configuration)null, (Transaction)null); } public boolean isClosed() { return true; } protected int doUpdate(MappedStatement ms, Object parameter) throws SQLException { throw new UnsupportedOperationException("Not supported."); } protected List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException { throw new UnsupportedOperationException("Not supported."); } protected <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { throw new UnsupportedOperationException("Not supported."); } protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException { throw new UnsupportedOperationException("Not supported."); } }
没有具体实现 我们不会用到
每执行一次update或select,就开启一个Statement对象,用完立刻关闭Statement对象。(可以是Statement或PrepareStatement对象)
public class SimpleExecutor extends BaseExecutor { public int doUpdate(MappedStatement ms, Object parameter) throws SQLException { Statement stmt = null; int var6; try { //获得configuration Configuration configuration = ms.getConfiguration(); //根据configuration获得对应的StatementHandler 后面讲 StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, (ResultHandler) null, (BoundSql) null); stmt = this.prepareStatement(handler, ms.getStatementLog()); var6 = handler.update(stmt); } finally { //释放Statement this.closeStatement(stmt); } return var6; } private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Connection connection = this.getConnection(statementLog); Statement stmt = handler.prepare(connection, this.transaction.getTimeout()); handler.parameterize(stmt); return stmt; } }
可以看到finally会关闭Statement
原文:https://www.cnblogs.com/LQBlog/p/10679265.html