@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分配的,所以我需要以某种方式将其作为键放入。
发布于 2021-09-01 10:59:29
带#result的SpeL可以访问返回的对象,您可以使用.和字段名来访问对象参数。例如
@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));
}使用您的DishResponseDTO字段名更改.id。
https://stackoverflow.com/questions/69012306
复制相似问题