zoukankan      html  css  js  c++  java
  • 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中配置缓存,而是直接在此接口中配置缓存;

    例如:

  • 相关阅读:
    SQL WHERE 子句:语法及案例剖析
    SQL SELECT DISTINCT 语句:语法及案例剖析
    SQL SELECT 语句:语法与案例剖析
    SQL 语法:一些最重要的 SQL 命令简介
    SQL 简介:什么是SQL,SQL能做什么
    mybatis 注解开发
    mybatis 缓存
    mybatis 延迟加载策略
    mybatis的多表查询
    mybatis 的动态 SQL 语句
  • 原文地址:https://www.cnblogs.com/xie-qi/p/12926530.html
Copyright © 2011-2022 走看看