mybatis一级缓存二级缓存

一级缓存

  Mybatis对缓存提供支持,但是在没有配置的默认情况下,它只开启一级缓存,一级缓存只是相对于同一个SqlSession而言。所以在参数和SQL完全一样的情况下,我们使用同一个SqlSession对象调用一个Mapper方法,往往只执行一次SQL,因为使用SelSession第一次查询后,MyBatis会将其放在缓存中,以后再查询的时候,如果没有声明需要刷新,并且缓存没有超时的情况下,SqlSession都会取出当前缓存的数据,而不会再次发送SQL到数据库。

              

  为什么要使用一级缓存,不用多说也知道个大概。但是还有几个问题我们要注意一下。

  1、一级缓存的生命周期有多长?

  a、MyBatis在开启一个数据库会话时,会 创建一个新的SqlSession对象,SqlSession对象中会有一个新的Executor对象。Executor对象中持有一个新的PerpetualCache对象;当会话结束时,SqlSession对象及其内部的Executor对象还有PerpetualCache对象也一并释放掉。

  b、如果SqlSession调用了close()方法,会释放掉一级缓存PerpetualCache对象,一级缓存将不可用。

  c、如果SqlSession调用了clearCache(),会清空PerpetualCache对象中的数据,但是该对象仍可使用。

  d、SqlSession中执行了任何一个update操作(update()、delete()、insert()) ,都会清空PerpetualCache对象的数据,但是该对象可以继续使用

    2、怎么判断某两次查询是完全相同的查询?

  mybatis认为,对于两次查询,如果以下条件都完全一样,那么就认为它们是完全相同的两次查询。

  2.1 传入的statementId

  2.2 查询时要求的结果集中的结果范围

  2.3. 这次查询所产生的最终要传递给JDBC java.sql.Preparedstatement的Sql语句字符串(boundSql.getSql() )

  2.4 传递给java.sql.Statement要设置的参数值

二级缓存:

  MyBatis的二级缓存是Application级别的缓存,它可以提高对数据库查询的效率,以提高应用的性能。

  MyBatis的缓存机制整体设计以及二级缓存的工作模式

  

 

  SqlSessionFactory层面上的二级缓存默认是不开启的,二级缓存的开席需要进行配置,实现二级缓存的时候,MyBatis要求返回的POJO必须是可序列化的。 也就是要求实现Serializable接口,配置方法很简单,只需要在映射XML文件配置就可以开启缓存了<cache/>,如果我们配置了二级缓存就意味着:

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

实践:

一、创建一个POJO Bean并序列化

  由于二级缓存的数据不一定都是存储到内存中,它的存储介质多种多样,所以需要给缓存的对象执行序列化。(如果存储在内存中的话,实测不序列化也可以的。)

package com.yihaomen.mybatis.model;import com.yihaomen.mybatis.enums.Gender;
import java.io.Serializable;
import java.util.List;/***  @ProjectName: springmvc-mybatis */
public class Student implements Serializable{private static final long serialVersionUID = 735655488285535299L;private String id;private String name;private int age;private Gender gender;private List<Teacher> teachers;
setters
&getters()....;toString(); }

 

 二、在映射文件中开启二级缓存

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yihaomen.mybatis.dao.StudentMapper"><!--开启本mapper的namespace下的二级缓存--><!--eviction:代表的是缓存回收策略,目前MyBatis提供以下策略。(1) LRU,最近最少使用的,一处最长时间不用的对象(2) FIFO,先进先出,按对象进入缓存的顺序来移除他们(3) SOFT,软引用,移除基于垃圾回收器状态和软引用规则的对象(4) WEAK,弱引用,更积极的移除基于垃圾收集器状态和弱引用规则的对象。这里采用的是LRU,移除最长时间不用的对形象flushInterval:刷新间隔时间,单位为毫秒,这里配置的是100秒刷新,如果你不配置它,那么当SQL被执行的时候才会去刷新缓存。size:引用数目,一个正整数,代表缓存最多可以存储多少个对象,不宜设置过大。设置过大会导致内存溢出。这里配置的是1024个对象readOnly:只读,意味着缓存数据只能读取而不能修改,这样设置的好处是我们可以快速读取缓存,缺点是我们没有办法修改缓存,他的默认值是false,不允许我们修改--><cache eviction="LRU" flushInterval="100000" readOnly="true" size="1024"/><resultMap id="studentMap" type="Student"><id property="id" column="id" /><result property="name" column="name" /><result property="age" column="age" /><result property="gender" column="gender" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" /></resultMap><resultMap id="collectionMap" type="Student" extends="studentMap"><collection property="teachers" ofType="Teacher"><id property="id" column="teach_id" /><result property="name" column="tname"/><result property="gender" column="tgender" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/><result property="subject" column="tsubject" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/><result property="degree" column="tdegree" javaType="string" jdbcType="VARCHAR"/></collection></resultMap><select id="selectStudents" resultMap="collectionMap">SELECTs.id, s.name, s.gender, t.id teach_id, t.name tname, t.gender tgender, t.subject tsubject, t.degree tdegreeFROMstudent sLEFT JOINstu_teach_rel strONs.id = str.stu_idLEFT JOINteacher tONt.id = str.teach_id</select><!--可以通过设置useCache来规定这个sql是否开启缓存,ture是开启,false是关闭--><select id="selectAllStudents" resultMap="studentMap" useCache="true">SELECT id, name, age FROM student</select><!--刷新二级缓存<select id="selectAllStudents" resultMap="studentMap" flushCache="true">SELECT id, name, age FROM student</select>-->
</mapper>

 

三、在 mybatis-config.xml中开启二级缓存

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><!--这个配置使全局的映射器(二级缓存)启用或禁用缓存--><setting name="cacheEnabled" value="true" />.....</settings>....
</configuration>

 

四、测试

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<Student> list = mapper.selectAllStudents();System.out.println(list);System.out.println("第二次执行");List<Student> list2 = mapper.selectAllStudents();System.out.println(list2);session.commit();System.out.println("二级缓存观测点");SqlSession session2 = sqlSessionFactory.openSession();StudentMapper mapper2 = session2.getMapper(StudentMapper.class);List<Student> list3 = mapper2.selectAllStudents();System.out.println(list3);System.out.println("第二次执行");List<Student> 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只执行了一次,证明我们的二级缓存生效了。

 

https://gitee.com/huayicompany/springmvc-mybatis

参考:

[1]杨开振 著,《深入浅出MyBatis技术原理与实战》, 电子工业出版社,2016.09

[2]博客,http://blog.csdn.net/luanlouis/article/details/41280959

[3]博客,http://www.cnblogs.com/QQParadise/articles/5109633.html

[4]博客,http://blog.csdn.net/isea533/article/details/44566257

转载于:https://www.cnblogs.com/happyflyingpig/p/7739749.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/253030.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CMOS Sensor的调试分享

目前&#xff0c;包括移动设备在内的很多多媒体设备上都使用了摄像头&#xff0c;而且还在以很快的速度更新换代。目前使用的摄像头分为两种&#xff1a;CCD(Charge Couple Device电荷偶合器件)和 CMOS(Complementary Metal Oxide Semiconductor互补金属氧化物半导体)。这两种各…

利用反射修改final数据域

当final修饰一个数据域时&#xff0c;意义是声明该数据域是最终的&#xff0c;不可修改的。常见的使用场景就是eclipse自动生成的serialVersionUID一般都是final的。 另外还可以构造线程安全&#xff08;thread safe&#xff09;的immutable类&#xff0c;比如String&#xff0…

mysql简单创建数据库权限(待修改备注)

CREATE DATABASE web DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;一、环境&#xff1a;CentOS 6.8mysql 5.6二、背景给外包的工作人员提供我司某台服务器的 mysql 中某个数据库的访问权限。之所以要做限制&#xff0c;是防止他们对我司其他的数据库非法进行操作。三、…

Centos 能ping通域名和公网ip但是网站不能够打开,服务器拒绝了请求。打开80端口解决。...

博客搬迁&#xff0c;给你带来的不便&#xff0c;敬请谅解&#xff01; http://www.suanliutudousi.com/2017/10/29/centos-%E8%83%BDping%E9%80%9A%E5%9F%9F%E5%90%8D%E5%92%8C%E5%85%AC%E7%BD%91ip%E4%BD%86%E6%98%AF%E7%BD%91%E7%AB%99%E4%B8%8D%E8%83%BD%E5%A4%9F%E6%89%93…

ISP 图像传感器camera原理

1、Color Filter Array — CFA 随着数码相机、手机的普及&#xff0c;CCD/CMOS 图像传感器近年来得到广泛的关注和应用。 图像传感器一般都采用一定的模式来采集图像数据&#xff0c;常用的有 BGR 模式和 CFA 模式。BGR 模式是一种可直接进行显示和压缩等处理的图像数据模式&am…

51nod 1027 大数乘法

1027 大数乘法基准时间限制&#xff1a;1 秒 空间限制&#xff1a;131072 KB 分值: 0 难度&#xff1a;基础题收藏关注给出2个大整数A,B&#xff0c;计算A*B的结果。 Input第1行&#xff1a;大数A 第2行&#xff1a;大数B (A,B的长度 < 1000&#xff0c;A,B > 0&#xff…

file mmap

do_set_pmd统计参数只会在这里设置&#xff1a; add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR);但是这貌似都是处理大页的情况哪&#xff0c;小页呢&#xff1f; alloc_set_pte中有函数&#xff1a;inc_mm_couter_fast(vma->vm_mm, mm_couter_file(page)&…

Linux链接库三(C跟C++之间动态库的相互调用)

http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http://www.jb51.net/article/34990.htm C和C之间库的互相调用 extern "C"的理解&#xff1a; 很多人认为"C"表示的C语言&#xff0c;实际并非如此&…

C#如何开发多语言支持的Winform程序

C# Winform项目多语言实现(支持简/繁/英三种语言)有很多种方案实现多语言&#xff0c;我在这里介绍一种最简单最容易理解的&#xff0c;作为教学材题应该从通俗易懂入手。在写这篇文章之前&#xff0c;本来想用枚举窗体对象成员的方式设置语言&#xff0c;但是找不到源代码了&a…

Alpha 冲刺 (2/10)

Alpha 冲刺 &#xff08;2/10&#xff09; 队名&#xff1a;第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬&#xff08;组长&#xff09; 过去两天完成了哪些任务&#xff1a; 文字/口头描述&#xff1a; 1、学习qqbot库&#xff1b; 2、实时保存…

Linux学习之第二课时--linux命令格式及命令概述

命令概述 Linux提供了大量的命令&#xff0c;利用它可以有效地完成大量的工作&#xff0c;如磁盘管理&#xff0c;文件存取&#xff0c;目录操作&#xff0c;进程管理&#xff0c;文件权限设定等 Linux命令格式 Linux命令的组成部分&#xff1a;命令字 命令选项参数&#xff…

Linux C语言调用C++动态链接库

Linux C语言调用C动态链接库 标签&#xff1a; C调用C库 2014-03-10 22:56 3744人阅读 评论(0) 收藏 举报 分类&#xff1a; 【Linux应用开发】&#xff08;48&#xff09; 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 如果你有一个c做的动态…

Android实践 -- 对apk进行系统签名

对apk进行系统签名 签名工具 网盘下载 &#xff0c;需要Android系统的签名的文件platform.x509.pem 和 platform.pk8 这个两个文件在Android源码中的 ./build/target/product/security 目录下 具体的使用方法&#xff1a; java -jar signapk.jar platform.x509.pem platform.…

Java编写基于netty的RPC框架

一 简单概念RPC: ( Remote Procedure Call),远程调用过程,是通过网络调用远程计算机的进程中某个方法,从而获取到想要的数据,过程如同调用本地的方法一样.阻塞IO :当阻塞I/O在调用InputStream.read()方法是阻塞的,一直等到数据到来时才返回,同样ServerSocket.accept()方法时,也…

linux下c和c++互相调用

c调用cpp 创建个目录 创建4个文件 c.c--c文件 cpp.cpp--c文件 cpp.hh--c声明文件 Makefile c.c [javascript] view plaincopy#include "cpp.hh" int main() { cpp_fun(); } cpp.cpp [cpp] view plaincopy#include "cpp.hh" #include <stdi…

Applications Manager Docker监控

Docker 是一个流行的开源容器应用程序&#xff0c;允许您将应用程序、应用程序的内部依赖和关联库打包到一个单元中。Docker 的主要优点在于单台机器上的多个 docker 容器共享同一操作系统内核&#xff0c;这可以帮助提升性能和节省大量内存。监控 docker 容器会很困难&#xf…

find

Linux中find常见用法示例 find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数&#xff1b; pathname: find命令所查找的目录路径。例如用.来表示当前目录&#xff0c;用/来表示系统根目录。-print&#xff1a; find命令将匹配的文件输出…

PHP将多个文件中的内容合并为新的文件

function test(){$hostdir iconv("utf-8","gbk","C:\Users\原万里\Desktop\日常笔记") ; //iconv()转换编码方式&#xff0c;将UTF-8转换为gbk&#xff0c;若是报错在gbk后加//IGNORE$filesnames scandir($hostdir); …

HTTP Live Streaming直播(iOS直播)技术分析与实现

不经意间发现&#xff0c;大半年没写博客了&#xff0c;自觉汗颜。实则2012后半年&#xff0c;家中的事一样接着一样发生&#xff0c;实在是没有时间。快过年了&#xff0c;总算忙里偷闲&#xff0c;把最近的一些技术成果&#xff0c;总结成了文章&#xff0c;与大家分享。 前些…

中文论文格式【杂】

转自知乎&#xff0c;https://www.zhihu.com/question/23791742/answer/344752056 【纸张】毕业论文一律打印&#xff0c;采取A4纸张&#xff0c;页边距一律采取&#xff1a;上、下2.5cm&#xff0c;左3cm,右1.5cm&#xff0c;行间距取多倍行距(设置值为1.25);字符间距为默认值…