苍穹外卖day04-问题与解决方法

自己写的DishService

public List<Dish> getByCategoryId(Long categoryId) {
        List<Dish> list=dishMapper.list(categoryId);
        return list;
    }

自己写的DishMapper

@Select("select * from dish where category_id = #{categoryId} and status = 1")
    List<Dish> list(Long categoryId);

标准答案

public List<Dish> list(Long categoryId) {
    Dish dish = Dish.builder()
        .categoryId(categoryId)
        .status(StatusConstant.ENABLE)
        .build();
    return dishMapper.list(dish);
}
<select id="list" resultType="Dish" parameterType="Dish">
    select * from dish
    <where>
        <if test="name != null">
            and name like concat('%',#{name},'%')
        </if>
        <if test="categoryId != null">
            and category_id = #{categoryId}
        </if>
        <if test="status != null">
            and status = #{status}
        </if>
    </where>
    order by create_time desc
</select>

差别分析:

我满足的需求为:根据分类名称查找在售的菜品
需要的需求为:根据分类名称或菜品名称查找在售的菜品
总结:观察前端页面时,漏了一个根据菜品名称查找菜品的需求,

自己写的SetmealService的存入中间表

//2.在中间表中保存套餐和菜品的关联关系(套餐和菜品是多对多关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.insertBatch(setmealDishes);

标准答案

//2.在中间表中保存套餐和菜品的关联关系(套餐和菜品是多对多关系)
        //
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);});
        setmealDishMapper.insertBatch(setmealDishes);

差别分析:

忘记了套餐加入数据库之后才会自增产生id,
所以在套餐保存到数据库之后才能获得套餐的id,并加入中间表中

前端页面未显示

Controller中,注释使用了@Controller,没使用@RestController,没给前端返回信息,当然收不到了

@RequestParam

@RequestParam 注解会将这些 id 收集到一个 List< Long > 中,以便在 deleteBatch(ids) 方法中作为批量删除的参数使用。

你可能感兴趣的:(java)