栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis缓存

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis缓存

一级缓存

一级缓存的作用域是Executor,实际就是SqlSession,默认开启并且无法关闭,

查询的时候会先判断SqlSession是否缓存存在,如果存在直接走缓存,如果不存在则走数据库

如下操作默认会走缓存: 

SqlSession sqlSession1 = factory.openSession(true);

SqlSession sqlSession2 = factory.openSession(true);

StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class);

StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class); System.out.println('studentMapper读取数据: ' + studentMapper.getStudentById(1)); System.out.println('studentMapper读取数据: ' + studentMapper.getStudentById(1)); System.out.println('studentMapper2更新了' + studentMapper2.updateStudentName('小岑',1)

跟spring集成的情况下操作:

 

如上操作会发现第二次查询并没有走索引,根据上面的一级缓存作用范围是sqlsession,如果不生效那说明这两次查询并没有使用同一个sqlsession,我们看看代码,发现跟spring集成之后,sqlSession对象使用的是

 我们在往下看看SqlSessionTemplate

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
    PersistenceExceptionTranslator exceptionTranslator) {

  notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
  notNull(executorType, "Property 'executorType' is required");

  this.sqlSessionFactory = sqlSessionFactory;
  this.executorType = executorType;
  this.exceptionTranslator = exceptionTranslator;
  this.sqlSessionProxy = (SqlSession) newProxyInstance(
      SqlSessionFactory.class.getClassLoader(),
      new Class[] { SqlSession.class },
      new SqlSessionInterceptor());
}

SqlSessionTemplate的private final SqlSession sqlSessionProxy;使用了一个代理对象

看看生成代理对象的代码

在使用sqlsessiontemplate做数据库操作,都会走到这个代码方法里,咱们接着看这里的ggetSqlSession 方法 

 除非是当前的会话是有事务的,会复用sqlSession,否则是创建新的sqlSession对象

那上面的问题来看就比较清晰了,如果跟spring集成之后如果当前会话没有事务,同样的查询语句并不会命中一级索引,如果会话是有事务的是可以命中一级缓存的

 

二级缓存

二级缓存的作用域MappedStatement,也就是一个Mapper.xml文件,二级缓存默认是开启的,配置方式为mapperconfig里


    
        
    


 

如果具体的mapper.xml文件需要使用缓存,需要在xml文件里使用了< cache/>、< cacheRef/>标签时创建,并且会按 NameSpace 为维度,为各个 MapperStatement 传入它所属的 Namespace 的二级缓存对象。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826951.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号