Mybatis一级缓存源码整理

编程入门 行业动态 更新时间:2024-10-24 22:19:16

Mybatis一级<a href=https://www.elefans.com/category/jswz/34/1771061.html style=缓存源码整理"/>

Mybatis一级缓存源码整理

  1. 使用一级缓存
    @Service
    public class UserService extends ServiceImpl<UserMapper, UserEntity>{@Autowiredprivate UserMapper userMapper ;//1. 这里添加@Transactional 注解@Transactional(rollbackFor = Exception.class)public List<UserEntity> listUser(UserEntity entity){LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>(entity) ;//2. 首次查询List<UserEntity> list = baseMapper.selectList(wrapper);//3. 再次查询, 这一会一级缓存list = baseMapper.selectList(wrapper);return list ;}
    }
    
  2. 关闭一级缓存
    mybatis-plus.configuration.local-cache-scope=statement
    
  3. 一级缓存生效两个条件, 1. 添加事务注解,2. 事务方法重再次同样的查询
缓存原理
  1. 添加@Transactional注解主要是为了保证两次查询使用同一个SqlSession对象
    private class SqlSessionInterceptor implements InvocationHandler {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {SqlSession sqlSession = getSqlSession(SqlSessionTemplate.this.sqlSessionFactory,SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator);try {Object result = method.invoke(sqlSession, args);if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {sqlSessionmit(true);}return result;} finally {if (sqlSession != null) {closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);}}}
    }
    
  2. DefaultSqlSessionFactory#openSessionFromDataSource中新创建Executor: SimpleExecutor 继承 BaseExecutor
    public class DefaultSqlSessionFactory{private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {final Environment environment = configuration.getEnvironment();final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);Transaction tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);final Executor executor = configuration.newExecutor(tx, execType);return new DefaultSqlSession(configuration, executor, autoCommit);}
    }
    
  3. BaseExecutor构造函数初始化一级缓存对象 localCache:PerpetualCache
  4. BaseExecutor#query 方法中业务逻辑
    public class BaseExecutor{public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,CacheKey key, BoundSql boundSql) throws SQLException {// 1. 从localCache缓存中获取数据List<E> list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;if (list == null) {// 2. 如果缓存数据不存在则从数据库查询数据 list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);} // 3. 当local-cache-scope配置为statement时,清空localCache中缓存if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {// issue #482clearLocalCache();}return list;}
    }
    
  5. BaseExecutor#queryFromDatabase 方法中业务逻辑
    public class BaseExecutor{private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds,ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {// 1. 从数据库中查询数据List<E> list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);// 2. 将查询到的数据放入localCache缓存中localCache.putObject(key, list);return list;}
    }
    

更多推荐

Mybatis一级缓存源码整理

本文发布于:2023-11-15 14:14:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1601059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   源码   Mybatis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!