博客
关于我
浅谈MyBatis缓存
阅读量:346 次
发布时间:2019-03-04

本文共 2883 字,大约阅读时间需要 9 分钟。

MyBatis 缓存机制解析

MyBatis 是一款流行的ORM框架,其默认配置提供了一级和二级缓存机制。通过合理配置这些缓存,可以显著提升应用程序的性能。本文将从一级缓存和二级缓存两个方面展开分析,并简要讨论第三方缓存(如ehcache)的使用方法。

一级缓存

MyBatis 的一级缓存是默认开启的,作用范围限于同一个 SqlSession 对象。其主要功能是缓存 mapper 接口方法调用的结果,避免重复执行相同的数据库查询。

示例代码解析:

public static void query() throws IOException {    Reader reader = Resources.getResourceAsReader("conf.xml");    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);    SqlSession session = sessionFactory.openSession();    StudentMapper studentMapper = session.getMapper(StudentMapper.class);    Student3 student1 = studentMapper.queryStudentByStuno(1);    Student3 student2 = studentMapper.queryStudentByStuno(1);    System.out.println(student1.getStuNo() + "," + student1.getStuName());    System.out.println(student2.getStuNo() + "," + student2.getStuName());    session.close();}

在以上代码中,student1 和 student2 是从同一个 SqlSession 中获取的 StudentMapper 实例,因此它们共享相同的缓存。通过日志可以看到,只执行了一条 SQL 语句,这表明一级缓存有效地避免了重复查询。

缓存清除:

如果需要清除一级缓存,可以通过 session.commit() 方法执行。这一点在上述代码中得到了验证:在 session.commit() 之后,执行相同的查询会触发两次数据库查询,缓存被清空。

二级缓存

与一级缓存不同,二级缓存的配置需要手动开启,且其作用范围基于相同的命名空间(namespace)。要实现二级缓存,需要完成以下配置步骤:

  • 核心配置文件设置:

    打开 mybatis-config.xml,在 <settings> 标签下添加以下配置:

  • Mapper 接口配置:

    在对应的 mapper.xml 文件中启用二级缓存,可以通过添加 <cache> 标签:

    如果需要针对特定查询禁用二级缓存,可以在 select 标签中添加 useCache="false" 属性。

  • 实体类序列化:

    为确保二级缓存正常工作,需要确保使用的实体类(如 Student3)实现 Serializable 接口。此外,级联属性也需进行序列化处理。

  • 二级缓存的使用示例:

    public static void query() throws IOException {    Reader reader = Resources.getResourceAsReader("conf.xml");    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);    SqlSession session = sessionFactory.openSession();    StudentMapper studentMapper = session.getMapper(StudentMapper.class);    Student3 student3 = studentMapper.queryStudentByStuno(2);    session.close();    // 第二次查询    SqlSession session2 = sessionFactory.openSession();    StudentMapper studentMapper2 = session2.getMapper(StudentMapper.class);    Student3 student4 = studentMapper2.queryStudentByStuno(2);    session2.close();    System.out.println(student3.getStuNo() + "," + student3.getStuName());    System.out.println(student4.getStuNo() + "," + student4.getStuName());}

    在上述代码中,虽然 session1 和 session2 是不同的 SqlSession 对象,但它们获取的 StudentMapper 实例共享同一个命名空间,因此可以正常使用二级缓存。

    缓存清除方法:

    二级缓存的清除操作与一级缓存类似,需执行增删改操作的 commit()。此外,若在 select 标签中设置 flushCache="true",可以实现自动清除缓存。

    第三方缓存(ehcache)

    ehcache 是 MyBatis 官方推荐的第三方二级缓存解决方案。其配置相对简单,主要步骤如下:

  • 导入必要 jar 文件:

    - Ehcache-core.jar- Mybatis-ehcache.jar- Self-api.jar
  • 配置 ehcache.xml:

    创建或修改 ehcache.xml 文件,通常放在 src/main/resources 目录下。示例配置如下:

  • 更新 mapper.xml:

    在 mapper.xml 中启用 ehcache 缓存:

  • 注意事项:

    • 实体类需实现 Serializable 接口,特别是包含级联属性的类。
    • ehcache 的配置需要确保 ehcache.jar 和相关依赖已正确导入项目。
    • 二级缓存的命名空间需与 MyBatis 的命名空间一致,否则可能导致缓存不命中。

    通过以上配置,可以有效提升 MyBatis 应用程序的性能,减少数据库查询压力。对于复杂的实体类或大数据量的查询,推荐结合一级和二级缓存使用,或者采用 ehcache 等第三方缓存解决方案。

    转载地址:http://ryhe.baihongyu.com/

    你可能感兴趣的文章
    Python制作简单的学生成绩管理系统
    查看>>
    python制作甘特图的基本知识(附Demo)
    查看>>
    python制作一个简单的服务器,【Python】 做一个简单的 http 服务器
    查看>>
    Python到底能做什么?它的优点在哪?
    查看>>
    python利用pytorch库导出图像分割算子
    查看>>
    python利用pyshark监听网卡来抓包其中pyshark中摸索的一些可用参数
    查看>>
    python利用excel分析过杀漏失
    查看>>
    python判断汉字数目
    查看>>
    python判断文件是空的,如果是空的,就删除
    查看>>
    python判断密码是否正确_python密码判断是否符合要求的方法
    查看>>
    python判断字符串包含中文_Python 判断字符串是否包含中文
    查看>>
    python删除第一行_Python 乱码指北:一行删掉根目录
    查看>>
    Python删除列表元素的三种方法
    查看>>
    python初步学习-python数据类型-集合(set)
    查看>>
    python列表生成字典_Python中将字典转换为列表的方法
    查看>>
    python列表对应元素合并为列表及判断一个列表是几维
    查看>>
    python列表去重复后按照顺序_从包含不可共元素的Python列表中删除重复元素,同时保留顺序?...
    查看>>
    python列表前几个_python之列表
    查看>>
    python列表元组
    查看>>
    Python列表/元组/字典和集合使用
    查看>>