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, ...
随机推荐
- liunx常用知识基本命令大全
liunx基础命令使用 标签(空格分隔):liunx常用命令 网络配置 虚拟网卡的绝对路径 /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ...
- Android 中 OkHttp 三步实现生命周期绑定
简介 OkHttps 是 OkHttp 增强版的超轻量封装包. 和 Retrofit 相比,它更加轻量(只有 59Kb),是 Retrofit (124Kb)的一半,而且更加的开箱即用,API 更加自 ...
- 日志分析工具ELK(三)
目前官网更新特别快,不到半年时间就更新了好几个版本,目前最新的是5.1 以下安装配置使用4.5版本的 https://www.elastic.co/guide/en/kibana/4.5/index. ...
- vue项目中使用bpmn-基础篇
内容概述 本系列“vue项目中使用bpmn-xxxx”分为五篇,均为自己使用过程中用到的实例,手工原创,目前属于陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项 ...
- Java ASM3学习(3)
MethodVisitor ClassVisitor的visitMethod能够访问到类中某个方法的一些入口信息,那么针对具体方法中字节码的访问是由MethodVisitor来进行的 访问顺序如下,其 ...
- Ribbon 框架简介及搭建
2019独角兽企业重金招聘Python工程师标准>>> Ribbon简介 1. 负载均衡框架,支持可插拔式的负载均衡规则 2. 支持多种协议,如HTTP.UDP等 3. 提供负 ...
- 业务SQL那些事--慎用LIMIT
业务SQL那些事--慎用LIMIT 在业务中使用LIMIT限制SQL返回行数是很常见的事情,但如果不知道其中可能的坑或者说真正执行逻辑,就可能会使SQL执行非常慢,严重影响性能. LIMIT OFFS ...
- 求x>0时,y=x^3-6x^2+15的极值
解: 当x→∞时,y也→∞,所以y没有最大值. y=x3-6x2+15=-4*(x/2)*(x/2)*(6-x)+15 而根据几何平均数小于等于算术平均数的定理,(x/2)*(x/2)*(6-x)在x ...
- JAVA第一次blog总结
JAVA第一次blog总结 0.前言 大一下学期我们开展了OPP这门课程,这也是我们第一次接触到JAVA.与上学期我们在学校里学C语言不同的是,这学期由于疫情原因我们是以网课的方式在学习.在学习中我发 ...
- Pointers and Memory
Stanford CS Education Library #102 一.Basic Pointers 指针主要有两个用途:使不同的代码段共享信息.方便链表(树)的处理. 指针示意图: derefer ...