第六章.MyBatis缓存结构
一级缓存
测试案例:
MyBatisTest.java
//缓存
@Test
public void testFindCustomerCache1() throws Exception{ SqlSession sqlSession=dataConn.getSqlSession(); //调用userMapper的方法
Customer customer1=sqlSession.selectOne("test.findCustomerById",);
System.out.println("用户姓名:"+customer1.getUsername()); Customer customer2=sqlSession.selectOne("test.findCustomerById",);
System.out.println("用户姓名:"+customer2.getUsername());
sqlSession.close();
}
测试结果:
只查询了一次
DEBUG [main] - ==> Preparing: SELECT * FROM CUSTOMER WHERE cus_id=?
DEBUG [main] - ==> Parameters: (Integer)
DEBUG [main] - <== Total:
用户姓名:Mr
用户姓名:Mr
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]
两次查询之间出现增删该查等情况时,即执行commit()方法。
在UserMapper.xml最后面加上
<update id="updateCustomerAcNo" parameterType="cn.com.mybatis.po.Customer" >
UPDATE CUSTOMER SET acno = #{acno} WHERE cus_id=#{cus_id}
</update>
在MyBatisTest.java中测试
@Test
public void testFindCustomerCache2() throws Exception{ SqlSession sqlSession=dataConn.getSqlSession(); //调用userMapper的方法
Customer customer1=sqlSession.selectOne("test.findCustomerById",);
System.out.println("用户姓名:"+customer1.getUsername()+"|"
+"卡号:"+customer1.getAcno()); String AcNo = "";
customer1.setAcno(AcNo);
System.out.println("修改用户卡号为:"+AcNo);
sqlSession.update("test.updateCustomerAcNo",customer1);
sqlSession.commit(); Customer customer2=sqlSession.selectOne("test.findCustomerById",);
System.out.println("用户姓名:"+customer2.getUsername()+"|"
+"卡号:"+customer2.getAcno()); sqlSession.close();
}
观察结果:
com.mysql.jdbc.JDBC4Connection@42d8062c]
DEBUG [main] - ==> Preparing: SELECT * FROM CUSTOMER WHERE cus_id=?
DEBUG [main] - ==> Parameters: (Integer)
DEBUG [main] - <== Total:
用户姓名:Mr|卡号:
修改用户卡号为:
DEBUG [main] - ==> Preparing: UPDATE CUSTOMER SET acno = ? WHERE cus_id=?
DEBUG [main] - ==> Parameters: (String), (Integer)
DEBUG [main] - <== Updates:
DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]
DEBUG [main] - ==> Preparing: SELECT * FROM CUSTOMER WHERE cus_id=?
DEBUG [main] - ==> Parameters: (Integer)
DEBUG [main] - <== Total:
用户姓名:Mr|卡号:
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]
DEBUG [main] - Returned connection to pool.
二级缓存
检查Customer.java文件
属性以及是否实现序列化接口
public class Customer implements Serializable{
private int cus_id;
private String username;
private String acno;
private String gender;
private String phone;
private List<Batch> batchList;
....
}
在MyBatisTest.java中测试
@Test
public void testFindCustomerOnMapper1() throws Exception{
SqlSession sqlSession=dataConn.getSqlSession(); //获取Mapper代理
CustomerMapper customerMapper1=sqlSession.getMapper(CustomerMapper.class);
//执行Mapper代理对象的查询方法
Customer customer1=customerMapper1.findCustomerById();
System.out.println("用户姓名:"+customer1.getUsername()+"|"
+"卡号:"+customer1.getAcno()); //获取Mapper代理
CustomerMapper customerMapper2=sqlSession.getMapper(CustomerMapper.class);
//执行Mapper代理对象的查询方法
Customer customer2=customerMapper2.findCustomerById();
System.out.println("用户姓名:"+customer2.getUsername()+"|"
+"卡号:"+customer2.getAcno()); sqlSession.close();
}
得到结果:
DEBUG [main] - ==> Preparing: SELECT * FROM CUSTOMER WHERE cus_id=?
DEBUG [main] - ==> Parameters: (Integer)
DEBUG [main] - <== Total:
用户姓名:Mr|卡号:
用户姓名:Mr|卡号:
若二级缓存中两个查询之间多出了commit()方法的执行?
MyBatisTest.java继续测试
@Test
public void testFindCustomerOnMapper2() throws Exception{
SqlSession sqlSession=dataConn.getSqlSession(); //获取Mapper代理
CustomerMapper customerMapper1=sqlSession.getMapper(CustomerMapper.class);
//执行Mapper代理对象的查询方法
Customer customer1=customerMapper1.findCustomerById();
System.out.println("用户姓名:"+customer1.getUsername()+"|"
+"卡号:"+customer1.getAcno()); //获取Mapper代理
CustomerMapper customerMapper2=sqlSession.getMapper(CustomerMapper.class);
String AcNo = "";
customer1.setAcno(AcNo);
//执行Mapper代理对象的修改方法
customerMapper2.updateCustomerAcNo(customer1);
System.out.println("修改用户姓名:"+customer1.getUsername()+"|"
+"的卡号为:"+customer1.getAcno());
sqlSession.commit(); //获取Mapper代理
CustomerMapper customerMapper3=sqlSession.getMapper(CustomerMapper.class);
//执行Mapper代理对象的查询方法
Customer customer3=customerMapper3.findCustomerById();
System.out.println("用户姓名:"+customer3.getUsername()+"|"
+"卡号:"+customer3.getAcno()); sqlSession.close();
}
测试结果:
DEBUG [main] - ==> Preparing: SELECT * FROM CUSTOMER WHERE cus_id=?
DEBUG [main] - ==> Parameters: (Integer)
DEBUG [main] - <== Total:
用户姓名:Mr|卡号:
DEBUG [main] - ==> Preparing: UPDATE CUSTOMER SET acno = ? WHERE cus_id=?
DEBUG [main] - ==> Parameters: (String), (Integer)
DEBUG [main] - <== Updates:
修改用户姓名:Mr|的卡号为:
DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@13c27452]
DEBUG [main] - ==> Preparing: SELECT * FROM CUSTOMER WHERE cus_id=?
DEBUG [main] - ==> Parameters: (Integer)
DEBUG [main] - <== Total:
用户姓名:Mr|卡号:
DEBUG [main] - Resetting autocommit to true on JDBC Connection
第六章.MyBatis缓存结构的更多相关文章
- MyBatis缓存结构
Mybatis Cache结构图: CacheKey(statementId, sql, sqlParams,other). 上图展示了Mybatis Cache的结构: 1)每个Mapper对应一块 ...
- 第三章 - CPU缓存结构和java内存模型
CPU 缓存结构原理 CPU 缓存结构 查看 cpu 缓存 速度比较 查看 cpu 缓存行 cpu 拿到的内存地址格式是这样的 CPU 缓存读 根据低位,计算在缓存中的索引 判断是否有效 0 去内存读 ...
- 第六章 mybatis注入映射器
为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:Map ...
- csapp第六章笔记-存储器结构
目录 随机访问存储器(Random-Access-Memory) 静态RAM 动态RAM 增强的DRAM 非易失性存储器 磁盘存储 磁盘构成 磁盘容量 磁盘操作 逻辑磁盘块 访问磁盘和连接I/O设备 ...
- 【转】MaBatis学习---源码分析MyBatis缓存原理
[原文]https://www.toutiao.com/i6594029178964673027/ 源码分析MyBatis缓存原理 1.简介 在 Web 应用中,缓存是必不可少的组件.通常我们都会用 ...
- Java EE数据持久化框架 • 【第5章 MyBatis代码生成器和缓存配置】
全部章节 >>>> 本章目录 5.1 配置MyBatis Generator 5.1.1 MyBatis Generator介绍 5.1.2 MyBatis Generat ...
- OpenGL ES着色器语言之语句和结构体(官方文档第六章)内建变量(官方文档第七、八章)
OpenGL ES着色器语言之语句和结构体(官方文档第六章) OpenGL ES着色器语言的程序块基本构成如下: 语句和声明 函数定义 选择(if-else) 迭代(for, while, do-wh ...
- MyBatis的学习总结六:Mybatis的缓存【参考】
一.Mybatis缓存介绍 正如大多数持久层框架一样,Mybatis同样提供了一级缓存和二级缓存 1.一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为Session, ...
- 第十六章 综合实例——《跟我学Shiro》
简单的实体关系图 简单数据字典 用户(sys_user) 名称 类型 长度 描述 id bigint 编号 主键 username varchar 100 用户名 password varchar 1 ...
随机推荐
- 第八章使用java实现面向对象-File I/O
java.io.File类用于表示文件(目录) File类只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问 RandomAccessFile java提供的对文件内容的访问,既可以 ...
- js扩展
http://www.css88.com/doc/underscore/#findWhere
- 常用软件下载开发环境七牛镜像Java、Node、Mongo
[jdk1.8] Linux:http://soft.yzeng.cc/jdk18/jdk-8u202-linux-x64.tar.gz Windows:http://soft.yzeng.cc/jd ...
- Javascript制作伸缩的二级菜单
1.javascript方法 <style> #navigation { width: 200px; font-family: Arial; } #navigation > ul { ...
- MySQL 8.0 压缩包版安装方法
转自:https://blog.csdn.net/yangs_2012/article/details/80412016 注意: 操作系统:Windows 10 专业版(64位) MySQL版本:my ...
- JavaScript对HTML字符转义与反转义(转码和解码)
HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...
- 基于svg.js实现对图形的拖拽、选择和编辑操作
本文主要记录如何使用 svg.js 实现对图形的拖拽,选择,图像渲染及各类形状的绘制操作. 1.关于SVG SVG 是可缩放的矢量图形,使用XML格式定义图像,可以生成对应的DOM节点,便于对单个图形 ...
- 使用electron构建跨平台Node.js桌面应用
最近,把团队内经常使用的一个基于Node.js制作的小工具给做成了可视化操作的桌面软件,使用的是electron,这里简单分享一下使用electron的一些经验和心得. 一.如何使用electron把 ...
- easyui window窗口 随body的滚动条 滚动
问题描述: 当easyui window窗口弹出的时候,依然可以滚动body 的滚动条,而且window窗口也会随它一起滚动 思路:bootstrap 模态框弹出的时候,给body 添加了 .moda ...
- ogr2ogr使用
简介 org2ogr是OGR模块中提供的一个重要工具,用于对数据源进行格式转换 使用方式 命令行参数 [xingxing.dxx@30_28_6_20 J50F001020]$ ogr2ogr --l ...