<select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
select setmeal_id from setmeal_dish where dish_id in
<foreach collection="dishIds" item="dishId" separator=","open="(" close=")">
#{dishId}</foreach></select>
dishFlavorMapper中添加删除口味的方法
dishFlavorMapper.java
@Delete("delete from dish_flavor where dish_id = #{dishId}")voiddeleteByDishId(Long dishId);
代码优化
删除菜品和口味是一个一个删除的,可能引发性能问题,需要sql语句批量删除数据
修改DishServiceImpl.java
@Override@TransactionalpublicvoiddeleteBatch(List<Long> ids){//判断当前菜品是否能够删除-是否起售中for(Long id : ids){Dish dish = dishMapper.getById(id);if(dish.getStatus()==StatusConstant.ENABLE){thrownewDeletionNotAllowedException(MessageConstant.DISH_ON_SALE);}}//判断当前菜品是否能够删除-是否被套餐关联List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);if(setmealIds !=null&& setmealIds.size()>0){thrownewDeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);}//删除菜品表中的菜品数据// for (Long id : ids) {// dishMapper.deleteById(id);// //删除菜品关联的口味数据// dishFlavorMapper.deleteByDishId(id);// }//批量删除
dishMapper.deleteByIds(ids);
dishFlavorMapper.deleteByDishIds(ids);}
DishMapper添加批量删除函数deleteByIds,并且在xml中实现
DishMapper.xml
<delete id="deleteByIds">
delete from dish where id in
<foreach collection="ids" separator=","open="(" close=")" item="id">
#{id}</foreach></delete>
DishFlavorMapper添加批量删除函数deleteByDishIds,并且在xml中实现
DishFlavorMapper.xml
<delete id="deleteByDishIds">
delete from dish_flavor where dish_id in
<foreach collection="dishIds" separator=","open="(" close=")" item="dishId">
#{dishId}</foreach></delete>