一、开启 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=?

最新文章

  1. Javaweb学习笔记——使用Jdom解析xml
  2. vuejs
  3. Effective C++ 之 Item 2:尽量以 const, enum, inline 替换 #define
  4. App_GlobalResources.afvubzdv.resources.dll”--“拒绝访问。“
  5. OA项目之分页
  6. ecshop详细的安装教程
  7. VB6 GDI+ 入门教程[1] GDI+介绍
  8. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)
  9. [LeetCode#277] Find the Celebrity
  10. 数据结构(树链剖分,线段树):SDOI 2016 游戏
  11. delegate实现Javascript的each方法
  12. “ddl”有一个无效 SelectedValue,因为它不在项目列表中。
  13. hdu 2454 Degree Sequence of Graph G (推断简单图)
  14. JVM 几个重要的参数
  15. 一步到位分布式开发Zookeeper实现集群管理
  16. python批量提取eml附件
  17. js数据三大储存格式
  18. JavaScript深入之词法作用域和动态作用域
  19. Eclipse Python 开发环境搭建 pydev 插件
  20. php使用MPDF导出PDF文件自定义字体

热门文章

  1. php设置错误级别
  2. background的水平条纹和斜向条纹
  3. msdn帮助,离线下载
  4. 关于Mysql group by 的记录
  5. 关于Mysql5.6 Failed to open file error2的记录
  6. Linux文件的操作及授权
  7. VMware Workstation 卸载时卡在“正在卸载网络驱动程序(Virtual Network Editor夯死)”
  8. 008-kvm虚拟化管理平台WebVirtMgr部署-完整记录(1)
  9. 下载放在resource下面的excel文件
  10. php内置函数分析之array_fill_keys()