基于 Spring Boot 瑞吉外卖系统开发(九)

基于 Spring Boot 瑞吉外卖系统开发(九)

保存菜品

菜品管理页面提供了一个"+新增菜品"按钮,单击该按钮时,会打开新增菜品页面。

请求路径/dish,请求方法POST,参数使用DishDto类接收。

DishDto 添加flavors属性用来接收flavors数据。

java 复制代码
@Data
public class DishDto extends Dish {
    private String categoryName;
    private List<DishFlavor> flavors;
}

添加菜品口味的通用类

新增菜品信息时,不仅需要新增菜品的基本信息,还需要新增菜品的口味信息,所以也需要操作菜品口味表。因此,需要创建DishFlavor通用类和接口。

java 复制代码
@Mapper
public interface DishFlavorMapper extends BaseMapper<DishFlavor> {
}
java 复制代码
public interface DishFlavorService extends IService<DishFlavor> {
}
java 复制代码
@Service
public class DishFlavorServiceImpl 
		extends ServiceImpl<DishFlavorMapper, DishFlavor> 
		implements DishFlavorService {
}

在DishController类中定义新增菜品的方法

java 复制代码
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

添加保存菜品和口味的接口

添加saveWithFlavor接口。

java 复制代码
public interface DishService extends IService<Dish> {

    public Page<DishDto> selectDishDtoPage(Page page);

    public void saveWithFlavor(DishDto dishDto);

}

DishServiceImpl 实现类添加实现方法。

java 复制代码
    @Autowired
    private DishFlavorService dishFlavorService;

    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //新增菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();//获取菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for(DishFlavor flavor:flavors){
            flavor.setDishId(dishId);
        }
        //新增菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

运行测试

输入测试数据

新增菜品成功

相关推荐
做个文艺程序员10 小时前
流式输出(SSE)在 Spring Boot 中的实现【OpenClAW + Spring Boot 系列 第3篇】
java·spring boot·后端
俺爱吃萝卜11 小时前
Spring Boot 3 + JDK 17:新一代微服务架构最佳实践
java·spring boot·架构
做个文艺程序员11 小时前
Spring Boot 项目集成 OpenClAW【OpenClAW + Spring Boot 系列 第1篇】
java·人工智能·spring boot·开源
霸道流氓气质12 小时前
SpringBoot+LangChain4j+Ollama实现本地大模型语言LLM的搭建、集成和示例流程
java·spring boot·后端
MegaDataFlowers13 小时前
使用SpringBoot+MyBatis+MySQL完成后端的数据库增删改查(CRUD)操作
数据库·spring boot·mybatis
做个文艺程序员13 小时前
Spring Boot 封装 OpenClAW 服务层最佳实践【OpenClAW + Spring Boot 系列 第2篇】
java·人工智能·spring boot·开源
2601_9498166814 小时前
如何在 Spring Boot 中配置数据库?
数据库·spring boot·后端
做个文艺程序员14 小时前
多轮对话与会话管理:构建上下文感知的 AI 接口【OpenClAW + Spring Boot 系列 第4篇】
人工智能·spring boot·开源
tang_jian_dong16 小时前
springboot + vue3 集成tianai.captcha验证码
java·spring boot·spring
indexsunny16 小时前
互联网大厂Java面试实录:微服务+Spring Boot在电商场景中的应用
java·spring boot·redis·微服务·eureka·kafka·spring security