当前位置: 首页 > news >正文

6--苍穹外卖-SpringBoot项目中菜品管理 详解(二)

目录

菜品分页查询

需求分析和设计

代码开发

设计DTO类

设计VO类

Controller层

Service层接口

Service层实现类

Mapper层

功能测试

删除菜品

需求设计和分析

代码开发

Controller层

Service层接口

Service层实现类

Mapper层

功能测试

修改菜品

需求分析和设计

代码开发

根据id查询菜品实现

Controller层

Service层接口

Service层实现类

Mapper层

修改菜品实现

Controller层

Service层接口

Service层实现类

Mapper层

功能测试


  
1--苍穹外卖-SpringBoot项目介绍及环境搭建 详解-CSDN博客

2--苍穹外卖-SpringBoot项目中员工管理 详解(一)-CSDN博客

3--苍穹外卖-SpringBoot项目中员工管理 详解(二)-CSDN博客

4--苍穹外码-SpringBoot项目中分类管理 详解-CSDN博客

5--苍穹外卖-SpringBoot项目中菜品管理 详解(一)-CSDN博客

6--苍穹外卖-SpringBoot项目中菜品管理 详解(二)-CSDN博客

7--苍穹外卖-SpringBoot项目中套餐管理 详解(一)-CSDN博客

8--苍穹外卖-SpringBoot项目中套餐管理 详解(二)-CSDN博客

9--苍穹外卖-SpringBoot项目中Redis的介绍及其使用实例 详解-CSDN博客

菜品分页查询

需求分析和设计

业务规则:

  • 根据页码展示菜品信息

  • 每页展示10条数据

  • 分页查询时可以根据需要输入菜品名称、菜品分类、菜品状态进行查询

代码开发

设计DTO类

根据菜品分页查询接口定义设计对应的DTO:

在sky-pojo模块中

package com.sky.dto;import lombok.Data;import java.io.Serializable;@Data
public class DishPageQueryDTO implements Serializable {private int page;private int pageSize;private String name;//分类idprivate Integer categoryId;//状态 0表示禁用 1表示启用private Integer status;}
设计VO类

根据菜品分页查询接口定义设计对应的VO:(在返回的数据中categoryName属于另外一个文件中,需要使用VO转换属性为json,方便前端展示)

在sky-pojo模块中

package com.sky.vo;import com.sky.entity.DishFlavor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DishVO implements Serializable {private Long id;//菜品名称private String name;//菜品分类idprivate Long categoryId;//菜品价格private BigDecimal price;//图片private String image;//描述信息private String description;//0 停售 1 起售private Integer status;//更新时间private LocalDateTime updateTime;//分类名称private String categoryName;//菜品关联的口味private List<DishFlavor> flavors = new ArrayList<>();//private Integer copies;
}
Controller层

根据接口定义创建DishController的page分页查询方法:

//菜品分页查询@GetMapping("/page")@ApiOperation("菜品分页查询")public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){log.info("菜品分页查询:{}",dishPageQueryDTO);PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);return Result.success(pageResult);}
Service层接口

在 DishService 中扩展分页查询方法:

 //菜品分页查询PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
Service层实现类

在 DishServiceImpl 中实现分页查询方法:

 //菜品分页查询@Overridepublic PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());Page<DishVO> page= dishMapper.pageQuery(dishPageQueryDTO);return new PageResult(page.getTotal(),page.getResult());}
Mapper层

在 DishMapper 接口中声明 pageQuery 方法:

 //菜品分页查询Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);

在 DishMapper.xml 中编写SQL:

 <select id="pageQuery" resultType="com.sky.vo.DishVO">select d.* ,c.name as categoryName from dish d left outer join category c on d.category_id=c.id<where><if test="name!=null">and d.name like concat('%',#{name},'%')</if><if test="categoryId!=null">and d.category_id=#{categoryId}</if><if test="status!=null">and d.status=#{status}</if></where>order by d.create_time desc</select>

功能测试

删除菜品

需求设计和分析

业务规则:

  • 可以一次删除一个菜品,也可以批量删除菜品

  • 起售中的菜品不能删除

  • 被套餐关联的菜品不能删除

  • 删除菜品后,关联的口味数据也需要删除掉

代码开发

Controller层

根据删除菜品的接口定义在DishController中创建方法:

//菜品批量删除@DeleteMapping@ApiOperation("菜品批量删除")public Result<String> delete(@RequestParam List<Long> ids){//@RequestParam注解使用mvc框架,可以获取到参数1,2,3中的变量值log.info("菜品批量删除,{}",ids);dishService.deleteBatch(ids);return Result.success();}
Service层接口

在DishService接口中声明deleteBatch方法:

  //菜品批量删除void deleteBatch(List<Long> ids);
Service层实现类

在DishServiceImpl中实现deleteBatch方法:

  //菜品批量删除@Transactional//事务public void deleteBatch(List<Long> ids) {//判断当前菜品是否能够删除---是否存在起售中的菜品for (Long id : ids) {//根据主键查询菜品Dish dish=dishMapper.getById(id);//查询是否起售if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)){//当前菜品处于起售中,不能删除throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);}}//判断当前菜品是否能够删除---是否被套餐关联了List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishIds(ids);if (setmealIds!=null&& !setmealIds.isEmpty()){//当前菜品被套餐关联了,不能删除throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);}//删除菜品表中的菜品数据for (Long id : ids) {dishMapper.deleteById(id);//删除菜品关联的口味数据dishFlavorMapper.deleteByDishId(id);}
Mapper层

在DishMapper中声明getById方法,并配置SQL:

 //根据主键查询菜品@Select("select *from dish where id=#{id}")Dish getById(Long id);

创建SetmealDishMapper,声明getSetmealIdsByDishIds方法,并在xml文件中编写SQL:

package com.sky.mapper;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface SetmealDishMapper {//根据菜品id查询对应的套餐idList<Long> getSetmealIdsByDishIds(List<Long> dishIds);}

SetmealDishMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.SetmealDishMapper"><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>
</mapper>

在DishMapper.java中声明deleteById方法并配置SQL:

    //根据主键删除菜品数据@Delete("delete from dish where id=#{id}")void deleteById(Long id);

在DishFlavorMapper中声明deleteByDishId方法并配置SQL:

 //根据菜品id删除对应的口味数据@Delete("delete from dish_flavor where dish_id=#{dishId}")void deleteByDishId(Long dishId);

 性能优化

在DishServiceImpl中,删除菜品是一条一条传送执行的,大大降低了执行效率,原代码如下:

//删除菜品表中的菜品数据for (Long id : ids) {dishMapper.deleteById(id);//删除菜品关联的口味数据dishFlavorMapper.deleteByDishId(id);}

为了提高性能,进行修改,使用动态sql执行删除操作

 //根据菜品id集合批量删除菜品数据//sql:delete from dish where id in(?,?,?)dishMapper.deleteByIds(ids);//根据菜品id集合批量删除关联的口味数据//sql:delete from dish_flavor where dish_id in(?,?,?)dishMapper.deleteByDishIds(ids);

在DishMapper中

 //根据菜品id集合批量删除菜品void deleteByIds(List<Long> ids);//根据菜品id集合批量删除关联的口味数据void deleteByDishIds(List<Long> dishId);

在DishMapper.xml中

 <delete id="deleteByIds">delete from dish where id in<foreach collection="ids" open="(" separator="," close=")" item="id">#{id}</foreach></delete><delete id="deleteByDishIds">delete from dish_flavor where dish_id in<foreach collection="dishIds" open="(" close=")" separator="," item="dishId">#{dishId}</foreach></delete>

功能测试

进行前后端联调,删除成功

修改菜品

需求分析和设计

接口:

  • 根据id查询菜品

  • 根据类型查询分类(已实现)

  • 文件上传(已实现)

  • 修改菜品

代码开发

根据id查询菜品实现

Controller层

根据id查询菜品的接口定义在DishController中创建方法:

 //根据id查询菜品@GetMapping("/{id}")@ApiOperation("根据id查询菜品")public  Result<DishVO> getById(@PathVariable Long id){log.info("根据id查询菜品:{}",id);DishVO dishVO=dishService.getByIdWithFlavor(id);return  Result.success(dishVO);}
Service层接口

在DishService接口中声明getByIdWithFlavor方法:

 //根据id查询菜品和对应的口味数据DishVO getByIdWithFlavor(Long id);
Service层实现类

在DishServiceImpl中实现getByIdWithFlavor方法:

//根据id查询菜品和对应的口味数据@Overridepublic DishVO getByIdWithFlavor(Long id) {//根据id查询菜品数据Dish dish=dishMapper.getById(id);//根据菜品id查询口味数据List<DishFlavor> dishFlavors=dishFlavorMapper.getByDishId(id);//将查询到的数据封装到VODishVO dishVO = new DishVO();BeanUtils.copyProperties(dish,dishVO);dishVO.setFlavors(dishFlavors);return dishVO;}
Mapper层

在DishFlavorMapper中声明getByDishId方法,并配置SQL:

//根据id查询对应的口味数据@Select("select *from dish_flavor where dish_id=#{dishId}")List<DishFlavor> getByDishId(Long dishId);

修改菜品实现

Controller层

根据修改菜品的接口定义在DishController中创建方法:

 //修改菜品@PutMapping@ApiOperation("修改菜品")public Result<String> update(@RequestBody DishDTO dishDTO){log.info("修改菜品:{}",dishDTO);dishService.updateWithFlavor(dishDTO);return Result.success();}
Service层接口

在DishService接口中声明updateWithFlavor方法:

 //根据id修改菜品基本信息和对应的口味数据void updateWithFlavor(DishDTO dishDTO);
Service层实现类

在DishServiceImpl中实现updateWithFlavor方法:

//根据id修改菜品基本信息和对应的口味信息@Overridepublic void updateWithFlavor(DishDTO dishDTO) {Dish dish = new Dish();BeanUtils.copyProperties(dishDTO,dish);//修改菜品表基本信息dishMapper.update(dish);//删除原有的口味数据dishFlavorMapper.deleteByDishId(dishDTO.getId());//重新插入口味数据List<DishFlavor> flavors = dishDTO.getFlavors();if (flavors!=null&& !flavors.isEmpty()){flavors.forEach(dishFlavor -> {dishFlavor.setDishId(dishDTO.getId());});//向口味表插入n条数据dishFlavorMapper.insertBatch(flavors);}}
Mapper层

在DishMapper中,声明update方法:

 //根据id动态修改菜品数据@AutoFill(value = OperationType.UPDATE)void update(Dish dish);

并在DishMapper.xml文件中编写SQL:

 <update id="update">update dish<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>

功能测试

相关文章:

  • spring boot 项目中redis的使用,key=value值 如何用命令行来查询并设置值。
  • Python编码系列—Python访问者模式:为对象结构添加新功能的艺术
  • 如何快速免费搭建自己的Docker私有镜像源来解决Docker无法拉取镜像的问题(搭建私有镜像源解决群晖Docker获取注册表失败的问题)
  • vue3 商城系统中的 sku 功能的实现
  • 优秀在线 notion 头像制作工具分享-Notion Avatar Maker
  • 35 | 实战一(下):手把手带你将ID生成器代码从“能用”重构为“好用”
  • Chromium 设置页面打开系统代理源码分析c++
  • C语言 | Leetcode C语言题解之第443题压缩字符串
  • 《中国电子报》报道: 安宝特AR为产线作业者的“秘密武器
  • 桥接模式和NET模式的区别
  • 今年Java回暖了吗
  • Python模拟真人鼠标轨迹算法
  • 帮儿女带孩子的老人,都有以下几种共性
  • Linux基础入门 --12 DAY(SHELL脚本编程基础)
  • Go基础学习06-Golang标准库container/list(双向链表)深入讲解;延迟初始化技术;Element;List;Ring
  • 11111111
  • Android 控件背景颜色处理
  • Angular 4.x 动态创建组件
  • CentOS6 编译安装 redis-3.2.3
  • CSS3 变换
  • FastReport在线报表设计器工作原理
  • JavaScript 奇技淫巧
  • javascript数组去重/查找/插入/删除
  • PV统计优化设计
  • Redis学习笔记 - pipline(流水线、管道)
  • Redis字符串类型内部编码剖析
  • STAR法则
  • SwizzleMethod 黑魔法
  • 如何在GitHub上创建个人博客
  • 试着探索高并发下的系统架构面貌
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 一些关于Rust在2019年的思考
  • ​【经验分享】微机原理、指令判断、判断指令是否正确判断指令是否正确​
  • ​linux启动进程的方式
  • ​TypeScript都不会用,也敢说会前端?
  • ​补​充​经​纬​恒​润​一​面​
  • # 数仓建模:如何构建主题宽表模型?
  • (07)Hive——窗口函数详解
  • (2024)docker-compose实战 (9)部署多项目环境(LAMP+react+vue+redis+mysql+nginx)
  • (4)STL算法之比较
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (含笔试题)深度解析数据在内存中的存储
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (一)kafka实战——kafka源码编译启动
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (转载)Linux网络编程入门
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .net core 管理用户机密
  • .Net Core 中间件与过滤器
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】