mybatis-notes:关于mybatis的一二级缓存

1. 一级缓存和二级缓存

  • 在mybatis中,为了节省资源,会将之前查询的数据缓存到SqlSession的对象中(一级缓存);
    当再次查询的时候,现在缓存中查询是否有相同的sql语句,如果有相同的语句,那么就直接从缓存中读出数据。
  • 一级缓存有个不足的地方就是,在一个项目中,不可能只有一个SqlSession,会有多个SqlSession存在;
    当多个SqlSession访问一个mapper的一个查询操作,并且sql语句相同时,那么每个SqlSession都会在数据库中查询一次,就比较消耗资源了;
    所以二级缓存就来了,二级缓存是基于mapper的,当有SqlSession来调用mapper时,会在SqlSessionFactory缓存中查找与mapper名空间对应的缓存区,查看缓存中是否有这个SqlSession要使用的sql语句相同的,如果有就直接从缓存中读取。
  • 但是一级缓存和二级缓存在执行了commit操作后,也就是增删改的提交操作后,会将缓存刷新;
    当一个SqlSession被关闭后,才会将这个SqlSession的一级缓存刷入二级缓存中。

2. 一级缓存的案例

因为一级缓存是默认的,我们只需要在mybatis中用同一条sql两次查询,就可以通过跟进日志了解一级缓存的执行信息。
下面是测试的部分代码,即用同一条sql执行两次查询,用不同的对象名来获取返回值:

        //绑定
        TeacherDao td = sqlSession.getMapper(TeacherDao.class);
        
        //一级缓存,基于mapper
        Teacher t= td.findTeacherByTname("htc");
        System.out.println("t:"+t);
        Teacher tt = td.findTeacherByTname("htc");
        System.out.println("tt:"+tt);

控制台打印信息:

Created connection 559670971.
==>  Preparing: select t.t_id t_id,t.t_name t_name from teacher t where t.t_name =? 
==> Parameters: htc(String)
<==    Columns: t_id, t_name
<==        Row: 1, htc
<==      Total: 1
Cache Hit Ratio [demo.cyj.dao.TeacherDao]: 0.0
==>  Preparing: select s.id id,s.stu_name stu_name,s.stu_age age,s.t_id t_id from student s left join teacher t on t.t_id = s.t_id where t.t_name=? 
==> Parameters: htc(String)
<==    Columns: id, stu_name, age, t_id
<==        Row: 8, 黑拐, 43, 1
<==        Row: 9, 拐, 50, 1
<==        Row: 10, 拐棍, 50, 1
<==        Row: 11, 大黑拐, 30, 1
<==        Row: 12, 小黑拐, 12, 1
<==        Row: 13, 拐哥, 29, 1
<==        Row: 14, 胡黑拐, 123, 1
<==      Total: 7
t:demo.cyj.pojo.Teacher_$$_jvst65e_0@5ae63ade
Cache Hit Ratio [demo.cyj.dao.TeacherDao]: 0.0
tt:demo.cyj.pojo.Teacher_$$_jvst65e_0@5ae63ade
  • 可以看到只创建了一次connection;
  • 并且只sql语句没有重复(控制台信息中之所以有两条sql,是因为我在之前配置过懒加载,什么是懒加载,为什么会有两条sql,可以点击并参考这篇文章);
  • 对象 t 和 tt 打印出来的hashcode也都是5ae63ade,也就说他们是相同的存储地址,的确是从缓存里拿出来的

3. 二级缓存案例

一级缓存是默认开启的,但二级缓存需要手动开启,所以会稍微做一些配置。配置如下:
在mybatis的总配置文件config.xml中的下添加一句

    
        
        
    

然后在需要用到二级缓存的mapper文件下的标签下添加一句


还有一个初次接触这个的人容易踩的一个坑就是,每个用到二级缓存的类都要序列化一次,也就是要实现serializable接口, 点击查看这个坑长什么样,建议不管用不用序列化,也将每个实体类都序列化了。

好了,配置做完了,就是测试了:

public class Test {
    
    public static void main(String[] args) throws IOException {
        String src = "config.xml";
        InputStream inputStream = Resources.getResourceAsStream(src);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //第一次查询
        SqlSession sqlSession = sessionFactory.openSession(true);
        TeacherDao td = sqlSession.getMapper(TeacherDao.class);     
        Teacher t= td.findTeacherByTname("htc");
        System.out.println("t:"+t);
        sqlSession.close();
        
        //第二次查询 
        SqlSession sqlSession2 = sessionFactory.openSession(true);
        TeacherDao td2 = sqlSession2.getMapper(TeacherDao.class);
        Teacher t2 = td2.findTeacherByTname("htc");
        System.out.println("t2:"+t2);
        sqlSession2.close();
    }
}

运行后,控制台打印日志:

Created connection 559670971.
==>  Preparing: select t.t_id t_id,t.t_name t_name from teacher t where t.t_name =? 
==> Parameters: htc(String)
<==    Columns: t_id, t_name
<==        Row: 1, htc
<==      Total: 1
Cache Hit Ratio [demo.cyj.dao.TeacherDao]: 0.0
==>  Preparing: select s.id id,s.stu_name stu_name,s.stu_age age,s.t_id t_id from student s left join teacher t on t.t_id = s.t_id where t.t_name=? 
==> Parameters: htc(String)
<==    Columns: id, stu_name, age, t_id
<==        Row: 8, 黑拐, 43, 1
<==        Row: 9, 拐, 50, 1
<==        Row: 10, 拐棍, 50, 1
<==        Row: 11, 大黑拐, 30, 1
<==        Row: 12, 小黑拐, 12, 1
<==        Row: 13, 拐哥, 29, 1
<==        Row: 14, 胡黑拐, 123, 1
<==      Total: 7
t:demo.cyj.pojo.Teacher_$$_jvst65e_0@5ae63ade
Closing JDBC Connection [com.mysql.jdbc.Connection@215be6bb]
Returned connection 559670971 to pool.
Cache Hit Ratio [demo.cyj.dao.TeacherDao]: 0.3333333333333333
t2:demo.cyj.pojo.Teacher@26aa12dd
  • 可以看到只create了一次connection,sql语句也没有重复,(为什么有两条sql参考 2.一级缓存 中的解释),但是hashcode有变化应该是从sqlSession关闭后,将数据刷到二级缓存中导致的,但是也的确只做了一次查询。

总结

mybatis中关于一二级缓存的代码已经封装的很好了,我们要用的话需要做的并不多,设置起来也很简单,唯一的细节之处就是关于序列化的问题

你可能感兴趣的:(mybatis-notes:关于mybatis的一二级缓存)