zoukankan      html  css  js  c++  java
  • 课时10:MyBatis一级缓存、二级缓存

    .1)查询缓存

      1.一级缓存

        1.1 同一个SqlSession对象,mybatis默认就开启了一级缓存,下方为示意图

        1.2 如果用同样的SqlSession对象查询相同的数据,则只会在第一次 查询时 向数据库发送SQL语句,并将查询的结果 放入SqlSession中(作为缓存存在);后续再次查询同样的对象时,则直接从缓存中查询该对象即可(则省略了数据库的访问)

        1.3 一旦commit就会清空SqlSession中的缓存

        1.4测试代码如下

     public static void selectOneStudent() throws IOException {
            Reader reader = Resources.getResourceAsReader("config.xml");
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
            SqlSession session=sessionFactory.openSession();
            IStudentDao iStudentDao=session.getMapper(IStudentDao.class);
            Student student = iStudentDao.selectStudentById(1);
            session.commit();
            Student student2 = iStudentDao.selectStudentById(1);
            System.out.println(student);
            System.out.println(student2);
            session.close();
            reader.close();
        }

      2.二级缓存

        2.1 mybatis默认关闭二级缓存 需要手动的打开

          2.1.1 在主配置文件中配置

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

          2.1.2 在具体的mapper.xml声明开启

    <!--    声明开启-->
    <cache/>

          2.1.3 异常提示 :java.io.NotSerializableExceptio可知,mybatis的二级缓存是将对象 放入硬盘

            序列化:内存---》硬盘

            反序列化:硬盘---》内存

            如何解决?

             在实体类中实现Serializable接口 (序列化student类,以及Student的级联属性,和Student父类)

        2.2  二级缓存的范围在同一个namespace中产生的接口生成的代理对象都可以共享缓存

           2.3 如果是同一个SqlSession对象进行多次查询,则直接进入一级缓存查询; 如果不是同一个SqlSession进行多次查询(但是都是一个namespace),则进入二级缓存

        2.4 测试如下:

     public static void selectOneStudent() throws IOException {
            Reader reader = Resources.getResourceAsReader("config.xml");
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
            SqlSession session=sessionFactory.openSession();
            IStudentDao iStudentDao=session.getMapper(IStudentDao.class);
            Student student = iStudentDao.selectStudentById(1);
            SqlSession session2=sessionFactory.openSession();
            IStudentDao iStudentDao2=session2.getMapper(IStudentDao.class);
            Student student2 = iStudentDao2.selectStudentById(1);
            System.out.println(student);
            System.out.println(student2);
            session.close();
            reader.close();
        }

        2.5 细节补充在查询完一次之后需要关闭SqlSession,不然该对象不会存入二级缓存

          存储时机就是当执行SqlSession.close()时 才会存入到二级缓存,

          如果多个文件的namespace的值相同,通过这些mapper.xml产生的mapper对象 仍然共享二级缓存

  • 相关阅读:
    服务器端渲染SSR的优缺点?
    Vue轻量级富文本编辑器-Vue-Quill-Editor
    浏览器跨域问题
    CSS中 !important 的使用
    如何使用vue-table-with-tree-grid的树形表格组件
    各类程序员学习路线图
    Vue中 el-table大数据量加载,不分页,节省内存的性能优化
    看完让你彻底理解 WebSocket 原理,附完整的实战代码(包含前端和后端)
    vue中class类名根据绑定的值三元表达式或字符串拼接动态添加类名
    close事件 vue_vue 监听浏览器关闭事件
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12458055.html
Copyright © 2011-2022 走看看