读mybatis源码之六:执行器Executor之基类BaseExecutor
2014-06-21 20:32
344 查看
在sqlsession中主要是执行都是通过executor来处理的,具体执行逻辑,大部分在BaseExecutor里面,使用的模板模式,
BoundSql boundSql = ms.getBoundSql(parameter);
CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
BoundSql :sql语句,参数等
CacheKey :缓存key,通过mapperID,sql、参数等产生缓存key
可以看见:
1、先查本地缓存,没有再去查数据库,注意这里的本地缓存是同一个session内的缓存,也就是同一个opensession内。
2、通过configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT来看,可以设置参数,将本地缓存去掉,不使用本地缓存。
本地缓存先占位,后更新。
使用不同的执行器去做查询动作,具体的doQuery由不同的执行去具体实现
二、update
清除本地缓存,做更新操作,具体更新操作在具体执行器中。
各大执行器主要需要写自己的:
一、query:
public <E> List<E> 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);
}
BoundSql :sql语句,参数等
CacheKey :缓存key,通过mapperID,sql、参数等产生缓存key
@SuppressWarnings("unchecked") public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, 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(); } List<E> list; try { queryStack++; list = resultHandler == null ? (List<E>) localCache.getObject(key) : null; if (list != null) { handleLocallyCachedOutputParameters(ms, key, parameter, boundSql); } else { list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql); } } finally { queryStack--; } if (queryStack == 0) { for (DeferredLoad deferredLoad : deferredLoads) { deferredLoad.load(); } deferredLoads.clear(); // issue #601 if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) { clearLocalCache(); // issue #482 } } return list; }
可以看见:
1、先查本地缓存,没有再去查数据库,注意这里的本地缓存是同一个session内的缓存,也就是同一个opensession内。
2、通过configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT来看,可以设置参数,将本地缓存去掉,不使用本地缓存。
private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { List<E> list; localCache.putObject(key, EXECUTION_PLACEHOLDER); try { list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql); } finally { localCache.removeObject(key); } localCache.putObject(key, list); if (ms.getStatementType() == StatementType.CALLABLE) { localOutputParameterCache.putObject(key, parameter); } return list; }
本地缓存先占位,后更新。
使用不同的执行器去做查询动作,具体的doQuery由不同的执行去具体实现
二、update
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; protected abstract List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException; protected abstract <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException;
相关文章推荐
- 读mybatis源码之五:执行器Executor创建
- Mybatis源码分析-BaseExecutor
- myBatis源码之Executor、BaseExecutor和CachingExecutor
- myBatis源码之Executor、BaseExecutor和CachingExecutor
- Elastic-Job项目源码分析4--核心执行器AbstractElasticJobExecutor
- 读mybatis源码之七:执行器Exceutor之四大执行器
- Mybatis执行BaseExecutor(二)
- OSMDroid源码分析之瓦片载入基类:MapTileModuleProviderBase
- Mybatis3.3.x技术内幕(四):五鼠闹东京之执行器Executor设计原本
- Mybatis执行BaseExecutor(二)
- myBatis源码之BatchExecutor
- Mybatis执行BaseExecutor(二)
- myBatis源码之SimpleExecutor
- myBatis源码之BatchExecutor
- mybatis源码之BaseStatementHandler
- MyBATIS原理第三篇: SqlSession下的四大对象之一——执行器(executor)
- Mybatis源码之SimpleExecutor
- mybatis源码之BaseStatementHandler
- Mybatis源码之SimpleExecutor
- myBatis源码之BatchExecutor