一:MyBatis缓存简介

MyBatis支持声明式数据缓存(declarative data caching)。当一条SQL语句被标记为“可缓存”后,首次执行它时从数据库获取的所有数据会被存储在一段高速缓存中,今后执行这条语句时就会从高速缓存中读取结果,而不是再次命中数据库。MyBatis提供了默认下基于Java HashMap的缓存实现,以及用于与OSCache、Ehcache、Hazelcast和Memcached连接的默认连接器。MyBatis还提供API供其他缓存实现使用。

  MyBatis执行SQL语句之后,这条语句就是被缓存,以后再执行这条语句的时候,会直接从缓存中拿结果,而不是再次执行SQL,这就是所说的一级缓存。一级缓存的作用域scope是SqlSession。

一级缓存

测试

同个session进行两次相同查询:

 @Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
User user2 = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user2);
} finally {
sqlSession.close();
}
}

test

MyBatis只进行1次数据库查询:

 ==>  Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2017}
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2017}

结果

同个session进行两次不同的查询:

 @Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
User user2 = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 2);
log.debug(user2);
} finally {
sqlSession.close();
}
}

test

MyBatis进行两次数据库查询:

 ==>  Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2017}
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 2(Integer)
<== Total: 1
User{id=2, name='FFF', age=50, birthday=Sat Dec 06 17:12:01 CST 2017}

结果

不同session,进行相同查询:

 @Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession2 = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
User user2 = (User)sqlSession2.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user2);
} finally {
sqlSession.close();
sqlSession2.close();
}
}

test

MyBatis进行了两次数据库查询:

 ==>  Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2017}
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2017}

结果

同个session,查询之后更新数据,再次查询相同的语句:

 @Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
user.setAge(100);
sqlSession.update("org.format.mybatis.cache.UserMapper.update", user);
User user2 = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user2);
sqlSession.commit();
} finally {
sqlSession.close();
}
}

test

更新操作之后缓存会被清除:

 ==>  Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2017}
==> Preparing: update USERS SET NAME = ? , AGE = ? , BIRTHDAY = ? where ID = ?
==> Parameters: format(String), 23(Integer), 2017-10-12 23:20:13.0(Timestamp), 1(Integer)
<== Updates: 1
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2017}

结果

很明显,结果验证了一级缓存的概念,在同个SqlSession中,查询语句相同的sql会被缓存,但是一旦执行新增或更新或删除操作,缓存就会被清除

MyBatis的缓存分析的更多相关文章

  1. 通过源码分析MyBatis的缓存

    前方高能! 本文内容有点多,通过实际测试例子+源码分析的方式解剖MyBatis缓存的概念,对这方面有兴趣的小伙伴请继续看下去~ MyBatis缓存介绍 首先看一段wiki上关于MyBatis缓存的介绍 ...

  2. Mybatis 缓存分析

    其实本来不想专门的写一篇关于mybatis缓存的博客的.在之前的博客中已经大致的把mybatis的整体流程讲了一遍.只要按照步骤一步步的点进去,关于缓存的代码很容易就能发现.但是今天在看代码的时候突然 ...

  3. MyBatis 源码分析 - 缓存原理

    1.简介 在 Web 应用中,缓存是必不可少的组件.通常我们都会用 Redis 或 memcached 等缓存中间件,拦截大量奔向数据库的请求,减轻数据库压力.作为一个重要的组件,MyBatis 自然 ...

  4. Mybatis源码分析之Cache二级缓存原理 (五)

    一:Cache类的介绍 讲解缓存之前我们需要先了解一下Cache接口以及实现MyBatis定义了一个org.apache.ibatis.cache.Cache接口作为其Cache提供者的SPI(Ser ...

  5. mybatis 源码分析(四)一二级缓存分析

    本篇博客主要讲了 mybatis 一二级缓存的构成,以及一些容易出错地方的示例分析: 一.mybatis 缓存体系 mybatis 的一二级缓存体系大致如下: 首先当一二级缓存同时开启的时候,首先命中 ...

  6. mybatis源码分析之06二级缓存

    上一篇整合redis框架作为mybatis的二级缓存, 该篇从源码角度去分析mybatis是如何做到的. 通过上一篇文章知道,整合redis时需要在FemaleMapper.xml中添加如下配置 &l ...

  7. mybatis源码分析之05一级缓存

    首先需要明白,mybatis的一级缓存就是指SqlSession缓存,Map缓存! 通过前面的源码分析知道mybatis框架默认使用的是DefaultSqlSession,它是由DefaultSqlS ...

  8. mybatis源码分析之走进缓存

    之前写了一篇关于mybatis缓存的读后感,想了想还是把缓存模块简单分析一下,附赠下载地址:https://github.com/MyBatis/MyBatis-3,github直接搜排名很靠前的. ...

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

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

随机推荐

  1. PHP的new self() 与new static()

    参考链接:[PHP中new static()与new self()的区别异同分析],[PHP中new self()和new static()的区别探究],[PHP中static和self的区别] 要点 ...

  2. Math.random理解练习

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. ArcGIS10.3+Oracle12C+ArcGIS Server10.3安装布署(之三)

    1.将Oracle的客户端切换到64位 (1)将C:\下的instantclient_12_1目录重命名为instantclient_12_1X86 (2)从Oracle的官方网站下载   insta ...

  4. android 自定义控件之ViewGroup生命周期执行步骤

    前言 了解ViewGroup的生命周期的执行步骤对于自己自定义ViewGroup的时候十分重要,清楚了整个流程才能对ViewGroup有更深的理解.本文从个人的总结,来阐述一下执行的顺序.执行说明 首 ...

  5. jsonp 返回以前必须要再转一次json

    public static void main(String[] args) {        String ajaxJsonStr = null;        AjaxJson ajaxJson ...

  6. python中的字符串编码问题——2.理解ASCII码、ANSI码、Unicode编码、UTF-8编码

    ASCII码:全名是American Standard Code for Information Interchange,ASCII码中,一个英文字母(不分大小写)占一个字节的空间,范围0x00~0x ...

  7. [CENTOS7] [IPTABLES] 卸载Firewall Id安装 IPTABLES及防火墙设置

    卸载Firewall ID,重装IPTABLES:先停止服务 systemctl stop firewalldsystemctl mask firewalld   yum install iptabl ...

  8. 申请Let’s Encrypt永久免费SSL证书过程教程及常见问题

    配置证书https://easy.zhetao.com/   虽然目前Let’s Encrypt免费SSL证书默认是90天有效期,但是我们也可以到期自动续约,不影响我们的尝试和使用,为了考虑到文章的真 ...

  9. C# 所有特性,特性所在命名空间,那些命名空间拥有特性类

    文章持续补充中 特性并不是集中在某一命名空间中,而是不同的特性在不同的命名空间下,特性是某一命名空间下提供的语法糖. 有哪些命名空间提供特性: 命名空间 描述 Microsoft.Build.Fram ...

  10. 安全预警-防范新型勒索软件“BlackRouter”

    近期,出现一种新型勒索软件“BlackRouter”,开发者将其与正常软件恶意捆绑在一起,借助正常软件的下载和安装实现病毒传播,并以此躲避安全软件的查杀.目前,已知的被利用软件有AnyDesk工具(一 ...