基于 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);
    }

运行测试

输入测试数据

新增菜品成功

相关推荐
凌冰_36 分钟前
Springboot MyBatis 数据库连接池
数据库·spring boot·mybatis
Q_Q19632884752 小时前
python大学校园旧物捐赠系统
开发语言·spring boot·python·django·flask·node.js·php
老李不敲代码3 小时前
榕壹云外卖跑腿系统:基于Spring Boot的开源生活服务平台技术解析
spring boot·微信小程序·uni-app·开源·生活·软件需求
星辰大海的精灵3 小时前
Spring Boot 中 WebClient 的实践详解
java·spring boot·后端
七七&5564 小时前
Spring Boot 常用注解整理
java·spring boot·后端
风象南6 小时前
SpringBoot的6种数据库垂直分片实现策略
java·spring boot·后端
sg_knight13 小时前
Spring Cloud LoadBalancer深度解析:官方负载均衡方案迁移指南与避坑实践
java·spring boot·spring·spring cloud·微服务·负载均衡
llwszx14 小时前
Spring Boot 整合 Spring AI 与 MCP 开发智能体工具指南
人工智能·spring boot·spring·智能体·spring ai·mcp