苍穹外卖--springCache-@CachePut使用
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import com.inmind.entity.User;
|
||||
import com.inmind.mapper.UserMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@Slf4j
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* 业务:在新增用户时,将用户信息缓存到redis中
|
||||
*
|
||||
* Spring Expression Language(SpEL)
|
||||
*
|
||||
*/
|
||||
@PostMapping
|
||||
// @CachePut(cacheNames = "userCache",key = "#user.id")//如果使用springCache缓存数据,key的生成的规则:userCache::动态ID
|
||||
@CachePut(cacheNames = "userCache",key = "#result.id")//对象导航,result就表示该方法的返回值!!!
|
||||
// @CachePut(cacheNames = "userCache",key = "#p0.id")//p0:param0 参数列表中第一个参数
|
||||
// @CachePut(cacheNames = "userCache",key = "#a0.id")//a0:argment0 参数列表中第一个参数
|
||||
// @CachePut(cacheNames = "userCache",key = "#root.args[0].id")//root.args[0]:save方法的参数列表的第0个参数
|
||||
public User save(@RequestBody User user){
|
||||
userMapper.insert(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteById(Long id){
|
||||
userMapper.deleteById(id);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delAll")
|
||||
public void deleteAll(){
|
||||
userMapper.deleteAll();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public User getById(Long id){
|
||||
User user = userMapper.getById(id);
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user