【java】【项目实战】[外卖五]菜品管理业务开发

目录

一、文件上传与下载

1.1 文件上传介绍

1.2 文件下载介绍

1.3 文件上传代码实现

1.3.1 新增upload.html

1.3.2 修改application.yml

 1.3.3 CommonController

1.3.4  功能测试

1.4 文件下载代码实现

 1.4.1  CommonController

1.4.2  功能测试

二、新增菜品

2.1 需求分析

2.2 数据模型

2.3 代码实现

2.3.1 菜品口味实体类DishFlavor 

2.3.2 DishFlavorMapper

2.3.3 DishFlavorService

2.3.4 DishFlavorServiceImpl

2.3.5 DishController 

2.3.6 CategoryController 新增方法

2.3.7 DishDto 

 2.3.8 DishService 

2.3.9  DishServiceImpl 

2.3.10 ReggieApplication 

 2.3.11 DishController 

2.4 功能测试

三、菜品信息分页查询

3.1 需求分析

3.2 代码分析

 3.2.1 DishController

3.3 功能测试

四、修改菜品

4.1 需求分析

4.2 代码分析

 4.2.1 DishService 

 4.2.2 DishServiceImpl

 4.2.3 DishController

4.3 功能测试


前言:菜品管理业务实现,涉及文件上传与下载、批量操作等

【java】【项目实战】[外卖五]菜品管理业务开发_第1张图片

一、文件上传与下载

1.1 文件上传介绍

【java】【项目实战】[外卖五]菜品管理业务开发_第2张图片

 【java】【项目实战】[外卖五]菜品管理业务开发_第3张图片

【java】【项目实战】[外卖五]菜品管理业务开发_第4张图片 

 

1.2 文件下载介绍

【java】【项目实战】[外卖五]菜品管理业务开发_第5张图片

 

1.3 文件上传代码实现

【java】【项目实战】[外卖五]菜品管理业务开发_第6张图片

1.3.1 新增upload.html

【java】【项目实战】[外卖五]菜品管理业务开发_第7张图片

 




  
  
  
  文件上传
  
  
  
  


   

1.3.2 修改application.yml

server:
  port: 8080
spring:
  application:
    # 应用名称 可选
    name: reggie_take_out_spuer
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: root
      password: runa#2050
mybatis-plus:
  configuration:
    #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: ASSIGN_ID
# 文件上传目录
reggie:
  path: D:\images\

 1.3.3 CommonController

【java】【项目实战】[外卖五]菜品管理业务开发_第8张图片

 

package com.runa.reggie.controller;

import com.runa.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传和下载
 */
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {

    // 获取yml路径
    @Value("${reggie.path}")
    private String basePath;


    /**
     * 文件下载
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        // file 是一个临时文件,需要转存到指定的位置,否则本次请求完成后临时文件会删除
        log.info(file.toString());

        //原始文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 使用uuid 重新生成文件,防止文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;

        // 创建一个目录对象
        File dir = new File(basePath);
        // 判断当前目录是否存在
        if(!dir.exists()){
            // 不存在,需要创建
            dir.mkdirs();
        }

        try {
            // 将临时文件转存到指定位置
            file.transferTo(new File(basePath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.success(fileName);
    }
}

1.3.4  功能测试

http://localhost:8080/backend/page/demo/upload.html 

1.4 文件下载代码实现

【java】【项目实战】[外卖五]菜品管理业务开发_第9张图片

 1.4.1  CommonController

package com.runa.reggie.controller;

import com.runa.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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传和下载
 */
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {

    // 获取yml路径
    @Value("${reggie.path}")
    private String basePath;


    /**
     * 文件下载
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        // file 是一个临时文件,需要转存到指定的位置,否则本次请求完成后临时文件会删除
        log.info(file.toString());

        //原始文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 使用uuid 重新生成文件,防止文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;

        // 创建一个目录对象
        File dir = new File(basePath);
        // 判断当前目录是否存在
        if(!dir.exists()){
            // 不存在,需要创建
            dir.mkdirs();
        }

        try {
            // 将临时文件转存到指定位置
            file.transferTo(new File(basePath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.success(fileName);
    }

    /**
     * 文件下载
     * @param name
     * @param response
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response){
        try {
            //输入流,通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));

            //输出流,通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }

            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

1.4.2  功能测试

http://localhost:8080/backend/page/demo/upload.html

 【java】【项目实战】[外卖五]菜品管理业务开发_第10张图片

 

二、新增菜品

2.1 需求分析

【java】【项目实战】[外卖五]菜品管理业务开发_第11张图片

 

2.2 数据模型

【java】【项目实战】[外卖五]菜品管理业务开发_第12张图片

【java】【项目实战】[外卖五]菜品管理业务开发_第13张图片 

 【java】【项目实战】[外卖五]菜品管理业务开发_第14张图片

 

2.3 代码实现

【java】【项目实战】[外卖五]菜品管理业务开发_第15张图片

 【java】【项目实战】[外卖五]菜品管理业务开发_第16张图片

 

2.3.1 菜品口味实体类DishFlavor 

package com.runa.reggie.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
菜品口味
 */
@Data
public class DishFlavor implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;


    //菜品id
    private Long dishId;


    //口味名称
    private String name;


    //口味数据list
    private String value;


    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;


    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;


    @TableField(fill = FieldFill.INSERT)
    private Long createUser;


    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;


    //是否删除
    private Integer isDeleted;

}

2.3.2 DishFlavorMapper

package com.runa.reggie.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.runa.reggie.entity.DishFlavor;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DishFlavorMapper extends BaseMapper {
}

2.3.3 DishFlavorService

package com.runa.reggie.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.runa.reggie.entity.DishFlavor;

public interface DishFlavorService extends IService {
}

2.3.4 DishFlavorServiceImpl

package com.runa.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runa.reggie.entity.DishFlavor;
import com.runa.reggie.mapper.DishFlavorMapper;
import com.runa.reggie.service.DishFlavorService;
import org.springframework.stereotype.Service;

@Service
public class DishFlavorServiceImpl extends ServiceImpl implements DishFlavorService {
}

2.3.5 DishController 

package com.runa.reggie.controller;

import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;
}

2.3.6 CategoryController 新增方法

package com.runa.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.entity.Category;
import com.runa.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category){
        log.info("新增分类:{}", category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R page(int page, int pageSize){
        log.info("page= {}, pageSize= {}, name = {}",page, pageSize);
        //分页构造器
        Page pageInfo = new Page(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件,根据sort排序
        queryWrapper.orderByAsc(Category::getSort);
        //进行分页查询
        categoryService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 根据id删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(Long ids){
        log.info("根据ID删除分类:{}",ids);
//        categoryService.removeById(ids);
        categoryService.remove(ids);
        return R.success("分类信息删除成功");
    }


    @PutMapping
    public R update(@RequestBody Category category){
        log.info("根据id更新分类信息:{}",category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R> list(Category category){
        log.info("查询的分类数据是:{}",category);
        // 条件构造器
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        // 添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}

【java】【项目实战】[外卖五]菜品管理业务开发_第17张图片

2.3.7 DishDto 

【java】【项目实战】[外卖五]菜品管理业务开发_第18张图片 

 【java】【项目实战】[外卖五]菜品管理业务开发_第19张图片

 

package com.runa.reggie.dto;

import com.runa.reggie.entity.Dish;
import com.runa.reggie.entity.DishFlavor;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;

@Data
public class DishDto extends Dish {

    private List flavors = new ArrayList<>();

    private String categoryName;

    private Integer copies;
}

 2.3.8 DishService 

package com.runa.reggie.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Dish;


public interface DishService extends IService {
    //新增菜品,通杀插入菜品对于的口味
    public void saveWithFlavor(DishDto dishDto);
}

2.3.9  DishServiceImpl 

package com.runa.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Dish;
import com.runa.reggie.entity.DishFlavor;
import com.runa.reggie.mapper.DishMapper;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品,通杀保存对于的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();

        // 菜品口味
        List flavorList =dishDto.getFlavors();
        flavorList = flavorList.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        // 保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavorList);

    }
}

2.3.10 ReggieApplication 

package com.runa.reggie;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Slf4j
@SpringBootApplication
@ServletComponentScan
@EnableTransactionManagement
public class ReggieApplication {
    public static void main(String[] args) {
        SpringApplication.run(ReggieApplication.class,args);
        log.info("项目启动成功~~~");
    }
}

 2.3.11 DishController 

package com.runa.reggie.controller;

import com.runa.reggie.common.R;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
      return R.success("新增菜品成功");
    }
}

2.4 功能测试

【java】【项目实战】[外卖五]菜品管理业务开发_第20张图片

 

三、菜品信息分页查询

3.1 需求分析

【java】【项目实战】[外卖五]菜品管理业务开发_第21张图片

 

3.2 代码分析

【java】【项目实战】[外卖五]菜品管理业务开发_第22张图片

 3.2.1 DishController

package com.runa.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Dish;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
      return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    public R page(int page, int pageSize, String name){
        // 构造分页构造器对象
        Page pageInfo = new Page<>(page, pageSize);
        Page dishDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        // 添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        //执行分页查询
        dishService.page(pageInfo,queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(pageInfo, dishDtoPage,"records");
        List records = pageInfo.getRecords();
        List list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();// 分类id
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());


        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }
}

 

3.3 功能测试

【java】【项目实战】[外卖五]菜品管理业务开发_第23张图片

 

四、修改菜品

4.1 需求分析

【java】【项目实战】[外卖五]菜品管理业务开发_第24张图片

 

4.2 代码分析

【java】【项目实战】[外卖五]菜品管理业务开发_第25张图片

 4.2.1 DishService 

package com.runa.reggie.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Dish;


public interface DishService extends IService {
    //新增菜品,同时插入菜品对于的口味
    public void saveWithFlavor(DishDto dishDto);


    //根据id查询对于菜品信息和口味信息
    public DishDto getByIdWithFlavor(Long id);

    //更新菜品信息
    public void updateWithFlavor(DishDto dishDto);
}

 4.2.2 DishServiceImpl

package com.runa.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Dish;
import com.runa.reggie.entity.DishFlavor;
import com.runa.reggie.mapper.DishMapper;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品,通杀保存对于的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();

        // 菜品口味
        List flavors =dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        // 保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        // 查询菜品基本信息,从dish查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        // 查询当前菜品对应的口味信息,从dish_flavor
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 更新菜品信息,同时更新口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        // 更新菜品表dish信息
        this.updateById(dishDto);

        // 清理菜品对应的口味数据 dish_flavor表的delete操作
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        // 添加当前提交过的口味数据 dish_flavor表的insert操作
        List flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }
}

 4.2.3 DishController

package com.runa.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Dish;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
      return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    public R page(int page, int pageSize, String name){
        // 构造分页构造器对象
        Page pageInfo = new Page<>(page, pageSize);
        Page dishDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        // 添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        //执行分页查询
        dishService.page(pageInfo,queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(pageInfo, dishDtoPage,"records");
        List records = pageInfo.getRecords();
        List list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();// 分类id
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());


        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }


    /**
     * 根据ID 查询菜品和对应口味信息 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        log.info("要查询显示的菜品信息是:{}",id);
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }
}

 

4.3 功能测试

【java】【项目实战】[外卖五]菜品管理业务开发_第26张图片

 

 

你可能感兴趣的:(java,java,开发语言)