1.首先先进行查找uuid为123的用户
@GetMapping("/{uuid}")
public User get(@PathVariable String uuid) {
return userService.findByUuid(uuid);
}
@Cacheable(value = "userInfo", key = "'findByUuid-' + #uuid")
@Override
public User findByUuid(String uuid) {
return userDao.findOne(uuid);
}
能确保uuid为123的用户查找得出来而且查找到的用户也存入缓存.(多查找几次控制台也只显示一次的sql语句)
2.更新uuid为123的用户
@PutMapping("/{uuid}")
public String update(@PathVariable String uuid,@RequestBody User updateUser){
User user = userService.findByUuid(uuid);
user.setUserName(updateUser.getUserName());
user.setPassword(updateUser.getPassword());
user.setAge(updateUser.getAge());
user.setPhone(updateUser.getPhone());
userService.save(user);
return "success";
}
@CacheEvict(value = "userInfo", key = "'findByUuid-' + #user.getUuid()")
@Override
public User save(User user) {
return userDao.save(user);
}
更新时执行却又执行了select ,按道理来说findByUuid(123)应该被存入缓存,而不会打印select 语句才对啊.
以下是更新uuid为123时控制台打印的语句
Hibernate:
select
user0_.uuid as uuid1_0_0_,
user0_.age as age2_0_0_,
user0_.password as password3_0_0_,
user0_.phone as phone4_0_0_,
user0_.user_name as user_nam5_0_0_
from
user user0_
where
user0_.uuid=?
Hibernate:
update
user
set
age=?,
password=?,
phone=?,
user_name=?
where
uuid=?