MyBatis缓存机制(一级缓存,二级缓存)
一,MyBatis一级缓存(本地缓存)
<select id="selectStudentByIdAndName" flushCache=”true” resultType="student">
select * from student where sid=#{Sid} and s_name=#{Sname}
</select>

public class MyBatisTest {
public static void main( String[] args ) {
SqlSession openSession = null;
try {
//mybatis配置文件
String resourse="mybatis-cfg.xml";
//通过 Resources 工具类将 ti -config.xm 配置文件读入 Reader
InputStream inputStream=Resources.getResourceAsStream(resourse);
//通过 SqlSessionFactoryBuilder 建造类使用 Reader 创建 SqlSessionFactory工厂对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//通过SqlSessionFactory工厂得到SqlSession
openSession = sqlSessionFactory.openSession();
//通过反射机制来获取对应的Mapper实例
StudentMapper mapper=openSession.getMapper(StudentMapper.class);
Student student1=mapper.selectStudentByIdAndName(2,"danghh");
Student student2=mapper.selectStudentByIdAndName(2,"danghh");
System.out.println(student1);
openSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally {
//最后一定不要忘记关闭 SqlSession ,否则会因为连接没有关闭导致数据库连接数过多,造成系统崩旗
openSession.close();
}
}
}
[DEBUG] - Setting autocommit to false on JDBC Connection[com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), danghh(String)
[DEBUG] - <== Total: 1
Student{SID=2, Sname='danghh', Sage=22, Ssex='nv', course=null}
Student{SID=2, Sname='danghh', Sage=22, Ssex='nv', course=null}
通过结果可以看出,由于代码中查询是在一个SqlSession,且两次查询过程中没有更新信息,不会导致一级缓存失效,所以结果只进行了一次数据库查询。
那如果是在两个SqlSession中分别进行查询呢?
结果:
[DEBUG] - Opening JDBC Connection
[DEBUG] - Checked out connection 234698513 from pool.
[DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), danghh(String)
[DEBUG] - <== Total: 1
[DEBUG] - Opening JDBC Connection
[DEBUG] - Created connection 1836797772.
[DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6d7b4f4c]
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), danghh(String)
[DEBUG] - <== Total: 1
Student{SID=2, Sname='danghh', Sage=22, Ssex='nv', course=null}
Student{SID=2, Sname='danghh', Sage=22, Ssex='nv', course=null}

[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), hjj(String)
[DEBUG] - <== Total: 1
[DEBUG] - ==> Preparing: update student set S_name=?,Sage=?,Ssex=? where Sid=?
[DEBUG] - ==> Parameters: hjj(String), 23(Integer), null, 2(Integer)
[DEBUG] - <== Updates: 1
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), hjj(String)
[DEBUG] - <== Total: 1
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
(这个参数是二级缓存的全局开关,默认值是 true ,初始状态为启用状态,所以也可忽略此步的配置)
(由于MyBatis二级缓存和命名空间namespace是绑定的 ,即二级缓存还需要在 Mapper.xml 映射文件中配置或者在 Mapper.java 接口中配置。)

- LRU (最近最少使用的) 移除最长时间不被使用的对象,这是默认值
- FIFO (先进先出〉 按对象进入缓存的顺序来移除它们
- SOFT (软引用) 移除基于垃圾回收器状态和软引用规则的对象
- WEAK (弱引用) 更积极地移除基于垃圾收集器状态和弱引用规则的对象
代码:
public class MyBatisTest {
public static void main( String[] args ) {
SqlSession openSession1 = null;
SqlSession openSession2 = null;
try {
//mybatis配置文件
String resourse="mybatis-cfg.xml";
//通过 Resources 工具类将 ti -config.xm 配置文件读入 Reader
InputStream inputStream=Resources.getResourceAsStream(resourse);
//通过 SqlSessionFactoryBuilder 建造类使用 Reader 创建 SqlSessionFactory工厂对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//通过SqlSessionFactory工厂得到SqlSession1
openSession1 = sqlSessionFactory.openSession();
StudentMapper mapper1=openSession1.getMapper(StudentMapper.class);
//通过SqlSessionFactory工厂得到SqlSession2
openSession2 = sqlSessionFactory.openSession();
StudentMapper mapper2=openSession2.getMapper(StudentMapper.class);
//使用会话1进行查询,此次查询结果只会存储在一级缓存中
Student student1=mapper1.selectStudentByIdAndName(2,"hjj");
System.out.println(student1);
//使用会话2进行查询,前面会话未关闭,数据不会被刷到二级缓存中,所以本次仍会执行sql
Student student2=mapper2.selectStudentByIdAndName(2,"hjj");
System.out.println(student2);
//使用会话2进行查询,由于前面已执行过该方法,所以可在一级缓存中查到
Student student3=mapper2.selectStudentByIdAndName(2,"hjj");
System.out.println(student3);
openSession1.commit();
} catch (IOException e) {
e.printStackTrace();
}finally {
//最后一定不要忘记关闭 SqlSession ,否则会因为连接没有关闭导致数据库连接数过多,造成系统崩旗
openSession1.close();
}
}
}
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0
[DEBUG] - Opening JDBC Connection
[DEBUG] - Checked out connection 234698513 from pool.
[DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), hjj(String)
[DEBUG] - <== Total: 1
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0
[DEBUG] - Opening JDBC Connection
[DEBUG] - Created connection 1843368112.
[DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6ddf90b0]
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), hjj(String)
[DEBUG] - <== Total: 1
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
[DEBUG] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - Returned connection 234698513 to pool.
该颜色:表示会话1第一次查询的结果,由于第一次查询,一级缓存和二级缓存中都没有数据,所以Mapper命中率为0.0,且进行了数据库查询,并将结果存储到会话1一级缓存中。
该颜色:表示会话2第一次查询的结果,由于会话1没有关闭,所以会话1的一级缓存不会刷到Mapper的二级缓存中,并且是在会话2中第一次查询该方法,所以Mapper命中率为0.0,且进行了数据库查询,并将结果存储到会话2的一级缓存中。
运行结果:
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0
[DEBUG] - Opening JDBC Connection
[DEBUG] - Checked out connection 234698513 from pool.
[DEBUG] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - ==> Preparing: select * from student where sid=? and s_name=?
[DEBUG] - ==> Parameters: 2(Integer), hjj(String)
[DEBUG] - <== Total: 1
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.0
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
[DEBUG] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@dfd3711]
[DEBUG] - Returned connection 234698513 to pool.
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.3333333333333333
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
[DEBUG] - Cache Hit Ratio [MyBatisDemo.StudentMapper]: 0.5
Student{SID=2, Sname='hjj', Sage=23, Ssex='null', course=null}
MyBatis缓存机制(一级缓存,二级缓存)的更多相关文章
- mybatis学习--缓存(一级和二级缓存)
声明:学习摘要! MyBatis缓存 我们知道,频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级) ...
- Mybatis学习(五)————— 延迟加载和缓存机制(一级二级缓存)
一.延迟加载 延迟加载就是懒加载,先去查询主表信息,如果用到从表的数据的话,再去查询从表的信息,也就是如果没用到从表的数据的话,就不查询从表的信息.所以这就是突出了懒这个特点.真是懒啊. Mybati ...
- Mybatis(五) 延迟加载和缓存机制(一级二级缓存)
踏踏实实踏踏实实,开开心心,开心是一天不开心也是一天,路漫漫其修远兮. --WH 一.延迟加载 延迟加载就是懒加载,先去查询主表信息,如果用到从表的数据的话,再去查询从表的信息,也就是如果没用到从表的 ...
- MyBatis加强(1)~缓存机制(一级缓存、二级缓存、第三方缓存技术redis、ehcache)
一.缓存机制 使用缓存可以使应用更快地获取数据,避免频繁的数据库交互操作,尤其是在查询越多,缓存命中率越高 的情况下,缓存的作用就越明显. 1.缓存原理:Map ■ 查询时,先从缓存区查询:找到,返回 ...
- Mybatis一级、二级缓存
Mybatis一级.二级缓存 一级缓存 首先做一个测试,创建一个mapper配置文件和mapper接口,我这里用了最简单的查询来演示. <mapper namespace="c ...
- mybatis缓存,包含一级缓存与二级缓存,包括ehcache二级缓存
一,引言 首先我们要明白一点,缓存所做的一切都是为了提高性能.明白了这一点下面我们开始进入正题. 二,mybatis缓存概要 ①.mybatis的缓存有两种,分别是一级缓存和二级缓存.两者都属于查询缓 ...
- Mybatis 的一级、二级缓存?
1)一级缓存: 基于 PerpetualCache 的 HashMap 本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该 Session 中的所有 C ...
- Mybatis 的一级、二级缓存?
1)一级缓存: 基于 PerpetualCache 的 HashMap 本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该 Session 中的所有 C ...
- Mybatis 源码分析之一二级缓存
一级缓存 其实关于 Mybatis 的一级缓存是比较抽象的,并没有什么特别的配置,都是在代码中体现出来的. 当调用 Configuration 的 newExecutor 方法来创建 executor ...
- Mybatis基于注解开启使用二级缓存
关于Mybatis的一级缓存和二级缓存的概念以及理解可以参照前面文章的介绍.前文连接:https://www.cnblogs.com/hopeofthevillage/p/11427438.html, ...
随机推荐
- 十分钟通过一个实际问题,真正教会大家如何解决Bug
前言 这篇文章从实际问题 -> 问题解决步骤 -> 问题解决思路,帮助大家能够明白如何在程序中发现问题,定位问题,解决问题.并真正理解那些问题解决思路. 首先说说这个实际问题是什么,又是怎 ...
- MapReduce基本认识
MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算. 主要由Split.Map.Partition.Sort.Combine(需要自己写).Merge.Reduce组成,一般来 ...
- 一种特殊的生成器函数-Generator函数
本节的内容,是建立在iterator遍历器知识的基础上.所以希望还没有看上一节的内容的话,最好还是看一看,当然你如果熟悉iterator就没有那个必要了. 既然你都看到这里来了,就咱们就接着往下讲.. ...
- js判断一个元素是否在数组内
1.indexOf()返回给定元素在数组内的索引值,如果不存在则返回-1 var arr=[0,1,2,3,4,5] console.log(arr.indexOf(1)) console.log(a ...
- umditor删除域名,配置为绝对路径
getAllPic: function (sel, $w, editor) { var me = this, arr = [], $imgs = $(sel, $w); $.each($imgs, f ...
- 2019-2020-1 20199325《Linux内核原理与分析》第十二周作业
什么是ShellShock? Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开.许多互联网守护进程,如网页服务器 ...
- automake autoconf 使用详解
本文地址: http://www.laruence.com/2009/11/18/1154.html 文章转自: http://www.linuxcomputer.cn/ 作为Linux下的程序开发人 ...
- 【Inno Setup】查看是否安装了VC++ 2015 Redistributeable
可能有必要先测一下注册表的这一项是否存在 if RegValueExists(HKLM, 'SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0\VC\Ru ...
- LaTex中文article模板(支持代码、数学、TikZ)
代码 请使用XeLatex编译 main.tex \documentclass{article} \usepackage{ctex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...
- $_server[]关于浏览器和服务器的参数获取
$_SERVER['USER'] www $_SERVER['HOME'] /home/www $_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS'] 1 $_SERVE ...