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, ...
随机推荐
- tp3.2的__construct和_initialize方法
在tp3.2框架里面,有一个php自带的__construct()构造函数和tp3自带的构造函数_initialize()的实行顺序是先实行 php自带的__construct()构造函数 再实行 t ...
- 地点下来框的实现(php)
效果图: 样式(bootstrap): class="selectpicker show-tick form-control",就是多了个form-contro就行了 概念: 这里 ...
- select和epoll区别
select.epoll 区别总结: 1.支持一个进程所能打开的最大连接数 select 单个进程所能打开的最大连接数有FD_SETSIZE宏定义,其大小是32个整数的大小(在32位的机器上,大小就是 ...
- java中functional interface的分类和使用
目录 简介 Functional Interface Function:一个参数一个返回值 BiFunction:接收两个参数,一个返回值 Supplier:无参的Function Consumer: ...
- 浅析Java三大特性封装、继承、多态,及作业分析
前言 本次博客衔接上次博客,作为这一阶段Java学习的分析.上一篇博客着重介绍了Java的OO编程思维,面向对象与面向过程的区别.本篇博客重心在Java的三大技术特性,附带作业分析. Java三大特性 ...
- ajax 技术
ajax 技术 $.ajax({ url:"", type:'GET', success:function(data){ console.log(data); }, error:f ...
- 边缘控制平面Ambassador全解读
Ambassador是由Datawire开源的一个API网关项目,主要在Kubernetes的容器编排框架中使用.Ambassador本质上是一个通过配置边缘/API来管理Envoy数据面板的控制面板 ...
- 【React踩坑记六】create-react-app创建的react项目通过iP地址访问(实现局域网内访问)
同项目组的小伙伴想用自己的电脑访问我电脑上开发阶段的create-react-app创建的react项目. 试过了了各种内网穿透工具ngrok以及localtunnel等. 奈何打开效率实在太过于龟速 ...
- ES6中Fetch的封装及使用,炒鸡简单~
之前写过一篇<ajax.axios.fetch之间的详细区别以及优缺点> 戳这里 1.封装 (http.js) class Ajax { get(url) { return new Pro ...
- 内蒙古特检院利用物联网/RFID技术提高电梯检测水平
随着电梯检验工作信息化进程的进一步深入,内蒙古特检院从检验工作中寻找新方法.新手段,为检验员新引入电梯检验手持终端设备,力求提高电梯检验水平,将"电梯安全惠民工程"落到实处. 电梯 ...