一、开启 springcache,启动类添加 @EnableCaching 注解

@SpringBootApplication
@EnableCaching
public class GatheringApplication { public static void main(String[] args) {
SpringApplication.run(GatheringApplication.class, args);
}
}

二、添加缓存,修改 findById 方法 通过 @Cacheable 添加缓存

    /**
* 根据ID查询实体
* @param id
* @return
*/
@Cacheable(value = "gathering",key = "#id")
public Gathering findById(String id) {
System.out.println("查询次数:"+i++);
return gatheringDao.findById(id).get();
}

三、删除缓存,修改 update 和 deleteById 方法 通过 @CacheEvict 删除缓存

/**
* 修改
* @param gathering
*/
@CacheEvict(value = "gathering",key = "#gathering.id")
public void update(Gathering gathering) {
gatheringDao.save(gathering);
} /**
* 删除
* @param id
*/
@CacheEvict(value = "gathering",key = "#id")
public void deleteById(String id) {
gatheringDao.deleteById(id);
}

四、执行结果

1、点击了5次查询,只有一次进入了方法体并,访问了数据库。其余4次都是从缓存中取数据。

  查询次数:1
  Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=? 2、执行修改方法

   Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?
   Hibernate: update tb_gathering set address=?, city=?, detail=?, endtime=?, enrolltime=?, where id=?

 3、再次查询(点击5次),因为执行 修改方法时,也删除了缓存,所以本次查询可以进入方法体,并交互数据库

   查询次数:2
   Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?

 4、执行删除方法

   Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?
   Hibernate: delete from tb_gathering where id=?

 5、再次查询(点击5次),因为执行 删除方法时,也删除了缓存,所以本次查询可以进入方法体,并交互数据库,但未查询出结果 

   查询次数:3
    Hibernate: select gathering0_.id as id1_0_0_, gathering0_.address as address2_0_0_ from tb_gathering gathering0_ where gathering0_.id=?

Spring Boot 之 springcache的使用的更多相关文章

  1. Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来

    计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来. 本文目录 一.Spring Ca ...

  2. 利用Spring Boot+zxing,生成二维码还能这么简单

    在网站开发中,经常会遇到要生成二维码的情况,比如要使用微信支付.网页登录等,本文分享一个Spring Boot生成二维码的例子,这里用到了google的zxing工具类. 本文目录 一.二维码简介二. ...

  3. 玩转spring boot——快速开始

    开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...

  4. 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)

    Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...

  5. 玩转spring boot——开篇

    很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...

  6. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  7. 玩转spring boot——AOP与表单验证

    AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...

  8. 玩转spring boot——结合JPA入门

    参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...

  9. 玩转spring boot——结合JPA事务

    接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

随机推荐

  1. JavaEE高级-SpringMVC学习笔记

    *SpringMVC概述 - Spring为展示层提供的基于MVC设计理念的优秀Web框架,是目前最主流的MVC框架之一 - Spring3.0后全面超越Struts2,成为最优秀的MVC框架 - S ...

  2. FPGA 物理时序不合理的体现(体现方式:数字钟的行扫描和列扫描)

    本人在这只讨论建模好的模块来比较解释现象,如有不周到请大家指正. 软件功能仿真和在硬件上的区别:可以从这个数码管的行扫描和列扫描实例来体会一下,物理时序的影响和改进方法. 数码管的行扫描.列扫描要求同 ...

  3. springboot easyexcel

    pom..xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel&l ...

  4. Delphi-----接口请求,Get与Post

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. VS2015开发常用快捷键

    以下内容均Ctrl+后面的按钮 M-O\P折叠 K-F 格式化 K-U\C注释 K-S侧外代码-(区域代码) 代码片段 ctor 自动生成默认的构造函数 prop 自动生成get set方法 cw 自 ...

  6. vue组件学习(一)

    1, vue中的 is 的用法,有时候我们需要把一个组件绑定到指定的标签下,比如把tr组件放到table下,直接这样写是不行的, <!DOCTYPE html> <html lang ...

  7. 【leetcode】All Paths From Source to Target

    题目如下: Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, a ...

  8. flex的12个属性

    容器(父元素)的属性: flex-direction属性决定主轴的方向 flex-wrap 属性决定项目在一行排不下的情况下是否换行 flex-flow flex-flow属性是flex-direct ...

  9. eclipse设置maven web项目打包

    如图:eclipse下的maven web项目,打包部署到本地tomcat时,需要关注的2个方面: 1. src/main/webapp目录下的文件,打包到/ 根路径下 2. 添加maven 依赖,打 ...

  10. [ZJU 1003] Crashing Balloon

    ZOJ Problem Set - 1003 Crashing Balloon Time Limit: 2 Seconds      Memory Limit: 65536 KB On every J ...