猿创征文|瑞吉外卖——管理端_菜品管理_1

个人名片:

博主:酒徒ᝰ.
专栏:瑞吉外卖
个人简介沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志真正的程序员不看参考手册,新手和胆小鬼才会看。

本项目基于B站黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis plus开发核心技术的真java实战项目。

视频链接【黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis
plus开发核心技术的真java实战项目】 https://www.bilibili.com/video/BV13a411q753?
点击观看

目录

  • 一、页面显示
    • 1.全部查询
    • 2.输入框查询
  • 二、新增菜品
    • 1.显示页面
      • 1.菜品分类
      • 2.文件上传
    • 2.保存

在页面显示中的输入框查询,删除与停售和起售中的批量与单个。都是在原代码基础上进行修改。不能另写代码,会报错。

因为菜品管理太多了,所有分成三部分来写了。这是第一部分。

一、页面显示

1.全部查询

在这里插入图片描述

分析:dish地址,GET方式,page地址,page,pageSize属性

package com.itheima.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishFlavorService;
import com.itheima.reggie.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;/*** <p>* 菜品管理 前端控制器* </p>** @author 酒徒* @since 2022-09-04*/
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {@Autowiredprivate IDishService dishService;@Autowiredprivate ICategoryService categoryService;/*** 页面呈现* @param page* @param pageSize* @return*/@GetMapping("/page")public R<Page> page(int page, int pageSize){Page<Dish> pageInfo = new Page<>(page, pageSize);//查询全部LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.orderByDesc(Dish::getUpdateTime);dishService.page(pageInfo, queryWrapper);return R.success(pageInfo);}
}

这是发现菜品没有图片,这是缺少文件的上传与下载。

在这里插入图片描述

分析:common地址,GET方式,download地址,name属性
在controller模块中新建CommonController类。写文件的上传与下载代码。

package com.itheima.reggie.controller;import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;/*** <p>* 文件的上传与下载 前端控制器* </p>** @author 酒徒* @since 2022-09-04*/
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {@Value("${reggie.path}")private String basePath;/*** 文件下载——图片在页面显示* @param name* @param response*/@GetMapping("/download")public void download(@PathParam("name") String name, HttpServletResponse response){FileInputStream fileInputStream = null;ServletOutputStream outputStream = null;try {//文件输入流  图片文件的地址fileInputStream = new FileInputStream(new File(basePath + name));//文件输出流 将本地图片文件输出到浏览器页面outputStream = response.getOutputStream();//设置输出是文件的类型response.setContentType("image/jpeg");//图片类型//输出图片文件  以读写的方式int len = 0;byte[] buff = new byte[1024];while((len = fileInputStream.read(buff)) != -1){outputStream.write(buff, 0 ,len);outputStream.flush();}} catch (IOException e) {throw new RuntimeException(e);}finally {try {outputStream.close();fileInputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}
}

完成后仍然发现一个小问题,那就是菜品分类栏不显示。查看日志发现是categoryId对应的分类名称没有查。
这里就需要结合dish表和category表同时使用,也就是DIshDto,在这个类中包括了所有的dish,也拥有categoryname。

/*** 页面呈现* @param page* @param pageSize* @return*/
@GetMapping("/page")
public R<Page> page(int page, int pageSize){Page<Dish> dishPage = new Page<>(page, pageSize);Page<DishDto> dishDtoPage = new Page<>(page, pageSize);//查询全部LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.orderByDesc(Dish::getUpdateTime);dishService.page(dishPage, queryWrapper);//获取dish的所有数据List<Dish> records = dishPage.getRecords();//将dish的所有数据拷贝到dishDto中BeanUtils.copyProperties(dishPage, dishDtoPage);//使用流的形式给每一个dishDto进行categoryname赋值List<DishDto> list = records.stream().map((item) -> {//item与dish内容相同DishDto dishDto = new DishDto();//1.拷贝dish到dishdto中BeanUtils.copyProperties(item, dishDto);//2.根据dish中categoryId在category表中查询name,在赋值给dishDto中的categoryNameLambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();wrapper.eq(Category::getId, item.getCategoryId());Category category = categoryService.getOne(wrapper);//设置dishDto中的categoryName值  先判断category是否为空if (category != null){dishDto.setCategoryName(category.getName());}else {dishDto.setCategoryName("不存在");}return dishDto;}).collect(Collectors.toList());dishDtoPage.setRecords(list);return R.success(dishDtoPage);
}

2.输入框查询

在这里插入图片描述

相对于全部查询来说,多了一个name属性。
比较简单,在查询中添加name是否为空的判断。添加如下内容。

if (name != null){queryWrapper.like(Dish::getName, name);
}

完整代码为:

package com.itheima.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishFlavorService;
import com.itheima.reggie.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;/*** <p>* 菜品管理 前端控制器* </p>** @author 酒徒* @since 2022-09-04*/
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {@Autowiredprivate IDishService dishService;@Autowiredprivate ICategoryService categoryService;/*** 页面呈现* @param page* @param pageSize* @return*/@GetMapping("/page")public R<Page> page(int page, int pageSize, String name){Page<Dish> dishPage = new Page<>(page, pageSize);Page<DishDto> dishDtoPage = new Page<>(page, pageSize);//查询全部LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();if (name != null){queryWrapper.like(Dish::getName, name);}queryWrapper.orderByDesc(Dish::getUpdateTime);dishService.page(dishPage, queryWrapper);//获取dish的所有数据List<Dish> records = dishPage.getRecords();//将dish的所有数据拷贝到dishDto中BeanUtils.copyProperties(dishPage, dishDtoPage);//使用流的形式给每一个dishDto进行categoryname赋值List<DishDto> list = records.stream().map((item) -> {//item与dish内容相同DishDto dishDto = new DishDto();//1.拷贝dish到dishdto中BeanUtils.copyProperties(item, dishDto);//2.根据dish中categoryId在category表中查询name,在赋值给dishDto中的categoryNameLambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();wrapper.eq(Category::getId, item.getCategoryId());Category category = categoryService.getOne(wrapper);//设置dishDto中的categoryName值  先判断category是否为空if (category != null){dishDto.setCategoryName(category.getName());}else {dishDto.setCategoryName("不存在");}return dishDto;}).collect(Collectors.toList());dishDtoPage.setRecords(list);return R.success(dishDtoPage);}
}

二、新增菜品

1.显示页面

在这里插入图片描述

分析:category地址,GET方式,list地址,type属性。
先进行测试,用以下代码进行。

@GetMapping("/list")
public R<String> list(@PathParam("type") String type){log.info("type:{}",type);return R.success("");
}

type为菜品和套餐的区分:1 菜品分类 2 套餐分类

在这里插入图片描述

从页面中可以分析出,需要补充的为菜品分类和文件上传

1.菜品分类

菜品分类就是categoryName属性。

/*** 页面显示——菜品分类显示* @param type* @param category* @return*/
@GetMapping("/list")
public R<List<Category>> list(@PathParam("type") String type, Category category){//log.info("type:{}",type);//查询Category表中的所有nameLambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(Category::getType, type);queryWrapper.orderByDesc(Category::getUpdateTime);List<Category> list = categoryService.list(queryWrapper);return R.success(list);
}

2.文件上传

在这里插入图片描述

分析:common地址,POST方式,upload地址。

/*** 文件的下载* @param file* @return*/
@PostMapping("/upload")
public R<String> upload(MultipartFile file){//1.获取文件名,生成新的名字,确保文件名具有唯一性//获取文件名String originalFilename = file.getOriginalFilename();//获取文件后缀名  .jpg类型String substring = originalFilename.substring(originalFilename.lastIndexOf("."));//生成一个新的文件名  唯一性String fileName = UUID.randomUUID().toString();//2.设置文件存储地址,若不存在,则生成File dir = new File(basePath + fileName);if (!dir.exists()){dir.mkdirs();}//3.将前端传过来的文件存到本地try {file.transferTo(dir);} catch (IOException e) {throw new RuntimeException(e);}return R.success(fileName);
}

2.保存

在这里插入图片描述

分析:dish地址,POST方式
第一次直接进行保存,发现出现一些问题。

@PostMapping
public R<String> dish(@RequestBody DishDto dishDto){log.info("dishDto:{}",dishDto);//dishDto:DishDto(flavors=[DishFlavor(id=null, dishId=null, name=甜味, value=["无糖","少糖","半糖","多糖","全糖"], createTime=null, updateTime=null, createUser=null, updateUser=null, isDeleted=null)], categoryName=null, copies=null)dishService.save(dishDto);return R.success("新增菜品成功");
}

没有保存DishFlavor,也就是各种菜品口味信息。
这些保存方法属于业务操作,最好写在service实现类中,增加代码可读性。
这里和视频中的出入比较大(本人写的较为繁琐),建议参考视频学习。

/*** 添加菜品* @param request* @param dishDto* @return*/
@PostMapping
public R<String> dish(HttpServletRequest request, @RequestBody DishDto dishDto){log.info("dishDto:{}",dishDto);//dishDto:DishDto(flavors=[DishFlavor(id=null, dishId=null, name=甜味, value=["无糖","少糖","半糖","多糖","全糖"], createTime=null, updateTime=null, createUser=null, updateUser=null, isDeleted=null)], categoryName=null, copies=null)dishService.saveDishFlavor(request, dishDto);return R.success("新增菜品成功");
}

在IDishService中创建saveDishFlavor方法

package com.itheima.reggie.service;import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.baomidou.mybatisplus.extension.service.IService;import javax.servlet.http.HttpServletRequest;/*** <p>* 菜品管理 服务类* </p>** @author 酒徒* @since 2022-09-04*/
public interface IDishService extends IService<Dish> {void saveDishFlavor(HttpServletRequest request, DishDto dishDto);
}

在IDishServiceImpl中实现方法

package com.itheima.reggie.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.IDishFlavorService;
import com.itheima.reggie.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;/*** <p>* 菜品管理 服务实现类* </p>** @author 酒徒* @since 2022-09-04*/
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {@Autowiredprivate IDishFlavorService dishFlavorService;@Override@Transactionalpublic void saveDishFlavor(HttpServletRequest request, DishDto dishDto) {//保存dish的基本信息到dish表this.save(dishDto);//存储dishIdList<DishFlavor> flavors = dishDto.getFlavors();flavors = flavors.stream().map((item) -> {item.setDishId(dishDto.getId());return item;}).collect(Collectors.toList());dishFlavorService.saveBatch(flavors);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1379718.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

【CSS】CSS 布局——常规流布局

<h1>基础文档流</h1><p>我是一个基本的块级元素。我的相邻块级元素在我的下方另起一行。</p><p>默认情况下&#xff0c;我们会占据父元素 100%的宽度&#xff0c;并且我们的高度与我们的子元素内容一样高。我们的总宽度和高度是我们的内容 内边距…

SpringBoot整合、SpringBoot与异步任务

目录 一、背景描述二、简单使用方法三、原理五、使用自定义线程池六、Async失效情况 一、背景描述 java 的代码是同步顺序执行&#xff0c;当我们需要执行异步操作时我们通常会去创建一个新线程去执行。比如new Thread()。start()&#xff0c;或者使用线程池线程池 new Thread…

stm工程文件夹

STM32工程文件构成 从下图可以看出我们的工程目录是由CORE、OBJ、STM32F10x_FWLib、USER、SYSTEM以及HARDWARE文件夹组成的。此外还有一个文本文档README.TXT、以及一个Windows 批处理文件 (.bat)keilkilll.bat。 1、CORE文件夹 CORE文件夹下一共有三个文件&#xff0c;它们分…

STL文件及其读取

1引言 STL(Stereo lithographic)文件格式是美国3D SYSTEMS公司提出的三维实体造型系统的一个接口标准&#xff0c;其接口格式规范。采用三角形面片离散地近似表示三维模型&#xff0c;目前已被工业界认为是快速成形(rapid prototypi ng)领域的标准描述文件格式。在逆向工程、有…

Python-OpenCV中的图像处理-直方图

Python-OpenCV中的图像处理-直方图 直方图统计直方图绘制直方图Matplotlib绘制灰度直方图Matplotlib绘制RGB直方图 使用掩膜统计直方图直方图均衡化Numpy图像直方图均衡化OpenCV中的直方图均衡化CLAHE 有限对比适应性直方图均衡化 2D直方图OpenCV中的2D直方图Numpy中2D直方图 直…

基于frida检测demo来学习frida检测及anti

原文地址:https://www.zhuoyue360.com/crack/108.html 前言 随着逆向的攻防强度不断的提升,目前主流的移动安全厂商的加固服务基本上都已包含了常见Hook框架的反调试,我们最常见的hook工具如下: fridaxposed 为了更好的提升自己相关的经验,我们可以拿这类demo来进行原理的学…

腾讯云轻量应用服务器镜像应用模板清单大全

腾讯云轻量应用服务器支持多种应用模板镜像&#xff0c;Windows和Linux镜像模板都有&#xff0c;如&#xff1a;宝塔Linux面板腾讯云专享版、WordPress、WooCommerce、LAMP、Node.js、Docker CE、K3s、宝塔Windows面板和ASP.NET等应用模板镜像&#xff0c;腾讯云服务器网分享腾…

聊一下互联网开源变现

(点击即可收听) 互联网开源变现其实是指通过开源软件或者开放源代码的方式&#xff0c;实现收益或盈利。这种方式越来越被广泛应用于互联网行业 在互联网开源变现的模式中&#xff0c;最常见的方式是通过捐款、广告、付费支持或者授权等方式获利。 例如&#xff0c;有些开源软件…

Linux 基础(五)常用命令-文件属性

文件属性 文件权限文件属性修改文件权限属性 文件所有者 文件权限 文件属性 Linux中文件权限 可以通过文件属性体现&#xff1b; 使用 ll 查看文件列表 最前面的 l d 表示文件类型 1 5 表示硬链接数 或者 子文件夹个数 所属用户 所属用户组 文件大小 创建/更新时间 文件&…

首个女性向3A手游要来了?获IGN认可,《以闪亮之名》能否突出重围

最近的手游市场可以说是热度十足&#xff0c;各大厂商都发布了旗下新作的消息&#xff0c;3A高自由度似乎成了所有新游的主基调&#xff0c;但说起与众不同&#xff0c;那便不得不说这款《以闪亮之名》&#xff0c;这是本季度新游中唯一一个女性向3A作品。 这款手游主打超自由时…

2009年“五一”假期市民旅游指南

信息来源于&#xff1a;上海旅游官网 为使广大市民更好地领略上海的都市风情&#xff0c;满足 市民的旅游消费需求&#xff0c;丰富节日生活 &#xff0c;本市部分景点 、旅游企业 精心策划&#xff0c;积极准备&#xff0c;推出一系列适合市民市内旅游的节目&#xff0c;在…

常州嬉戏谷游玩全攻略

攻略导读&#xff1a;常州环球动漫嬉戏谷&#xff0c;一座国际动漫游戏体验博览园&#xff0c;颠覆传统&#xff0c;突破创新&#xff0c;定位鲜明&#xff0c;以满足逾4亿中国互联网用户的庞大娱乐需求为目标&#xff0c;以更适合未来前往的体验型公园为前瞻。假面文化的“梦幻…

第四十八周周报

学习目标&#xff1a; 修改ViTGAN 学习内容&#xff1a; 位置编码和多尺度 学习时间&#xff1a; 8.5-8。12 学习产出&#xff1a; 这两周主要工作在修改ViTGAN的结构和代码&#xff0c;将相对位置编码加入ViTGAN并将生成器变为多尺度&#xff0c;由于匹配维度很困难&am…

Jpa与Druid线程池及Spring Boot整合(二): spring-boot-starter-data-jpa 踏坑异常处理方案

Jpa与Druid线程池及Spring Boot整合(一) Jpa与Druid线程池及Spring Boot整合(二)&#xff1a;几个坑 附录官网文档&#xff1a;core.domain-events域事件 从聚合根发布事件 存储库管理的实体是聚合根。在领域驱动设计应用程序中&#xff0c;这些聚合根通常会发布领域事件。Sp…

pass 软件_PASS软件非劣效Logrank检验的h1参数如何设置?

前言 近日&#xff0c;有朋友在《统计咨询》公众号咨询&#xff1a;在使用PASS中的Non-Inferiority Logrank Tests程序计算样本量时&#xff0c;h1(Hazard Rate of Reference Group) 这个参数不懂得如何设置&#xff1f;见下图红色矩形标注的参数。相信这个也是其他很多朋友碰…

简单聊聊什么是Sass、Pass和Iass?

Iass&#xff0c;Pass和Saas都是什么意思&#xff1f;想必大家都听过也查阅过资料。但现在网上很多文章都会把一些比较简单的概念包装得非常牛气&#xff0c;逼格很高&#xff0c;各种高大上就是不说大白话&#xff0c;本文正好通过搭建网校平台为例和小伙伴简单分享一下它们之…

项目如何简单的使用pass平台部署服务

目录 前言&#xff1a; 一&#xff1a;Pass平台的优势 二&#xff1a;Pass平台的相关要素 三&#xff1a;docker|jenkins\k8s\pass\git之间关系 四&#xff1a;项目如何使用pass 五&#xff1a;pass平台常规操作 5.1应用重启 5.1.1定位到命名空间下的容器项目 5.1.2服务…

云计算之IasS、PasS、SaaS

越来越多的软件&#xff0c;开始采用云服务。 云服务只是一个统称&#xff0c;可以分成三大类。 IaaS&#xff1a;基础设施服务&#xff0c;Infrastructure-as-a-servicePaaS&#xff1a;平台服务&#xff0c;Platform-as-a-serviceSaaS&#xff1a;软件服务&#xff0c;Softw…

pass平台的搭建

Docker容器化部署Rancher CentOS 7.0默认使用的是firewall作为防火墙 查看防火墙状态 firewall-cmd --state ​ 停止firewall systemctl stop firewalld.service ​ 禁止firewall开机启动 systemctl disable firewalld.service ​ 安装rancher docker run -d --restart=unless-…

pass,saas区别

毫无疑问&#xff0c;PaaS与企业在服务与开发过程中的需求密切相关&#xff0c;特别是随着云计算的发展和企业平台化战略的驱动&#xff0c;企业对于云原生应用和全新的应用开发都提出了更高要求&#xff0c;而PaaS作为“承上启下”的中间层也变得越来越重要&#xff0c;更成为…