目录结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/57edfc9d93484a9da821f40b9d7858f9.png)
Controller层
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private CacheManager cacheManager;
@Autowired
private UserService userService;
@PostMapping
@CachePut(value = "userCache", key = "#user.id")
public User save(User user){
userService.save(user);
return user;
}
@DeleteMapping("/{id}")
@CacheEvict(value = "userCache",key = "#id")
public void delete(@PathVariable Long id){
userService.removeById(id);
}
@CacheEvict(value = "userCache",key = "#user.id")
@PutMapping
public User update(User user){
userService.updateById(user);
return user;
}
@GetMapping