在写苍穹外卖的修改套餐模块时发现回显仍然显示套餐菜品 明明已经调用了deleteById方法 然后我又去看之前的菜品管理 发现菜品管理的口味也回显了 我记得我写完然后调试的时候明明没有回显口味 但是更新可以正常更新 没有其他报错 求解答
SetmealController
@GetMapping("/{id}")
@ApiOperation("根据id查询套餐")
public Result<SetmealVO> select(@PathVariable Long id){
log.info("当前查询套餐id为:{}",id);
SetmealVO setmealVO = setmealService.selectById(id);
return Result.success(setmealVO);
}
/**
* 修改套餐
*
* @param setmealDTO
* @return
*/
@PutMapping
@ApiOperation("修改套餐")
public Result update(@RequestBody SetmealDTO setmealDTO) {
setmealService.update(setmealDTO);
return Result.success();
}
SetmealService
/**
* 根据id查询套餐
* @param id
* @return
*/
SetmealVO selectById(Long id);
/**
* 修改套餐
* @param setmealDTO
*/
void update(SetmealDTO setmealDTO);
SetmealServiceImpl
/**
* 根据id查询套餐
* @param id
* @return
*/
@Override
public SetmealVO selectById(Long id) {
Setmeal setmeal = setmealMapper.getById(id);
List<SetmealDish> setmealDishes = setMealDishMapper.getBySetMealDishId(id);
SetmealVO setmealVO = new SetmealVO();
BeanUtils.copyProperties(setmeal,setmealVO);
setmealVO.setSetmealDishes(setmealDishes);
return setmealVO;
}
/**
* 修改套餐
* @param setmealDTO
*/
@Transactional
public void update(SetmealDTO setmealDTO) {
Setmeal setmeal = new Setmeal();
BeanUtils.copyProperties(setmealDTO, setmeal);
//1、修改套餐表,执行update
setmealMapper.update(setmeal);
//套餐id
Long setmealId = setmealDTO.getId();
//2、删除套餐和菜品的关联关系,操作setmeal_dish表,执行delete
setMealDishMapper.deleteById(setmealId);
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
setmealDishes.forEach(setmealDish -> {
setmealDish.setSetmealId(setmealId);
});
//3、重新插入套餐和菜品的关联关系,操作setmeal_dish表,执行insert
setMealDishMapper.insertBatch(setmealDishes);
}
}
SetmealMapper
@AutoFill(OperationType.UPDATE)
void update(Setmeal setmeal);
SetmealDishMapper
@Select("select * from setmeal_dish where setmeal_id = #{setmealId};")
List<SetmealDish> getBySetMealDishId(Long id);
@Delete(("delete from setmeal_dish where setmeal_id = #{setmealId};"))
void deleteById(Long setmealId);
SetmealMapper.xml
<update id="update">
update setmeal
<set>
<if test="name != null">
name = #{name},
</if>
<if test="categoryId != null">
category_id = #{categoryId},
</if>
<if test="price != null">
price = #{price},
</if>
<if test="image != null">
image = #{image},
</if>
<if test="description != null">
description = #{description},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
<if test="updateUser != null">
update_user = #{updateUser},
</if>
</set>
where id = #{id}
</update>