【SpringBoot学习】23-@Cacheable实现

@Cacheable
在这里插入图片描述
源码分析
在这里插入图片描述
@Cacheable中参数

在这里插入图片描述

配置类略
依赖导入
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
启动类

使用@EnableCaching 开启缓存

@EnableCaching //开启注解 缓存
@SpringBootApplication
@MapperScan("cn.qqqking.springboot.mapper")
public class SpringBoot13CacheApplication {

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

}
Mapper
/**
 * @author AnQi
 * @date 2020/3/15 10 46:34
 * @description
 */
public interface UserMapper {
    @Select("select * from user where id = #{id}")
    User getUserById(Integer id);
}
service层

方法前使用 @Cacheable(cacheNames = “user”,key = “#id”)

  • cacheNames 缓存容器名字
  • key 缓存容器中key
  • value return user;
/**
 * @author AnQi
 * @date 2020/3/15 10 57:15
 * @description
 */
@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * cacheNames 缓存容器名字
     * key 缓存容器中key
     * value return user;
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "user",key = "#id")
    public User getUserById(Integer id){
        User user = userMapper.getUserById(id);
        return user;
    }
}
controller层
/**
 * @author AnQi
 * @date 2020/3/15 10 58:45
 * @description
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
        User user = userService.getUserById(id);
        return user;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值