一级缓存

测试案例:

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缓存结构的更多相关文章

  1. MyBatis缓存结构

    Mybatis Cache结构图: CacheKey(statementId, sql, sqlParams,other). 上图展示了Mybatis Cache的结构: 1)每个Mapper对应一块 ...

  2. 第三章 - CPU缓存结构和java内存模型

    CPU 缓存结构原理 CPU 缓存结构 查看 cpu 缓存 速度比较 查看 cpu 缓存行 cpu 拿到的内存地址格式是这样的 CPU 缓存读 根据低位,计算在缓存中的索引 判断是否有效 0 去内存读 ...

  3. 第六章 mybatis注入映射器

    为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:Map ...

  4. csapp第六章笔记-存储器结构

    目录 随机访问存储器(Random-Access-Memory) 静态RAM 动态RAM 增强的DRAM 非易失性存储器 磁盘存储 磁盘构成 磁盘容量 磁盘操作 逻辑磁盘块 访问磁盘和连接I/O设备 ...

  5. 【转】MaBatis学习---源码分析MyBatis缓存原理

    [原文]https://www.toutiao.com/i6594029178964673027/ 源码分析MyBatis缓存原理 1.简介 在 Web 应用中,缓存是必不可少的组件.通常我们都会用 ...

  6. Java EE数据持久化框架 • 【第5章 MyBatis代码生成器和缓存配置】

    全部章节   >>>> 本章目录 5.1 配置MyBatis Generator 5.1.1 MyBatis Generator介绍 5.1.2 MyBatis Generat ...

  7. OpenGL ES着色器语言之语句和结构体(官方文档第六章)内建变量(官方文档第七、八章)

    OpenGL ES着色器语言之语句和结构体(官方文档第六章) OpenGL ES着色器语言的程序块基本构成如下: 语句和声明 函数定义 选择(if-else) 迭代(for, while, do-wh ...

  8. MyBatis的学习总结六:Mybatis的缓存【参考】

    一.Mybatis缓存介绍 正如大多数持久层框架一样,Mybatis同样提供了一级缓存和二级缓存 1.一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为Session, ...

  9. 第十六章 综合实例——《跟我学Shiro》

    简单的实体关系图 简单数据字典 用户(sys_user) 名称 类型 长度 描述 id bigint 编号 主键 username varchar 100 用户名 password varchar 1 ...

随机推荐

  1. vue中echarts随窗体变化

    <div id="myChart" :style="{width: '100%', height: '345px'}"></div> & ...

  2. spring历史和哲学

    spring 历史: 2004年 Spring Framework 1.0 final 正式问世. 1.在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分 ...

  3. springboot自定义异常

    SpringBoot自定义异常以及异常处理 在web项目中,我们可能需要给前端返回不同的提示码.例如:401表示没有权限,500代表位置异常,200代表请求成功等.但是这些提示码远远不能满足我们返回给 ...

  4. 监听域对象创建和销毁的Listener

    1.什么是Servlet监听器? 先来看看什么是监听器.监听器是专门用于对其它对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时立即采取相应的行动.Servlet监听器是S ...

  5. ccf-201809-2 买菜

    问题描述 小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一些菜然后去旁边的一个广场把菜装上车,两人都要买n种菜,所以也都要装n次车.具体的,对于小H来说有n个不相交的时间段 ...

  6. JavaScript中callee,caller,argument的理解

    argument代表当前函数的参数数组: 1.callee的用法: argument.callee表示谁引用的这个函数 其他解释:(arguments.callee表示引用当前正在执行的函数,或者说是 ...

  7. javascript时间格式转换(今天,昨天,前天)

    function transDate() { var $time =document.getElementById("share-time"); var date = $time. ...

  8. Makefile一 头文件及库搜索路径

    头文件及库搜索路径 头文件的搜索路径: 头文件的搜索规则是:找到就使用,停止继续往下寻找 1: #include “mytest.h” 搜索的顺序为: (1)先搜索当前目录 (2)然后搜索编译时 -I ...

  9. Windows API-----top level window

    原文地址: http://blog.163.com/cumt_xl/blog/static/19071504420136911838683/ Q: What is a top-level window ...

  10. arm汇编学习(三)

    一.ndk编译android上运行的c程序 新建个hello目录,底下要有jni目录,下面就是Android.mk文件 1.Android.mk文件内容如下: LOCAL_PATH:= $(call ...