Mybatis一级二级缓存

一 mybatis 一级缓存
**mybatis默认开启的是一级缓存。**第一次执行完毕会将数据库中查询的数据写到缓存(内存),第二次会从缓存中获取数据将不再从数据库查询,从而提高查询效率。当一个sqlSession结束后该sqlSession中的一级缓存也就不存在了。
Mybatis 对缓存提供支持,在没有默认配置的情况下,它只开启一级缓存,一级缓存只是相对于同一个sqlsession而言。所有参数和sql完全一样的情况下,我们使用同一个sqlsession对象调用一个mapper方法,往往只执行一次sql,因为使用SelSession第一次查询后,MyBatis会将其放在缓存中,以后再查询的时候,如果没有声明需要刷新,并且缓存没有超时的情况下,SqlSession都会取出当前缓存的数据,而不会再次发送SQL到数据库。
在这里插入图片描述
二 二级缓存
MyBatis的二级缓存是Application级别的缓存,它可以提高对数据库查询的效率,以提高应用的性能。
MyBatis的缓存机制整体设计以及二级缓存的工作模式
在这里插入图片描述
 SqlSessionFactory层面上的二级缓存默认是不开启的,二级缓存的开席需要进行配置,实现二级缓存的时候,MyBatis要求返回的POJO必须是可序列化的。 也就是要求实现Serializable接口,配置方法很简单,只需要在映射XML文件配置就可以开启缓存了,如果我们配置了二级缓存就意味着:

映射语句文件中的所有select语句将会被缓存。
映射语句文件中的所欲insert、update和delete语句会刷新缓存。
缓存会使用默认的Least Recently Used(LRU,最近最少使用的)算法来收回。
根据时间表,比如No Flush Interval,(CNFI没有刷新间隔),缓存不会以任何时间顺序来刷新。
缓存会存储列表集合或对象(无论查询方法返回什么)的1024个引用
缓存会被视为是read/write(可读/可写)的缓存,意味着对象检索不是共享的,而且可以安全的被调用者修改,不干扰其他调用者或线程所做的潜在修改。

<?xml version="1.0" encoding="UTF-8" ?> 复制代码

三、在 mybatis-config.xml中开启二级缓存
复制代码

<?xml version="1.0" encoding="UTF-8" ?> ..... .... 复制代码

四、测试
复制代码
package com.yihaomen.service.student;

import com.yihaomen.mybatis.dao.StudentMapper;
import com.yihaomen.mybatis.model.Student;
import com.yihaomen.mybatis.model.Teacher;
import com.yihaomen.service.BaseTest;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;

/**
*

  • @ProjectName: springmvc-mybatis
    */
    public class TestStudent extends BaseTest {

    public static void selectAllStudent() {
    SqlSessionFactory sqlSessionFactory = getSession();
    SqlSession session = sqlSessionFactory.openSession();
    StudentMapper mapper = session.getMapper(StudentMapper.class);
    List list = mapper.selectAllStudents();
    System.out.println(list);
    System.out.println(“第二次执行”);
    List list2 = mapper.selectAllStudents();
    System.out.println(list2);
    session.commit();
    System.out.println(“二级缓存观测点”);
    SqlSession session2 = sqlSessionFactory.openSession();
    StudentMapper mapper2 = session2.getMapper(StudentMapper.class);
    List list3 = mapper2.selectAllStudents();
    System.out.println(list3);
    System.out.println(“第二次执行”);
    List list4 = mapper2.selectAllStudents();
    System.out.println(list4);
    session2.commit();

    }

    public static void main(String[] args) {
    selectAllStudent();
    }
    }
    复制代码

结果:

[QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(98) | Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@51e0173d]
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | ==> Preparing: SELECT id, name, age FROM student
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | > Parameters:
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | <
Total: 6
[Student{id=‘1’, name=‘刘德华’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘张惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘谢霆锋’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峰’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]
第二次执行
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.0
[Student{id=‘1’, name=‘刘德华’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘张惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘谢霆锋’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峰’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]
二级缓存观测点
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.3333333333333333
[Student{id=‘1’, name=‘刘德华’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘张惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘谢霆锋’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峰’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]
第二次执行
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.5
[Student{id=‘1’, name=‘刘德华’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘张惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘谢霆锋’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峰’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]

Process finished with exit code 0

我们可以从结果看到,sql只执行了一次,证明我们的二级缓存生效了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章