mybatis一级缓存和二级缓存的使用
在mybatis中,有一级缓存和二级缓存的概念:
一级缓存:一级缓存 Mybatis的一级缓存是指SQLSession,一级缓存的作用域是SQLSession, Mabits默认开启一级缓存。在同一个SqlSession中,执行相同的SQL查询时;第一次会去查询数据库,并写在缓存中,第二次会直接从缓存中取。当执行SQL时候两次查询中间发生了增删改的操作,则SQLSession的缓存会被清空。
这么设计的原因是避免读取脏数据:假设A查询了某商品库存为10件,并将10件库存的数据存入缓存中,之后被客户买走了10件,数据被delete了,但是下次查询这件商品时,并不从数据库中查询,而是从缓存中查询,就会出现错误。
二级缓存:二级缓存是mapper级别的,Mybatis默认是没有开启二级缓存的。 第一次调用mapper下的SQL去查询用户的信息,查询到的信息会存放代该mapper对应的二级缓存区域。
看概念什么的最烦了是不是?用代码看看呗:
package top.bigking; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import top.bigking.dao.DeptMapper;
import top.bigking.dao.UserMapper;
import top.bigking.pojo.Dept;
import top.bigking.pojo.User; import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class mybatisStudy_start {
public static void main(String[] args) throws Exception{
InputStream in = Resources.getResourceAsStream("mybatisStudy-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession(true);//true 表示 自动提交
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
List<Dept> deptList = deptMapper.query();
for(Dept dept : deptList){
System.out.println(dept);
}
System.out.println("-----------------------------------");
//insert(deptMapper);
//update(deptMapper);
findById(deptMapper); findById(deptMapper);
//delete(deptMapper);
//findByIdDname(deptMapper); // UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// List<User> userList = userMapper.query();
// for(User user : userList)
// System.out.println(user);
sqlSession.close(); }
private static void insert(DeptMapper deptMapper){
Dept dept = new Dept();
dept.setDeptNo(11);
dept.setDname("ABKing");
dept.setLoc("111");
int count = deptMapper.insert(dept);
System.out.println(count);
}
public static void update(DeptMapper deptMapper){
Dept dept = new Dept();
dept.setDeptNo(11);
dept.setDname("ABKing");
dept.setLoc("上海");
int count = deptMapper.update(dept);
System.out.println(count);
}
public static void findById(DeptMapper deptMapper){
Dept dept = deptMapper.findById(11);
System.out.println(dept);
}
public static void delete(DeptMapper deptMapper){
int count = deptMapper.delete(11);
System.out.println(count);
}
public static void findByIdDname(DeptMapper deptMapper){
Map map = new HashMap();
map.put("deptNo", "11");
map.put("dname", "ABKing");
Dept dept = deptMapper.findByIdDname(map);
System.out.println(dept);
}
}
看30行和32行,能看到调用了两次findById(deptMapper),运行,查看控制台的日志(需要log4j包)
DEBUG [main] - ==> Preparing: select deptNo, dname, loc from Dept where deptNo=?
只打印出了一条SQL语句
而如果我们在31行加入sqlSession.clearCache();
package top.bigking; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import top.bigking.dao.DeptMapper;
import top.bigking.dao.UserMapper;
import top.bigking.pojo.Dept;
import top.bigking.pojo.User; import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class mybatisStudy_start {
public static void main(String[] args) throws Exception{
InputStream in = Resources.getResourceAsStream("mybatisStudy-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession(true);//true 表示 自动提交
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
List<Dept> deptList = deptMapper.query();
for(Dept dept : deptList){
System.out.println(dept);
}
System.out.println("-----------------------------------");
//insert(deptMapper);
//update(deptMapper);
findById(deptMapper);
sqlSession.clearCache();
findById(deptMapper);
//delete(deptMapper);
//findByIdDname(deptMapper); // UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// List<User> userList = userMapper.query();
// for(User user : userList)
// System.out.println(user);
sqlSession.close(); }
private static void insert(DeptMapper deptMapper){
Dept dept = new Dept();
dept.setDeptNo(11);
dept.setDname("ABKing");
dept.setLoc("111");
int count = deptMapper.insert(dept);
System.out.println(count);
}
public static void update(DeptMapper deptMapper){
Dept dept = new Dept();
dept.setDeptNo(11);
dept.setDname("ABKing");
dept.setLoc("上海");
int count = deptMapper.update(dept);
System.out.println(count);
}
public static void findById(DeptMapper deptMapper){
Dept dept = deptMapper.findById(11);
System.out.println(dept);
}
public static void delete(DeptMapper deptMapper){
int count = deptMapper.delete(11);
System.out.println(count);
}
public static void findByIdDname(DeptMapper deptMapper){
Map map = new HashMap();
map.put("deptNo", "11");
map.put("dname", "ABKing");
Dept dept = deptMapper.findByIdDname(map);
System.out.println(dept);
}
}
此时查看控制台的日志
DEBUG [main] - ==> Preparing: select deptNo, dname, loc from Dept where deptNo=?
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <== Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - ==> Preparing: select deptNo, dname, loc from Dept where deptNo=?
可以看到打印出了两条SQL语句,这足以证明,使用了sqlSession.clearCache();之后,缓存被清空了
接下来证明二级缓存:
应当明确的是,二级缓存是mapper级别的缓存,使用同一个Mapper.class的的Mapper类和SqlSession类都共用同一个二级缓存
代码如下:
package top.bigking; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import top.bigking.dao.DeptMapper;
import top.bigking.dao.UserMapper;
import top.bigking.pojo.Dept;
import top.bigking.pojo.User; import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class mybatisStudy_start {
public static void main(String[] args) throws Exception{
InputStream in = Resources.getResourceAsStream("mybatisStudy-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession(true);//true 表示 自动提交
SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
DeptMapper deptMapper1 = sqlSession1.getMapper(DeptMapper.class);
List<Dept> deptList = deptMapper.query();
for(Dept dept : deptList){
System.out.println(dept);
}
System.out.println("-----------------------------------");
//insert(deptMapper);
//update(deptMapper);
findById(deptMapper);
sqlSession.close();
//sqlSession.clearCache();
findById(deptMapper1);
sqlSession1.close();
//delete(deptMapper);
//findByIdDname(deptMapper); // UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// List<User> userList = userMapper.query();
// for(User user : userList)
// System.out.println(user);
//sqlSession.close(); }
private static void insert(DeptMapper deptMapper){
Dept dept = new Dept();
dept.setDeptNo(11);
dept.setDname("ABKing");
dept.setLoc("111");
int count = deptMapper.insert(dept);
System.out.println(count);
}
public static void update(DeptMapper deptMapper){
Dept dept = new Dept();
dept.setDeptNo(11);
dept.setDname("ABKing");
dept.setLoc("上海");
int count = deptMapper.update(dept);
System.out.println(count);
}
public static void findById(DeptMapper deptMapper){
Dept dept = deptMapper.findById(11);
System.out.println(dept);
}
public static void delete(DeptMapper deptMapper){
int count = deptMapper.delete(11);
System.out.println(count);
}
public static void findByIdDname(DeptMapper deptMapper){
Map map = new HashMap();
map.put("deptNo", "11");
map.put("dname", "ABKing");
Dept dept = deptMapper.findByIdDname(map);
System.out.println(dept);
}
}
在DeptMapper.xml中
<select id="findById" resultType="Dept" useCache="true">
select deptNo, dname, loc from Dept where deptNo=#{deptNo}
</select>
通过useCache可以使用二级缓存
在mybatisStudy-config.xml中,在properties标签下,添加
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
这是全局设置,表示开启二级缓存
查看控制台输出的日志:
DEBUG [main] - ==> Preparing: select deptNo, dname, loc from Dept where deptNo=?
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <== Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a9273a8]
可以看到,由于使用了二级缓存,我们的日志中,只出现了一条SQLQL语句
而如果我们把DeptMapper.xml中的代码改为:
<select id="findById" resultType="Dept" useCache="false">
select deptNo, dname, loc from Dept where deptNo=#{deptNo}
</select>
表示不使用二级缓存
运行,查看控制台:
DEBUG [main] - ==> Preparing: select deptNo, dname, loc from Dept where deptNo=?
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <== Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a9273a8]
DEBUG [main] - Returned connection 2056418216 to pool.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Checked out connection 2056418216 from pool.
DEBUG [main] - ==> Preparing: select deptNo, dname, loc from Dept where deptNo=?
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <== Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a9273a8]
可以看到两条SQL语句
可是我们的代码中,并没有使用到清空一级缓存的函数sqlSession.clearCache();
由此可证明,这里是二级缓存的开关
参考:
https://m.w3cschool.cn/kzsow/kzsow-qmod2gri.html
https://www.cnblogs.com/happyflyingpig/p/7739749.html
https://www.cnblogs.com/yuluoxingkong/p/8205858.html
https://www.cnblogs.com/charlypage/p/9747145.html
----------------------------------------------------------------------------
大家好,我是ABKing
金麟岂是池中物,一遇风云便化龙!
欢迎与我交流技术问题
mybatis一级缓存和二级缓存的使用的更多相关文章
- [原创]关于mybatis中一级缓存和二级缓存的简单介绍
关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...
- MyBatis 延迟加载,一级缓存,二级缓存设置
什么是延迟加载 resultMap中的association和collection标签具有延迟加载的功能. 延迟加载的意思是说,在关联查询时,利用延迟加载,先加载主信息.使用关联信息时再去加载关联信息 ...
- mybatis高级(3)_延迟加载_深度延迟_一级缓存_二级缓存
设置延迟加载需要在mybatis.xml中设置 注: 侵入式延迟加载为真时是延迟加载 侵入式延迟加载为假时是深度延迟加载 <!-- 延迟加载和深度延迟加载 --> <settings ...
- 9.Mybatis一级缓存和二级缓存
所谓的缓存呢?其实原理很简单,就是在保证你查询的数据是正确的情况下,没有去查数据库,而是直接查找的内存,这样做有利于缓解数据库的压力,提高数据库的性能,Mybatis中有提供一级缓存和二级缓存. 学习 ...
- 八 mybatis查询缓存(一级缓存,二级缓存)和ehcache整合
1 查询缓存 1.1 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存.
- myBatis学习(9):一级缓存和二级缓存
正如大多数持久层框架一样,MyBatis同样提供了一级缓存和二级缓存的支持 1. MyBatis一级缓存基于PerpetualCache的HashMap本地缓存,其存储作用域为 Session,默认情 ...
- mybatis 详解(九)------ 一级缓存、二级缓存
上一章节,我们讲解了通过mybatis的懒加载来提高查询效率,那么除了懒加载,还有什么方法能提高查询效率呢?这就是我们本章讲的缓存. mybatis 为我们提供了一级缓存和二级缓存,可以通过下图来理解 ...
- MyBatis从入门到放弃六:延迟加载、一级缓存、二级缓存
前言 使用ORM框架我们更多的是使用其查询功能,那么查询海量数据则又离不开性能,那么这篇中我们就看下mybatis高级应用之延迟加载.一级缓存.二级缓存.使用时需要注意延迟加载必须使用resultMa ...
- Mybatis第八篇【一级缓存、二级缓存、与ehcache整合】
Mybatis缓存 缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题. myba ...
- MyBatis 一级缓存,二级缓存,延迟加载设置
1 什么是延迟加载 resultMap中的association和collection标签具有延迟加载的功能. 延迟加载的意思是说,在关联查询时,利用延迟加载,先加载主信息.使用关联信息时再 ...
随机推荐
- thinkphp一对多关系
兹有用户表user和评论表comment 一对一 public function returnmany() { return $this->hasOne('commnet','uid','use ...
- Docker(五):Dockerfile
我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dockerfile 是镜像和容器的关键,Dockerfile 可以非常容易的定义镜像内容,同时在我们后期的微服务实践中,Docke ...
- 【leetcode】1213.Intersection of Three Sorted Arrays
题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...
- Linux设置程序开机自启动
注意: 作者测试时,Linux版本为RedHat6,同时应用在CentOS6应该也可以(作者未实测,但有同事在CentOS6上使用可行),系统版本的不同,可能造成操作上的差异(CentOS7就与Cen ...
- Codeforces 912D Fishs ( 贪心 && 概率期望 && 优先队列 )
题意 : 给出一个 N * M 的网格,然后给你 K 条鱼给你放置,现有规格为 r * r 的渔网,问你如果渔网随意放置去捕捞小鱼的情况下,捕到的最大期望值是多少? 分析 : 有一个很直观的想法就是 ...
- C++中string常用函数用法总结
string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<string>,注意不是<string.h>,下面记录一下string中比较常用的 ...
- opengl中相关的计算机图形变换矩阵之:齐次坐标 (摘编)
模型视图变换(几何变换)矩阵: 1. 齐次坐标:两条平行线也可以相交. 在欧几里得空间中,两条平行线是无法相交的,但是在投影空间(Projective Space)这条定理就不再适用了. 比如上图中, ...
- 第三周syh
第三周作业 7-1 判断上三角矩阵 (15 分) 上三角矩阵指主对角线以下的元素都为0的矩阵:主对角线为从矩阵的左上角至右下角的连线. 本题要求编写程序,判断一个给定的方阵是否上三角矩阵. 输入格 ...
- jquery.fileupload-image-editor.js
jquery.fileupload-image-editor.js中 _initEventHandlers: function () { this._super(); var handlers = { ...
- ffmpeg保持原视频画面比例 自动添加黑边
ffmpeg保持原视频画面比例 自动添加黑边 例如源是1280*528要转成640*480要保持画面比例实际上应该640*264 所以需要在上下都加黑边 ffmpeg -i d:/Media/e.f4 ...