ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mybatis中的缓存

2020-05-20 21:55:33  阅读:267  来源: 互联网

标签:mapper 缓存 student2 二级缓存 StudentMapper2 sqlSession mybatis


mybatis中的缓存和hibernate的缓存类似,也分为一级缓存和二级缓存;

①一级缓存:为sqlSession级别的缓存。默认开启。使用同一个sqlSession执行查询时,会先从sqlSession的缓存中去获取数据,如果缓存中不存在,再去数据库中查询,并将查询结果缓存到sqlSession。

特点:

1) Mybatis的一级缓存,使用的PerpetualCache类来实现。其内部是通过HashMap来存储缓存数据的。Map的key值,基于namespace:sql:参数;

2) Mybatis一级缓存不存在自动失效,缓存淘汰机制。只有当执行了DML(insert update delete)操作并提交时,才会去清空缓存;

3) sqlSession关闭时,对应的一级缓存失效;

 

@Test
 public void cacheTest(){
      StudentMapper2 mapper = sqlSession.getMapper(StudentMapper2.class);
      Student2 student2 = mapper.selectByIds(2);
      System.out.println(student2);
      //如果执行了DML操作,则会清空缓存
      //student2.setStudentName("张无忌");
      //mapper.update(student2);
      //第二次查询会直接从一级缓存中获取
      Student2 student21 = mapper.selectByIds(2);
      System.out.println(student21);
 }

②二级缓存:为mapper级别的缓存。不同的sqlSession可以共享。需要单独配置,默认不开启。当开启二级缓存之后,数据的查询执行流程是:二级缓存==>一级缓存==>数据库。

如果对mapper进行了DML操作,那么对应Mapper的二级缓存将清空。

配置:

1)在mybatis主配置文件中配置开启二级缓存;

<!--开启二级缓存,默认开启-->
<setting name="cacheEnabled" value="true"/>

2)在需要缓存的mapper中配置缓存(通过cache标签配置);

配置二级缓存
eviction:指定缓存策略;
LRU:最近最少使用策略;(默认规则)
FIFO:先进先出;按进入顺序进行移除
SOFT:软引用;
WEAK:弱引用;
flushInterval:刷新时间,单位为毫秒;
size:指定缓存的最大存储个数;
type:指定缓存实现,当定义了缓存实现方式时,可以通过该属性配置

<cache eviction="LRU" flushInterval="300000" readOnly="false" size="1024"></cache>

测试代码为:

 //测试二级缓存
        @Test
        public void cache2Test(){
            StudentMapper2 mapper = sqlSession.getMapper(StudentMapper2.class);
            Student2 student2 = mapper.selectById(3);
            System.out.println(student2);
            //sqlSession必须commit才会将数据放入二级缓存中
            sqlSession.commit();
            //开启第二个sqlSession
            System.out.println("==========开启第二个sqlSession查询==============");
            SqlSession sqlSession2 = sqlSessionFactory.openSession();
            StudentMapper2 mapper1 = sqlSession2.getMapper(StudentMapper2.class);
            Student2 student21 = mapper1.selectById(3);
            System.out.println(student21);
            sqlSession2.close();

        }

测试结果为:

在这里我们要注意一个问题,如果我们在写接口的查询时用的是mybatis注解代替XML文件中的标签是,我们就不再需要在mapper中配置缓存,而是直接在此接口中配置缓存;

例如:

 

标签:mapper,缓存,student2,二级缓存,StudentMapper2,sqlSession,mybatis
来源: https://www.cnblogs.com/xie-qi/p/12926530.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有