mysql springboot 缓存_SpringBoot中默认缓存实现方案的示例代码

本文详细介绍了如何在Spring Boot项目中实现默认缓存,结合Spring Cache注解,创建web项目,添加相关依赖,配置数据源,创建缓存配置类,定义实体、仓库、服务及控制器,最后启动项目进行测试,展示数据来源于缓存而非每次查询数据库。
摘要由CSDN通过智能技术生成

在上一节中,我带大家学习了详解SpringBoot集成Redis来实现缓存技术方案,尤其是结合Spring Cache的注解的实现方案,接下来在本章节中,我带大家通过代码来实现。

一. Spring Boot实现默认缓存

1. 创建web项目

我们按照之前的经验,创建一个web程序,并将之改造成Spring Boot项目,具体过程略。

f34b3fcaaf07d272fd5349c31f6c9a2d.png

2. 添加依赖包

org.springframework.boot

spring-boot-starter-data-jpa

mysql

mysql-connector-java

org.springframework.boot

spring-boot-starter-cache

3. 创建application.yml配置文件

server:

port: 8080

spring:

application:

name: cache-demo

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

username: root

password: syc

url: jdbc:mysql://localhost:3306/spring-security?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&serverTimezone=UTC

#cache:

#type: generic #由redis进行缓存,一共有10种缓存方案

jpa:

database: mysql

show-sql: true #开发阶段,打印要执行的sql语句.

hibernate:

ddl-auto: update

4. 创建一个缓存配置类

主要是在该类上添加@EnableCaching注解,开启缓存功能。

package com.yyg.boot.config;

import org.springframework.cache.annotation.EnableCaching;

/**

* @Author 一一哥Sun

* @Date Created in 2020/4/14

* @Description Description

* EnableCaching启用缓存

*/

@Configuration

@EnableCaching

public class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值