二级缓存原理:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApAAAAF4CAIAAABsKF51AAAf90lEQVR4nO3dIXTbyhaFYcMHAwsLLzT0MgoMDCws8gosDItZYcwKCwPDXFhYWBhgEFgYGKgHktiSRtIcSaOZPfL/gbtaRT469Zxoy7JvsigAAIC8ReoGAACAH4ENAEAGCGwAADJAYAMAkAECGwCADBDYAABkgMAGACADBDYAABkgsAEAyACBDQBABghsAAAyQGADAJABAhsAgAwQ2AAAZIDABgAgAwQ2AAAZILABAMgAgQ0AQAYIbAAAMhAysJfL5QIAADiWy+XIkA0Z2IsFr9cBAGgwPiIJbAAAJkdgAwCQAQIbAIAMENgAAGSAwAYAIAMENgAAGSCwAQDIAIENAEAGCGwAADJAYAMAkAECGwCADBDYAABkgMAGACADBDYAABkgsAEAyACBDQBABghsIF+H3Xqx3h1qWzb7sXX3m8ViUavctR1ABAQ2kK+JAvuwWy/W6/WiXmm/WazX9SOmsd1uU7cAxEZgA/nyBPZht168O257e5Fc3VYcduv1bv+292b/XnZfy/630vtN6YgNBzjs1uvd4XiU475t240t1RHYOEMENpCvzsBueLFd3n+/qaRsORffd6tWf0/qUmDvN8eHnIpVSpUqdG03tFQU1ZAmsHGGCGwgX97AriZeNcLbs/1YtprO9U2NjbjH6NxubanYbrcENs4cgQ3ky/ce9sct6/d9Sveeq7eg2wL7+Jr3tEMlsCsFm4L5tHvLdltLW0egJxDICYENZKx8E7l5Q1F6qd36ibTWwD69cf3x9VNgl68WAr3Cbm+p/gobOEMENpCz/abhzeeGnU4vmJs+490e2B8v0o+POgV2KcUPu3X5FXY53Rvfwy5tN7ZUFNwGx9kjsIHMle8qt3z4urS54S52Z2DXI7V8S/xUa7Op3PreHT89XrmWaNpubAkAgQ0gpLagJYCBsQhsAAER2MBUCGwAADJAYAMAkAECGwCADBDYAABkgMAGACADBDYAABkgsAEAyACBDQBABghsAAAyQGADAJABAhsAgAwQ2AAAZIDABgAgAwQ2AAAZILABAMgAgQ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yoBXY/O5qAAAajf9l27wmBgAgAwQ2AAAZILABAMgAgQ0AQAYIbAAAMkBgAwCQgf8Dahc9npduo/UAAAAASUVORK5CYII=" alt="" />

首先开启mybatis的二级缓存。

sqlSession1去查询用户id为1的用户信息,查询到用户信息会将查询数据存储到二级缓存中。

如果SqlSession3去执行相同 mapper下sql,执行commit提交,清空该 mapper下的二级缓存区域的数据。

sqlSession2去查询用户id为1的用户信息,去缓存中找是否存在数据,如果存在直接从缓存中取出数据。

二级缓存与一级缓存区别,二级缓存的范围更大,多个sqlSession可以共享一个UserMapper的二级缓存区域。

UserMapper有一个二级缓存区域(按namespace分) ,其它mapper也有自己的二级缓存区域(按namespace分)。

每一个namespace的mapper都有一个二缓存区域,两个mapper的namespace如果相同,这两个mapper执行sql查询到数据将存在相同 的二级缓存区域中。

准备工作:

开启二级缓存

mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓存。

在核心配置文件SqlMapConfig.xml中加入

<setting name="cacheEnabled" value="true"/>

 

描述

允许值

默认值

cacheEnabled

对在此配置文件下的所有cache 进行全局性开/关设置。

true false

true

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAb0AAABLCAIAAABr8M6jAAAJH0lEQVR4nO2dsW7juhKG9Tx5BDd+ifRpDL9DigO7ce9GyDZp0wipDuDWUJDqAFuuGhebNMHFqrs49Z1byJZIiqRESZZl5/swRRyK5Iimfg1JmYoEAABCiC7tAADAlYFuAgCEgW4CAISBbgIAhIFuAgCEcQ26+fPX/GG/+XlpNwAAROQ6dPPrYzE93Zw/7M+d3XrM/GFf/39PZwAgiInp5tfH4uHXu+WfZ9fNQo+s5snSpzprpY3HDO5JA18fi4d/Xr7OVTzANTIl3fz5a/6wX/z9r/n/EeNNVYA84Z5HXtvob9DfpXoaSZ6K/HIfyvuPPfMkACpT0c3Pv/9xXpwT083GjC2rcAmi65j68aFVd6b4diy3NIBvySR08/3Hfu4ZDI44VB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JzzSer+KXb/QrTYCpcZYfpBugm/0wx+n8Egng9kE3AQDCQDcBAMJANwEAwkA3AQDCQDcBAMLoqZtjLPn7+H7bswPAxemnm0PtQc9rLQDgemihm+53V5h70Hdm4tuzD/TbLAC4DZp007PXxoBqMnHdHGgvAAC4DXy66d/bzboH/VW/1sLn/BB76wPAbeDUTd9ewiL2HUeu+rUWbQLbfnudAsBtYNVN77srCqwrQlf9Wou2u7V331sfAG6DbvGm+/Gjq36thd159QDiTQDoNr/ZZkXoql9roTlfVjXc690B4Jrpsp7u2YNe4apfa2FOLLCeDgAlHZ7fdO5Bf9WvtfA5z/ObAKAQ/HuhEfagPxu81gIABiBUNy/9g/Q+8FoLABiCb7YfEq+1AIDefDPdBADozVE3//Pf/2EYhmF+QzcxDMPCDN3EMAwLM3QTwzAszNBNDMOwMEM3MQzDwgzdxDAMCzN0E8MwLMzQTQzDsDBDNzEMw8IM3cQwDAszdBPDMCzM0E0Mw7AwQzcxDMPCTNNNAABoCboJABDG/wHxNhX9J56wvwAAAABJRU5ErkJggg==" alt="" />

在UserMapper.xml中开启二缓存,UserMapper.xml下的sql执行完成会存储到它的缓存区域(HashMap)。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlwAAABfCAIAAACydY5gAAAQe0lEQVR4nO2dTW/bxhaG2R/QdZd373VWWlzdH+FdUHgj6B9koUVAoYBXBQwvqjoLe1VACKyki7pVYRitrlzF+bKb6ySWAwux5CQ2BNFVE9WRY9fp3AWH5MxwZkjK+iCd98FBIHE4wzNH1Lw8MyPHIAAAAAAghBBiTNoBAAAAIC4EiOLz/z0Yjx8AAADAxIEoAgAAABSIIgAAAECBKAIAAAAUiCIAAABAgSgCAAAAFIgiAAAAQIEoAgAAABQqih/OzjvH72qb9Z9+fVzbrHeO3304OycQxU+VSpZMGWSpQUiZTBkkVx7t5ZrzZMog0/OjvUpcKJMpg1SG3eqnFUMQmjF/l68AVBS3dprbe68Pu73e+cfDbm9779XWTvP07Byi+GmylCJ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RBEGg8GSaBDFwdE4D1GEwWCwJBpEcXA0zkMUYTAYLIkGUaQUF8304urlRLHz1Tf5a2udNkQRBoPBkmkQxeGJ4u93jPydYu+fNkQRBoPBkmnJEcVW9Vr+TjHGoli8TdPEti2K//r3tfzCV63Jf8YwGAwGC2kJEcXf7xh5KjkxFcVWlZVAO1Ms3s4b+fyXv0/+Y4bBYDBYGEuAKD5YW2ClhRHFjbk5M21Su7684enV6qJ7PD23/AunZfJatij+sjwnr8U26Ggn6+2DtQXjdt19606f2s67GSQMBoPB4mxxF8Xi7bzBT0I6orh6w1TkdhvL173jwmnKWsVFVvBWbzB6+cvyXNpcLLKaurjKi2L9Sz4j5NYUW9Vr+TwrmTAYDAaLp8VaFL/6Ju9uXWGPU6ESU0A57LyoppYwfcq83Zib86ehi0VWFJktNrb5NtrUv8znjW+qDyb9ecNgMBhMY7EWxbY6U9QuAXITpOyEp6aWWhRXb5h8a6bJi6L3SwzXkCnCYDBYEi3uothWrCkWF/2LhYwiMkWs2qlrBYjiDZmSUlf5LTa2eaLIbBGCwWAwWMwtAaLY7sl2n24sXxc217Ci6MqbvUHGfauspRFFYU1RFMXibUkWiN2nMBgMlkRLiCj2ZL9T3Fi+bsp2n7LH55aLy3PclKmilkYUHV0U52PbvX/8W2xsw+8UYTAYLImWHFFkjkumMidB257alW2fwV+0gcFgsCQaRHFwpFtsbIMowmAwWBINojg4GuchijAYDJZEi7Uoqpi0GlI0zkMUYTAYLIkWTRQBAACAKw9EEQAAAKBAFAEAAAAKRBEAAACgQBQBAAAACkQRAAAAoEAUAQAAAApEEQAAAKBAFAEAAAAKRBEAAACgQBQBAAAASoAoAgAAAJ8OEEUAAACA8n++yURt/vO3LwAAAABJRU5ErkJggg==" alt="" />

测试:

第一步:在ordersMapperCustom.xml中开启二级缓存:

<mapper namespace="cn.itcast.mybatis.mapper.OrdersMapperCustom">
<cache></cache>

第二步:编写pojo类(要实现Serializable)必须要实现的:

为什么要实现Serializable接口呢:

为了将缓存数据取出执行反序列化操作,因为二级缓存数据存储介质多种多样,不一样在内存。

package cn.itcast.mybatis.po;

import java.io.Serializable;
import java.util.Date;
import java.util.List; public class User implements Serializable {
private int id;//对应数据库中主键
private String username;//对应数据库中用户的名称
private Date birthday;//对应数据库中的生日
private String sex;//性别
private String address;//地址
//一个用户对应多个订单表
private List<Orders> ordersList;
public List<Orders> getOrdersList() {
return ordersList;
}
public void setOrdersList(List<Orders> ordersList) {
this.ordersList = ordersList;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} }

第三步;编写测试代码:

package cn.itcast.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List; 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 org.junit.Before;
import org.junit.Test; import cn.itcast.mybatis.mapper.OrdersMapperCustom;
import cn.itcast.mybatis.mapper.userMapper;
import cn.itcast.mybatis.po.Orders;
import cn.itcast.mybatis.po.User;
import cn.itcast.mybatis.po.UserCustom;
import cn.itcast.mybatis.po.UserQueryVo; public class Mybatis_mappertest { private SqlSessionFactory sqlSessionFactory;
@Before
public void setup() throws IOException
{ String resource="SqlMapConfig.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
//主要是生成SqlsessionFactory。
this.sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testMaper()
{
SqlSession sqlSession1=null;
SqlSession sqlSession2=null;
sqlSession1=sqlSessionFactory.openSession();
sqlSession2=sqlSessionFactory.openSession();
//生成代理类
OrdersMapperCustom orderMapper=sqlSession1.getMapper(OrdersMapperCustom.class); OrdersMapperCustom orderMapper2=sqlSession2.getMapper(OrdersMapperCustom.class); User user=orderMapper.finduserByid(1);
//这个close必须要加,不然缓存存不进去。
sqlSession1.close(); User user2=orderMapper2.finduserByid(1);
sqlSession2.close(); } }

运行结果:DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@5bae4145]
DEBUG [main] - ==>  Preparing: SELECT * FROM USER WHERE ID=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@5bae4145]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@5bae4145]
DEBUG [main] - Returned connection 1538146629 to pool.
DEBUG [main] - Cache Hit Ratio [cn.itcast.mybatis.mapper.OrdersMapperCustom]: 0.5

发现只有一条select语句,说明在执行第二条 User user2=orderMapper2.finduserByid(1)时,二级缓存发挥了作用。

我们注意到:

 //生成两个sqlsession。因为二级缓存,多个sqlSession可以共享一个UserMapper的二级缓存区域。UserMapper有一个二级缓存区域(按namespace分)      
SqlSession sqlSession1=null;
SqlSession sqlSession2=null;
sqlSession1=sqlSessionFactory.openSession();
sqlSession2=sqlSessionFactory.openSession();
//生成代理类
OrdersMapperCustom orderMapper=sqlSession1.getMapper(OrdersMapperCustom.class); OrdersMapperCustom orderMapper2=sqlSession2.getMapper(OrdersMapperCustom.class); User user=orderMapper.finduserByid(1); User user2=orderMapper2.finduserByid(1);
这里的orderMapper,orderMapper2是不同的sqlSession生成的。但是没关系,二级缓存的作用范围是1.跨sqlSession.  2.

每一个namespace的mapper都有一个二缓存区域,两个mapper的namespace如果相同,这两个mapper执行sql查询到数据将存在相同 的二级缓存区域中。

--------------------------------------------------------------------------------------------------------------------------------------------------------------

清空缓存的操作:

// 二级缓存测试
@Test
public void testCache2() throws Exception {
SqlSession sqlSession1 = sqlSessionFactory.openSession();
SqlSession sqlSession2 = sqlSessionFactory.openSession();
SqlSession sqlSession3 = sqlSessionFactory.openSession();
// 创建代理对象
UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
// 第一次发起请求,查询id为1的用户
User user1 = userMapper1.findUserById(1);
System.out.println(user1); //这里执行关闭操作,将sqlsession中的数据写到二级缓存区域
sqlSession1.close(); //使用sqlSession3执行commit()操作
UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);
User user = userMapper3.findUserById(1);
user.setUsername("张明明");
userMapper3.updateUser(user);
//执行提交,清空UserMapper下边的二级缓存
sqlSession3.commit();
sqlSession3.close(); UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
// 第二次发起请求,查询id为1的用户,因为前面的操作清除了缓存,所以这里的操作会重新从数据库中去查询。
User user2 = userMapper2.findUserById(1);
System.out.println(user2); sqlSession2.close(); }

---------------------------------------------------------------------------------------------------------------------------------------------------------

有没有这样的一种情况,一个mapper.xml中,有些sql语句是要缓存功能的,但是有些sql语句我不想他具有缓存功能,需要每次都从数据库中得到数据?可以

在statement中设置useCache=false可以禁用当前select语句的二级缓存,即每次查询都会发出sql去查询,默认情况是true,即该sql使用二级缓存。

<select id="findOrderListResultMap" resultMap="ordersUserMap" useCache="false">

总结:针对每次查询都需要最新的数据sql,要设置成useCache=false,禁用二级缓存。

这样的话这个mapper.xml中别的sql语句具有二级缓存的功能,这句sql语句就没有了缓存功能,每次读取数据时都要从数据库中去读数据。

--------------------------------------------------------------------------------------------------------------------------------------------------------

刷新缓存(就是清空缓存)

在mapper的同一个namespace中,如果有其它insert、update、delete操作数据后需要刷新缓存,如果不执行刷新缓存会出现脏读。

设置statement配置中的flushCache="true" 属性,默认情况下为true即刷新缓存,如果改成false则不会刷新。使用缓存时如果手动修改数据库表中的查询数据会出现脏读。

如下:

<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User" flushCache="true">

总结:一般下执行完commit操作都需要刷新缓存,flushCache=true表示刷新缓存,这样可以避免数据库脏读。

28Mybatis_查询缓存-二级缓存-二级缓存测试-的更多相关文章

  1. mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存

    关本文是Mybatis基础系列的第四篇文章,点击下面链接可以查看前面的文章: mybatis基础系列(三)——动态sql mybatis基础系列(二)——基础语法.别名.输入映射.输出映射 mybat ...

  2. hibernate笔记--缓存机制之 二级缓存(sessionFactory)和查询缓存

    二级缓存(sessionFactory): Hibernate的二级缓存由SessionFactory对象管理,是应用级别的缓存.它可以缓存整个应用的持久化对象,所以又称为“SessionFactor ...

  3. [原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓存,时间戳缓存

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. 01-08-05【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate二级缓存:第三方MemCache缓存

    一.准备工作 [1]根据操作系统(位数)选择下载相应版本的MemCache, MemCache的下载和安装,参看: http://www.cnblogs.com/easy5weikai/p/37606 ...

  5. mybatis的缓存机制(一级缓存二级缓存和刷新缓存)和mybatis整合ehcache

    1.1  什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时需要构造 s ...

  6. MyBatis总结八:缓存介绍(一级缓存,二级缓存)

    简介 为数据库的查询进行缓存,是减少数据库压力的主要途径.分为一级缓存和二级缓存. 一级缓存:session级别缓存,作用于当前会话. 二级缓存:SessionFactory级别缓存,作用于整个Ses ...

  7. MyBatis(七):MyBatis缓存详解(一级缓存/二级缓存)

    一级缓存 ​ MyBatis一级缓存上SqlSession缓存,即在统一SqlSession中,在不执行增删改操作提交事务的前提下,对同一条数据进行多次查询时,第一次查询从数据库中查询,完成后会存入缓 ...

  8. Hibernate缓存策略(一级缓存和EHcache二级缓存)

    如何配置二级缓存: 第一步:导入EHcache依赖 1)Maven项目: <!--此处使用hibernate4--> <dependency> <groupId>o ...

  9. mybatis源码分析(7)-----缓存Cache(一级缓存,二级缓存)

    写在前面  MyBatis 提供查询缓存,用于减轻数据库压力,提高数据库性能. MyBatis缓存分为一级缓存和二级缓存. 通过对于Executor 的设计.也可以发现MyBatis的缓存机制(采用模 ...

随机推荐

  1. leetCode(29):Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  2. win10 显示详细信息窗格

      win10 显示详细信息窗格 CreateTime--2018年5月26日09点13分 Author:Marydon 1.说明: win10无法像win7那样将详细信息窗格显示在窗口的底部,只能显 ...

  3. HDU1823-Luck and Love-二维线段树(模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1823 好吧,给这题跪了. ..orz.... 一道非常基础的二维线段树的模板题: 可是细节非常多:尤 ...

  4. myeclipse2014安装jad反编译插件

    myeclipse上默认不能查看class文件,需要查看的话安装反编译插件 安装步骤: 准备图中框里的两个文件 1. [net.sf.jadclipse_3.3.0.jar]文件拷贝到如下路径([D: ...

  5. Linux下查看磁盘挂载的三种方法

    Linux下查看磁盘挂载的三种方法 2009-06-05 23:17 好久没有更新日志了,呵呵.不是没有要写的东东.实在抽不出时间来写,要准备公司的考试呢,C++考试.已经有七个月没有写C++代码了, ...

  6. MySQL学习总结(二)数据库以及表的基本操作

    上一节中详细的介绍了关于MySQL数据库的安装过程,接下来我们就该对数据库以及表进行一些基本的操作了. 1.数据类型 MySQL数据库中提供了整数类型.浮点数类型.定点数类型.日期和时间类型.字符串类 ...

  7. 关于python 中的 sys.argv 的使用方法

    sys.argv是获取在cmd运行python文件的时候输入的命令行参数,呈现的数据结构是列表的格式 1.用pacharm时运行时的结果是: 输出结果: 2.当我在cmd中输入指令      debu ...

  8. 让HttpClient不要打印巨多的日志

    让HttpClient不要打印巨多的日志 log4j.logger.org.apache.commons.httpclient=DEBUG 參考http://hc.apache.org/httpcli ...

  9. HDU 5321 Beautiful Set 漂亮集合

    题意:给定一个集合,含有n个数.浙理工先生和杭电先生各自有计算这个集合漂亮值的方法. 浙理工先生的计算方法是:对于这个n个数的某个排列,此排列的漂亮值为这个排列全部的区间最大公约数之和.然后这个集合的 ...

  10. jquery获取元素索引值index()

    jquery获取元素索引值index()方法实例. jquery获取元素索引值index()方法: jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 ...