当前位置: 首页 > news >正文

动手试一试 Spring Boot默认缓存管理

1.准备数据

        使用之前创建的springbootdata的数据库,该数据库有两个表t_article和t_comment,这两个表预先插入几条测试数据。

2.编写数据库表对应的实体类

@Entity(name = "t_comment")
public class Comment {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;private String content;private String author;@Column(name = "a_id")private Integer aId;//补充get set toString}

3.创建CommentRepository

public interface CommentRepository extends JpaRepository<Comment,Integer>{@Transactional@Modifying@Query("UPDATE t_comment c SET c.author= ?1 WHERE  c.id = ?2")public int updateComment(String author,Integer id);}

4.创建CommentService

在该类中编写数据的查询、修改和删除操作

@Service
public class CommentService {@Autowiredprivate CommentRepository commentRepository;public Comment findById(int comment_id){Optional<Comment> optional = commentRepository.findById(comment_id);if(optional.isPresent()){return optional.get();}return null;}public Comment updateComment(Comment comment){commentRepository.updateComment(comment.getAuthor(), comment.getId());return comment;}public void deleteComment(int comment_id){commentRepository.deleteById(comment_id);}
}

5.创建CommentController

使用注入的CommentService实例对象编写对Comment评论数据的查询、修改和删除方法。

@RestController
public class CommentController {@Autowiredprivate CommentService commentService;@GetMapping("/get/{id}")public Comment findById(@PathVariable("id") int comment_id) {Comment comment = commentService.findById(comment_id);return comment;}@GetMapping("/update/{id}/{author}")public Comment updateComment(@PathVariable("id") int comment_id, @PathVariable("author") String author) {Comment comment = commentService.findById(comment_id);comment.setAuthor(author);Comment updateComment = commentService.updateComment(comment);return updateComment;}@GetMapping("/delete/{id}")public void deleteComment(@PathVariable("id") int comment_id) {commentService.deleteComment(comment_id);}
}

6.设置配置信息

spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true

7.项目测试

项目启动成功后,在浏览器上访问http://localhost:8080/get/1,浏览器每刷新一次,控制台会新输出一条SQL语句

每执行一次查询操作,都会访问一次数据库并执行一次SQL语句。

8、启动类上添加@EnableCaching注解

开启基于注解的缓存支持

//启动类
@EnableCaching
@SpringBootApplication
public class Chapter06Application {public static void main(String[] args) {SpringApplication.run(Chapter06Application.class, args);}
}

9、使用@Cacheable注解对数据操作方法进行缓存管理

//业务层
@Cacheable(cacheNames = "comment")
public Comment findById(int comment_id){Optional<Comment> optional = commentRepository.findById(comment_id);if(optional.isPresent()){return optional.get();}return null;
}

10、Spring Boot默认缓存测试

项目启动成功后,在浏览器上访问http://localhost:8080/get/1,不论浏览器刷新多少次,页面的查询结果都会显示同一条数据

重复进行同样的查询操作,数据库只执行了一次SQL查询语句,说明项目开启的默认缓存支持已经生效。

http://www.xdnf.cn/news/127369.html

相关文章:

  • 中央对齐模式1 2与更新中断
  • Apifox 4月更新|Apifox在线文档支持LLMs.txt、评论支持使用@提及成员、支持为团队配置「IP 允许访问名单」
  • 使用setGraphicsEffect重新设置阴影导致程序崩溃的问题
  • SAP SuccessFactors Recruiting and Onboarding The Comprehensive Guide
  • 【oql】spark thriftserver内存溢出,使用oql查询导致oom的sql
  • 覆盖纸(Overlay Paper):装饰材料领域的“隐形冠军”
  • 每日一练(4~24):互质的数【省模拟赛】
  • 【python】解释builtin.py函数为何全是pass
  • Kaamel白皮书:Model Context Protocol (MCP) 隐私安全最佳实践
  • AGP8+ fullMode 完全模式混淆闪退
  • MAC地址攻击和ARP攻击的原理及解决方法
  • nodejs导入文件模块和导入文件夹
  • 研0调研入门
  • 【Vue3 实战】插槽封装与懒加载
  • LJF-Framework 第14章 LjfSecurity适配SpringSecurity
  • springcloud-openfeign
  • 使用钉钉机器人推送系统内部的ERP停机维护公告
  • 微信小程序 tabbar底部导航栏
  • 传统的图像压缩技术(二)
  • mysql——索引事务和JDBC编程
  • 【C++基础知识】namespace前加 inline
  • 低代码平台开发胎压监测APP
  • 【MySQL数据库】表的增删改查
  • C++智能指针上
  • 如何在Spring Boot中禁用Actuator端点安全性
  • containerd 配置代理
  • 生成随机验证码-解析与优化
  • 扩张尺度张量填充方式
  • HTML字符实体和转义字符串
  • 【Linux】基本指令(下)