前言
SpringBoot继承了Spring框架的缓存管理功能, 通过使用@EnableCaching注解开启基于注解的缓存支持,SpringBoot就可以启动缓存管理的自动化配置 。
接下来针对SpringBoot支持的默认缓存管理进行讲解。
SpringBoot默认缓存管理
1、基础环境搭建
(1)准备数据
使用该数据库中包含两张数据表:t_article和t_comment。
(2)创建项目,代码编写
1、在项目依赖中添加SQL模块的JPA依赖、MySql依赖以及Web模块中的Web依赖,如下图所示:

引入这三个依赖器创建项目,在项目pom.xml文件会出现以下依赖:

2、编写数据库表对应的实体类,并使用JPA相关注解配置映射关系
package com.hardy.springbootdatacache.entity;
import org.springframework.data.annotation.Id;
import javax.persistence.*;
/**
* @Author: HardyYao
* @Date: 2021/6/19
*/
@Entity(name = "t_comment") // 设置ORM实体类,并指定映射的表名
public class Comment {
@Id // 映射对应的主键id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 设置主键自增策略
private Integer id;
private String content;
private String author;
@Column(name = "a_id") // 指定映射的表字段名
private Integer aId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getaId() {
return aId;
}
public void setaId(Integer aId) {
this.aId = aId;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", content='" + conten

本文介绍了SpringBoot的默认缓存管理,包括基础环境搭建、使用@EnableCaching和@Cacheable注解实现缓存、缓存注解的详细解析,以及不同注解在缓存管理中的作用。通过示例展示了如何利用缓存减少数据库访问。
 默认缓存管理&spm=1001.2101.3001.5002&articleId=118489638&d=1&t=3&u=0f73621994b44525bd07299dd47d8f7a)
583

被折叠的 条评论
为什么被折叠?



