【springboot】内置缓存

一.代码示例
1.pom配置坐标
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
 <!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>
2.application配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

mybatis-plus:
  global-config:
    db-config:
      table-prefix: learning_
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.启用缓存
package com.learning;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

/**
 * @Author wangyouhui
 * @Description TODO
 **/
@SpringBootApplication
// 开启缓存
@EnableCaching
public class SpringBootLearningApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootLearningApplication.class, args);
    }
}

4.controller
package com.learning.controller;

import com.learning.domain.Student;
import com.learning.service.StudentService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wangyouhui
 * @Description 测试缓存
 **/
@RestController
@RequestMapping("/cache")
@AllArgsConstructor
public class CacheController {

    private StudentService studentService;

    @GetMapping("/get")
    public Student get(String id){
        return studentService.getById(id);
    }
}

5.service
1.@Cacheable既会存,也会取
2.@CachePut只会存,不会取
package com.learning.service.impl;

import com.learning.dao.StudentDao;
import com.learning.domain.Student;
import com.learning.service.StudentService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @Author wangyouhui
 * @Description 学生实现类
 **/
@Service
@AllArgsConstructor
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;

    @Override
    @Cacheable(value="studentCache", key="#id")
    public Student getById(String id) {
        return studentDao.selectById(id);
    }
}

6.dao
package com.learning.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.learning.domain.Student;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentDao extends BaseMapper<Student> {
}
二.效果示例
1. 查询两次接口,只会查询一次日志

在这里插入图片描述

2.数据库截图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值