sqlSession.selectOne底层实现原理

上一篇 << 下一篇 >>>Mybatis一级缓存知识汇总


DefaultSqlSession
List list = this.selectList(statement, parameter);

MappedStatement ms = this.configuration.getMappedStatement(statement);
var5 = this.executor.query(ms, this.wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
CachingExecutor
BoundSql boundSql = ms.getBoundSql(parameterObject);
//调用BaseExecutor的方法,多种缓存策略使用同一个key
CacheKey key = this.createCacheKey(ms, parameterObject, rowBounds, boundSql);
key(-25999532:1693644761:com.jgspx.mapper.UserMapper.getUser:0:2147483647:select * from user where id=?:1:development)
return this.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
 Cache cache = MappedStatement.getCache();
    //如果二级缓存存在,则直接返回数据
    return (List)this.TransactionalCacheManager.getObject(cache, key);

    //从以及缓存或数据库中查找
    this.delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
//如果当前没有读的操作,且有刷新的需求,则进行刷新
if (this.queryStack == 0 && ms.isFlushCacheRequired()) {
    this.clearLocalCache();
 }
    ++this.queryStack;[--协助操作缓存清理]
    //判断如果一级缓存存在,则调用一级缓存数据返回
    (List)this.localCache.getObject(key) ;
    return this.handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
 
    //否则查询数据库,并缓存到一级缓存PerpetualCache中
    this.queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
    --this.queryStack;
设置占位符,查询数据库并更新PerpetualCache一级缓存
this.PerpetualCache.putObject(key, ExecutionPlaceholder.EXECUTION_PLACEHOLDER);
list = this.doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
this.PerpetualCache.removeObject(key);
this.PerpetualCache.putObject(key, list);

推荐阅读:
<< << << << << << << << << <<

你可能感兴趣的:(sqlSession.selectOne底层实现原理)