• 基于Springboot外卖系统18:套餐分页查询模块+删除套餐+多数据表同步


    1. 套餐分页查询模块

    1.1 需求分析

    系统中的套餐数据很多的时候,如果在一个页面中全部展示出来会显得比较乱,不便于查看,所以一般的系统中都会以分页的方式来展示列表数据。

    在进行套餐数据的分页查询时,除了传递分页参数以外,还可以传递一个可选的条件(套餐名称)。查询返回的字段中,包含套餐的基本信息之外,还有一个套餐的分类名称,在查询时,需要关联查询这个字段。

    1.2 套餐分页查询时前端页面和服务端的前端页面交互过程分析

    1). 访问页面(backend/page/combo/list.html),页面加载时,会自动发送ajax请求,将分页查询参数(page、pageSize、name)提交到服务端,获取分页数据

     2). 在列表渲染展示时,页面发送请求,请求服务端进行图片下载,用于页面图片展示(已实现)

    已经实现文件下载功能,因此主要实现列表分页查询功能, 具体的请求信息如下:

    请求说明
    请求方式GET
    请求路径/setmeal/page
    请求参数?page=1&pageSize=10&name=xxx

    1.3 套餐分页查询中基本信息查询流程

    1). 构建分页条件对象

    2). 构建查询条件对象,如果传递了套餐名称,根据套餐名称模糊查询, 并对结果按修改时间降序排序

    3). 执行分页查询

    4). 组装数据并返回

    1.4 代码编写

    在查询套餐信息时, 只包含套餐的基本信息, 并不包含套餐的分类名称,因此在这里查询到套餐的基本信息后, 还需要根据分类ID(categoryId),查询套餐分类名称(categoryName),并最终将套餐的基本信息及分类名称信息封装到SetmealDto中。

    1.4.1 引入SetmealDto

    1. package com.itheima.reggie.dto;
    2. import com.itheima.reggie.entity.Setmeal;
    3. import com.itheima.reggie.entity.SetmealDish;
    4. import lombok.Data;
    5. import java.util.List;
    6. @Data
    7. public class SetmealDto extends Setmeal {
    8. private List setmealDishes;
    9. private String categoryName;
    10. }

    1.4.2 SetmealController中page()代码编写

    1. package com.itheima.reggie.controller;
    2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import com.itheima.reggie.common.R;
    5. import com.itheima.reggie.dto.SetmealDto;
    6. import com.itheima.reggie.entity.Category;
    7. import com.itheima.reggie.entity.Setmeal;
    8. import com.itheima.reggie.service.CategoryService;
    9. import com.itheima.reggie.service.SetmealDishService;
    10. import com.itheima.reggie.service.SetmealService;
    11. import lombok.extern.slf4j.Slf4j;
    12. import org.apache.commons.lang.StringUtils;
    13. import org.springframework.beans.BeanUtils;
    14. import org.springframework.beans.factory.annotation.Autowired;
    15. import org.springframework.web.bind.annotation.*;
    16. import java.util.List;
    17. import java.util.stream.Collectors;
    18. /**
    19. * Description: 套餐管理
    20. * 不仅需要保存套餐的基本信息,还需要保存套餐关联的菜品数据,所以需要再该方法中调用业务层方法,完成两块数据的保存。
    21. * @version 1.0
    22. * @date 2022/8/19 15:37
    23. */
    24. @RestController
    25. @RequestMapping("/setmeal")
    26. @Slf4j
    27. public class SetmealController {
    28. @Autowired
    29. private SetmealService setmealService;
    30. @Autowired
    31. private CategoryService categoryService;
    32. @Autowired
    33. private SetmealDishService setmealDishService;
    34. @PostMapping
    35. // 页面传递的数据是json格式,需要在方法形参前面加上@RequestBody注解, 完成参数封装。
    36. public R save(@RequestBody SetmealDto setmealDto){
    37. /**@Description: 新增套餐
    38. * @version v1.0
    39. * @author LiBiGo
    40. * @date 2022/8/19 16:04
    41. */
    42. log.info("套餐信息:{}",setmealDto);
    43. setmealService.saveWithDish(setmealDto);
    44. return R.success("新增套餐成功");
    45. }
    46. @GetMapping("/page")
    47. public R page(int page,int pageSize,String name){
    48. /**@Description: 套餐分页查询
    49. * @author LiBiGo
    50. * @date 2022/8/21 10:40
    51. */
    52. // 分页构造器对象
    53. Page pageInfo = new Page<>(page,pageSize);
    54. Page dtoPage = new Page<>();
    55. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
    56. // 添加查询条件,根据name进行like模糊查询
    57. queryWrapper.like(name!=null,Setmeal::getName,name);
    58. // 排序条件,根据更新时间进行降序排序
    59. queryWrapper.orderByDesc(Setmeal::getUpdateTime);
    60. setmealService.page(pageInfo,queryWrapper);
    61. // 拷贝对象
    62. BeanUtils.copyProperties(pageInfo,dtoPage,"record");
    63. List records = pageInfo.getRecords();
    64. List list = records.stream().map((item) -> {
    65. SetmealDto setmealDto = new SetmealDto();
    66. //对象拷贝
    67. BeanUtils.copyProperties(item,setmealDto);
    68. //分类id
    69. Long categoryId = item.getCategoryId();
    70. //根据分类id查询分类对象
    71. Category category = categoryService.getById(categoryId);
    72. if(category != null){
    73. //分类名称
    74. String categoryName = category.getName();
    75. setmealDto.setCategoryName(categoryName);
    76. }
    77. return setmealDto;
    78. }).collect(Collectors.toList());
    79. dtoPage.setRecords(list);
    80. return R.success(dtoPage);
    81. }
    82. }

    1.5 功能测试

    代码完善后,重启服务,测试列表查询,我们发现, 抓取浏览器的请求响应数据,我们可以获取到套餐分类名称categoryName,也可以在列表页面展示出来 。


    2. 删除套餐

    2.1 需求分析

    在套餐管理列表页面,点击删除按钮,可以删除对应的套餐信息。也可以通过复选框选择多个套餐,点击批量删除按钮一次删除多个套餐。

    对于状态为售卖中的套餐不能删除,需要先停售,然后才能删除。

    2.2 删除套餐时前端页面和服务端的前端页面交互过程分析

    1). 点击删除, 删除单个套餐时,页面发送ajax请求,根据套餐id删除对应套餐

     2). 删除多个套餐时,页面发送ajax请求,根据提交的多个套餐id删除对应套餐

    开发删除套餐功能,其实就是在服务端编写代码去处理前端页面发送的这2次请求即可,一次请求为根据ID删除,一次请求为根据ID批量删除。

    观察删除单个套餐和批量删除套餐的请求信息可以发现,两种请求的地址请求方式都是相同的,不同的则是传递的id个数,所以在服务端可以提供一个方法来统一处理。

    具体的请求信息如下:

    请求说明
    请求方式DELETE
    请求路径/setmeal
    请求参数?ids=1423640210125656065,1423338765002256385

    2.3 代码开发

    在服务端的逻辑中, 删除套餐时不仅要删除套餐, 还要删除套餐与菜品的关联关系。

    1). 在SetmealController中创建delete方法

    1. /**
    2. * 删除套餐
    3. * @param ids
    4. * @return
    5. */
    6. @DeleteMapping
    7. public R delete(@RequestParam List ids){
    8. log.info("ids:{}",ids);
    9. return R.success("套餐数据删除成功");
    10. }

     2). SetmealService接口定义方法removeWithDish

    1. package com.itheima.reggie.service;
    2. import com.baomidou.mybatisplus.extension.service.IService;
    3. import com.itheima.reggie.dto.SetmealDto;
    4. import com.itheima.reggie.entity.Setmeal;
    5. import java.util.List;
    6. public interface SetmealService extends IService {
    7. // 新增套餐,同时需要保存套餐和菜品的关联关系
    8. public void saveWithDish(SetmealDto setmealDto);
    9. // 删除套餐,同时需要删除套餐和菜菜品的关联
    10. public void removeWithDish(List ids);
    11. }

    3). SetmealServiceImpl中实现方法removeWithDish

    A. 查询该批次套餐中是否存在售卖中的套餐, 如果存在, 不允许删除

    B. 删除套餐数据

    C. 删除套餐关联的菜品数据

    代码实现:

    1. package com.itheima.reggie.service.impl;
    2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    4. import com.itheima.reggie.common.CustomException;
    5. import com.itheima.reggie.dto.SetmealDto;
    6. import com.itheima.reggie.entity.Setmeal;
    7. import com.itheima.reggie.entity.SetmealDish;
    8. import com.itheima.reggie.mapper.SetmealMapper;
    9. import com.itheima.reggie.service.SetmealDishService;
    10. import com.itheima.reggie.service.SetmealService;
    11. import lombok.extern.slf4j.Slf4j;
    12. import org.springframework.beans.factory.annotation.Autowired;
    13. import org.springframework.stereotype.Service;
    14. import org.springframework.transaction.annotation.Transactional;
    15. import java.util.List;
    16. import java.util.stream.Collectors;
    17. /**
    18. * Description: new java files header..
    19. *
    20. * @author w
    21. * @version 1.0
    22. * @date 2022/8/16 10:17
    23. */
    24. @Service
    25. @Slf4j
    26. public class SetmealServiceImpl extends ServiceImpl implements SetmealService {
    27. @Autowired
    28. private SetmealDishService setmealDishService ;
    29. @Transactional
    30. @Override
    31. public void saveWithDish(SetmealDto setmealDto) {
    32. /**@Description: 新增套餐,同时需要保存套餐和菜品的关联关系
    33. *
    34. * A. 保存套餐基本信息
    35. * B. 获取套餐关联的菜品集合,并为集合中的每一个元素赋值套餐ID(setmealId)
    36. * C. 批量保存套餐关联的菜品集合
    37. *
    38. * @author LiBiGo
    39. * @date 2022/8/19 16:10
    40. */
    41. // 保存套餐的基本信息,操作setmeal,执行insert操作
    42. this.save(setmealDto);
    43. List setmealDishes = setmealDto.getSetmealDishes();
    44. setmealDishes.stream().map((item) -> {
    45. item.setSetmealId(setmealDto.getId());
    46. return item;
    47. }).collect(Collectors.toList());
    48. // 保存套餐和菜品的关联信息,操作setmeal_dish,执行insert操作
    49. setmealDishService.saveBatch(setmealDishes);
    50. }
    51. @Override
    52. @Transactional
    53. // 删除套餐,同时需要删除套餐和菜菜品的关联
    54. public void removeWithDish(List ids) {
    55. //查询套餐状态,确定是否可用删除
    56. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
    57. queryWrapper.in(Setmeal::getId,ids);
    58. queryWrapper.eq(Setmeal::getStatus,1);
    59. int count = this.count(queryWrapper);
    60. if(count > 0){
    61. //如果不能删除,抛出一个业务异常
    62. throw new CustomException("套餐正在售卖中,不能删除");
    63. }
    64. //如果可以删除,先删除套餐表中的数据---setmeal
    65. this.removeByIds(ids);
    66. //delete from setmeal_dish where setmeal_id in (1,2,3)
    67. LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
    68. lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
    69. //删除关系表中的数据----setmeal_dish
    70. setmealDishService.remove(lambdaQueryWrapper);
    71. }
    72. }

     4). 完善SetmealController代码

    1. package com.itheima.reggie.controller;
    2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import com.itheima.reggie.common.R;
    5. import com.itheima.reggie.dto.SetmealDto;
    6. import com.itheima.reggie.entity.Category;
    7. import com.itheima.reggie.entity.Setmeal;
    8. import com.itheima.reggie.service.CategoryService;
    9. import com.itheima.reggie.service.SetmealDishService;
    10. import com.itheima.reggie.service.SetmealService;
    11. import lombok.extern.slf4j.Slf4j;
    12. import org.apache.commons.lang.StringUtils;
    13. import org.springframework.beans.BeanUtils;
    14. import org.springframework.beans.factory.annotation.Autowired;
    15. import org.springframework.web.bind.annotation.*;
    16. import java.util.List;
    17. import java.util.stream.Collectors;
    18. /**
    19. * Description: 套餐管理
    20. * 不仅需要保存套餐的基本信息,还需要保存套餐关联的菜品数据,所以需要再该方法中调用业务层方法,完成两块数据的保存。
    21. * @version 1.0
    22. * @date 2022/8/19 15:37
    23. */
    24. @RestController
    25. @RequestMapping("/setmeal")
    26. @Slf4j
    27. public class SetmealController {
    28. @Autowired
    29. private SetmealService setmealService;
    30. @Autowired
    31. private CategoryService categoryService;
    32. @Autowired
    33. private SetmealDishService setmealDishService;
    34. @PostMapping
    35. // 页面传递的数据是json格式,需要在方法形参前面加上@RequestBody注解, 完成参数封装。
    36. public R save(@RequestBody SetmealDto setmealDto){
    37. /**@Description: 新增套餐
    38. * @version v1.0
    39. * @author LiBiGo
    40. * @date 2022/8/19 16:04
    41. */
    42. log.info("套餐信息:{}",setmealDto);
    43. setmealService.saveWithDish(setmealDto);
    44. return R.success("新增套餐成功");
    45. }
    46. @GetMapping("/page")
    47. public R page(int page,int pageSize,String name){
    48. /**@Description: 套餐分页查询
    49. * @author LiBiGo
    50. * @date 2022/8/21 10:40
    51. */
    52. // 分页构造器对象
    53. Page pageInfo = new Page<>(page,pageSize);
    54. Page dtoPage = new Page<>();
    55. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
    56. // 添加查询条件,根据name进行like模糊查询
    57. queryWrapper.like(name!=null,Setmeal::getName,name);
    58. // 排序条件,根据更新时间进行降序排序
    59. queryWrapper.orderByDesc(Setmeal::getUpdateTime);
    60. setmealService.page(pageInfo,queryWrapper);
    61. // 拷贝对象
    62. BeanUtils.copyProperties(pageInfo,dtoPage,"record");
    63. List records = pageInfo.getRecords();
    64. List list = records.stream().map((item) -> {
    65. SetmealDto setmealDto = new SetmealDto();
    66. //对象拷贝
    67. BeanUtils.copyProperties(item,setmealDto);
    68. //分类id
    69. Long categoryId = item.getCategoryId();
    70. //根据分类id查询分类对象
    71. Category category = categoryService.getById(categoryId);
    72. if(category != null){
    73. //分类名称
    74. String categoryName = category.getName();
    75. setmealDto.setCategoryName(categoryName);
    76. }
    77. return setmealDto;
    78. }).collect(Collectors.toList());
    79. dtoPage.setRecords(list);
    80. return R.success(dtoPage);
    81. }
    82. @DeleteMapping
    83. public R delete(@RequestParam List ids){
    84. /**@Description: 删除套餐
    85. * @author LiBiGo
    86. * @date 2022/8/21 11:35
    87. */
    88. log.info("ids:{}",ids);
    89. setmealService.removeWithDish(ids);
    90. return R.success("套餐数据删除成功");
    91. }
    92. @GetMapping("/list")
    93. public R> list(Setmeal setmeal) {
    94. log.info("setmeal:{}", setmeal);
    95. //条件构造器
    96. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
    97. queryWrapper.like(StringUtils.isNotEmpty(setmeal.getName()), Setmeal::getName, setmeal.getName());
    98. queryWrapper.eq(null != setmeal.getCategoryId(), Setmeal::getCategoryId, setmeal.getCategoryId());
    99. queryWrapper.eq(null != setmeal.getStatus(), Setmeal::getStatus, setmeal.getStatus());
    100. queryWrapper.orderByDesc(Setmeal::getUpdateTime);
    101. return R.success(setmealService.list(queryWrapper));
    102. }
    103. }

    3.4 功能测试

    代码完善后,重启服务,测试套餐的删除功能,主要测试以下几种情况。

    1). 删除正在启用的套餐,报错

    2). 执行批量操作, 删除两条记录, 一个启售的, 一个停售的

  • 相关阅读:
    二:OpenCV图片叠加逻辑运算
    使用J2EE 登录实例开发
    网络标准之:永远是1.0版本的MIME
    金融的本质是什么?
    SpringBoot+自定义注解+AOP高级玩法打造通用开关
    线性回归法学习笔记
    InVEST模型在固碳、生境质量、产水等领域案例分析
    Linux磁盘分区与挂载
    SpringCloud 组件Gateway服务网关【断言工厂&过滤器工厂】
    别让“防御性编程”毁了我们的职业
  • 原文地址:https://blog.csdn.net/qq_39237205/article/details/126449551