sqlSession.selectOne底层实现原理

作者: 迦叶_金色的人生_荣耀而又辉煌 | 来源:发表于2021-02-07 14:05 被阅读0次

上一篇 <<<userMapper.getUser(1);底层实现原理
下一篇 >>>Mybatis一级缓存知识汇总


DefaultSqlSession
List<T> 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);

推荐阅读:
<<<Mybatis的整体执行原理图解
<<<SqlSessionFactory的创建过程原理
<<<SqlSession的创建过程
<<<sqlSession如何获得具体的Mapper接口信息
<<<userMapper.getUser(1);底层实现原理
<<<Mybatis一级缓存知识汇总
<<<Mybatis二级缓存知识汇总
<<<Springboot整合Mybatis二级缓存
<<<Mybatis常见面试题

相关文章

网友评论

    本文标题:sqlSession.selectOne底层实现原理

    本文链接:https://www.haomeiwen.com/subject/dwqktltx.html