CachePut如何获取返回值的Id?
CachePut how to get and Id of returned value?
@CachePut(value = "dishDTOs", key = "IDOFTHERETURNEDVALUE")
@CacheEvict(cacheNames = "dishDTOList", allEntries = true)
public DishResponseDTO createDish(DishCreationDTO dishCreationDTO) {
int restaurantId = dishCreationDTO.getRestaurantId();
var restaurant = restaurantRepository.getById(restaurantId);
var dish = DishMapper.toDish(dishCreationDTO, restaurant);
return toDishResponseDTO(dishRepository.save(dish));
}
Id 是由 DB 分配的,所以我需要以某种方式获取它作为键。
SpeL 可以使用 #result
访问返回的对象,您可以使用 .
和字段名称访问对象参数。例如
@CachePut(value = "dishDTOs", key = "#result.id)
@CacheEvict(cacheNames = "dishDTOList", allEntries = true)
public DishResponseDTO createDish(DishCreationDTO dishCreationDTO) {
int restaurantId = dishCreationDTO.getRestaurantId();
var restaurant = restaurantRepository.getById(restaurantId);
var dish = DishMapper.toDish(dishCreationDTO, restaurant);
return toDishResponseDTO(dishRepository.save(dish));
}
将 .id
更改为您的 DishResponseDTO 字段名称。
@CachePut(value = "dishDTOs", key = "IDOFTHERETURNEDVALUE")
@CacheEvict(cacheNames = "dishDTOList", allEntries = true)
public DishResponseDTO createDish(DishCreationDTO dishCreationDTO) {
int restaurantId = dishCreationDTO.getRestaurantId();
var restaurant = restaurantRepository.getById(restaurantId);
var dish = DishMapper.toDish(dishCreationDTO, restaurant);
return toDishResponseDTO(dishRepository.save(dish));
}
Id 是由 DB 分配的,所以我需要以某种方式获取它作为键。
SpeL 可以使用 #result
访问返回的对象,您可以使用 .
和字段名称访问对象参数。例如
@CachePut(value = "dishDTOs", key = "#result.id)
@CacheEvict(cacheNames = "dishDTOList", allEntries = true)
public DishResponseDTO createDish(DishCreationDTO dishCreationDTO) {
int restaurantId = dishCreationDTO.getRestaurantId();
var restaurant = restaurantRepository.getById(restaurantId);
var dish = DishMapper.toDish(dishCreationDTO, restaurant);
return toDishResponseDTO(dishRepository.save(dish));
}
将 .id
更改为您的 DishResponseDTO 字段名称。