spring提供的内置缓存功能

本文介绍了如何在SpringBoot项目中使用内置缓存技术,包括添加spring-boot-starter-cache依赖,配置启动类开启缓存,以及在Service方法上使用@Cacheable注解进行数据缓存。
摘要由CSDN通过智能技术生成
第一步:添加依赖
        <!--spring内置的缓存技术-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

第二步:在启动类上添加@EnableCaching
        @SpringBootApplication
        @MapperScan("com.example.demousermanagement.dao")
        //将缓存开关打开
        @EnableCaching
        public class DemoUserManagementApplication {

            public static void main(String[] args) {
                SpringApplication.run(DemoUserManagementApplication.class, args);
            }

        }

第三步:在service实现方法上添加@Cacheable(value = "cacheInfoEle", key = "#id")
        @Override
        //创建一个缓冲区,缓冲区的名字叫cacheInfoEle(其实是个map),里面存放以#id为key的键值对
        @Cacheable(value = "cacheInfoEle", key = "#id")
        public Info queryById(String id) {
            return infoDao.queryById(id);
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值