第六章.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 ...
随机推荐
- easyui导出当前datagrid数据(含表头)
JS代码 //导出当前DataGrid数据 function doExportCommon() { var list = getCheckedRowCommon(); var exportList = ...
- Ubuntu17.10下编译Openjdk8u
一开始笔者用的系统和软件版本都是最新的,导致编译了好几次都失败,最后找到解决的办法,现在记录一下编译及解决的方法 避免以后忘记 所用操作系统 Ubuntu17.10 所用软件及版本 make 3.8. ...
- 一:SpringMVC架构流程
架构流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. 3.处理器映射器根据请求url ...
- javaweb之jsp的九个隐含对象与基本语法
1.在页面上可以不用声明直接使用的对象称为jsp页面的隐含对象.使用<% %>编写的java代码在_jspService方法中,如下: public void _jspService(fi ...
- sql:function
--查询权限函数 --1 declare @names varchar(3000) set @names='' select @names=@names+isnull(AdminPermissForm ...
- angular之$on、$emit、$broadcast
1.$scope.$on view事件 //View被加载但是DOM树构建之前时: $scope.$on('$viewContentLoading', function(event, viewConf ...
- iview中事件获取自定义参数
前提:页面渲染多个cascaer组件,根据不同cascader组件选中的值,加载不同内容 解决:此时需要解决的问题是,在on-change事件中返回index索引使用如下格式: <Cascade ...
- AngularJS实现原理
个人觉得,要很好的理解AngularJS的运行机制,才能尽可能避免掉到坑里面去.在这篇文章中,我将根据网上的资料和自己的理解对AngularJS的在启动后,每一步都做了些什么,做一个比较清楚详细的解析 ...
- EasyUI combobox 加载JSON数据
Action返回 JSON 格式如下: jsonResult = { total=7,rows=[ {TEXT=技术支持, ID=402894ca4419acf1014419b148a10000}, ...
- FeatureLayer 里属性数据的提取与显示
我们用工程文件所发布的WebServer下,包含一个个图层,这些图层根据顺序进行了 0 开始的编号,这些就是FeatureLayer的地址了! FeatureLayer 包含了地图的属性信息,十分好用 ...