一级缓存

测试案例:

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. WPF TextBox 聚焦

    1.利用行为 http://blog.csdn.net/lianchangshuai/article/details/9223125 2. 利用装饰器 http://stackoverflow.com ...

  2. npm proxy报错处理

    npm经常抽风,动不动安装一个模块就这样了: 提示是否设置了正确的代理地址,解决方法网上有很多,有说取消代理.重新设置代理等等,最简单粗暴解决: 删除nodejs安装路径下面的npmrc文件,再使用淘 ...

  3. MyBatis_注解式开发

    一.注解式开发 mybatis的注解主要替换映射文件. 二.基础语法 注解首字母大写,因为注解与类.接口是同一级别的(类同一层级的:类,接口,注解,枚举).一个注解,后台对应着一个@interface ...

  4. LinearLayout中的android:layout_garvity的center_vertical和center_horizontal

    当LinearLayout的排列方向是 horizontal时,只有垂直方向上的对齐方式才会生效.因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对 ...

  5. [Java反射基础二]获取类的信息

    本文接上文“Class类的使用”,以编写一个用来获取类的信息(成员函数.成员变量.构造函数)的工具类来讲解"反射之获取类的信息" 1.获取成员函数信息 /** * 获取成员函数信息 ...

  6. 为什么分布式一定要有redis?(转)

    为什么分布式一定要有redis? 程序员小灰 6天前 点击上方“程序员小灰”,选择“置顶公众号” 有趣有内涵的文章第一时间送达! 作者:孤独烟 来自:http://rjzheng.cnblogs.co ...

  7. JS 面向对象之继承---多种组合继承

    1. 组合继承:又叫伪经典继承,是指将原型链和借用构造函数技术组合在一块的一种继承方式. 下面来看一个例子: function SuperType(name) { this.name = name; ...

  8. docker镜像使用和总结

    一.Docker镜像是什么? 操作系统分为内核和用户空间.在Linux中,内核启动后会挂载 root 文件系统为其提供用户空间支持. docker镜像就相当于一个 root文件系统.比如:官方镜像ub ...

  9. sass判断语句

    @if判断 @if可一个条件单独使用,也可以和@else结合多条件使用 scss.style css.style 三目判断 语法为:if($condition, $if_true, $if_false ...

  10. 表格 滚动条 (tbody部分滚动)

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 html <table> <thead> < ...