文章目录
- 需求分析
- 数据模型
- dish表
- dis flaow表
- 代码开发
- 搭建框架
- 新增dishFlavor实体类
- 新增Mapper接口DishFlavorMapper
- 新增业务接口DishFlavorService
- 新增业务层实现类DishFlavorServiceImpl
- 新增控制层DishController
- 梳理交互过程
- 编辑CategoryControoler
- 下拉框功能测试
- 导入DTO
- 编写DishController
- 添加菜品测试
- 编写DishService接口
- 编写DishServiceImpl
- 编写ReggieApplication
- 完善DishController
- 功能测试
需求分析
新增完后在手机端显示
数据模型
dish表
dis flaow表
代码开发
搭建框架
新增dishFlavor实体类
package com.taotao.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;//菜品idprivate Long dishId;//口味名称private String name;//口味数据listprivate 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;}
新增Mapper接口DishFlavorMapper
package com.taotao.reggie.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.reggie.entity.DishFlavor;
import org.apache.ibatis.annotations.Mapper;/*** create by 刘鸿涛* 2022/9/23 19:32*/
@SuppressWarnings({"all"})
@Mapper
public interface DishFlavorMapper extends BaseMapper<DishFlavor> {}
新增业务接口DishFlavorService
package com.taotao.reggie.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.reggie.entity.DishFlavor;/*** create by 刘鸿涛* 2022/9/23 19:33*/
@SuppressWarnings({"all"})
public interface DishFlavorService extends IService<DishFlavor> {}
新增业务层实现类DishFlavorServiceImpl
package com.taotao.reggie.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.entity.DishFlavor;
import com.taotao.reggie.mapper.DishFlavorMapper;
import com.taotao.reggie.service.DishFlavorService;
import org.springframework.stereotype.Service;/*** create by 刘鸿涛* 2022/9/23 19:35*/
@SuppressWarnings({"all"})
@Service
public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorMapper, DishFlavor> implements DishFlavorService {}
新增控制层DishController
package com.taotao.reggie.controller;import com.taotao.reggie.service.DishFlavorService;
import com.taotao.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** create by 刘鸿涛* 2022/9/23 19:37*/
@SuppressWarnings({"all"})
/*** 菜品管理*/
@RestController
@RequestMapping("/dish")
public class DishController {@Autowiredprivate DishService dishService;@Autowiredprivate DishFlavorService dishFlavorService;}
梳理交互过程
编辑CategoryControoler
/*** 根据条件查询分类数据* @param category* @return*/@GetMapping("list")public R<List<Category>> list(Category category){//条件构造器LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();//添加条件queryWrapper.eq(category.getType() != null,Category::getType,category.getType());//添加排序条件 根据Category的sort进行排序 根据category的更新时间进行排序queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);List<Category> list = categoryService.list(queryWrapper);return R.success(list);}
下拉框功能测试
导入DTO
package com.taotao.reggie.dto;import com.taotao.reggie.entity.Dish;
import com.taotao.reggie.entity.DishFlavor;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;@Data
public class DishDto extends Dish {private List<DishFlavor> flavors = new ArrayList<>();private String categoryName;private Integer copies;
}
编写DishController
package com.taotao.reggie.controller;import com.taotao.reggie.common.R;
import com.taotao.reggie.dto.DishDto;
import com.taotao.reggie.service.DishFlavorService;
import com.taotao.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;/*** create by 刘鸿涛* 2022/9/23 19:37*/
@SuppressWarnings({"all"})
/*** 菜品管理*/
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {@Autowiredprivate DishService dishService;@Autowiredprivate DishFlavorService dishFlavorService;@PostMappingpublic R<String> save(@RequestBody DishDto dishDto){log.info(dishDto.toString());return null;}
}
添加菜品测试
编写DishService接口
编写DishServiceImpl
package com.taotao.reggie.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.dto.DishDto;
import com.taotao.reggie.entity.Dish;
import com.taotao.reggie.entity.DishFlavor;
import com.taotao.reggie.mapper.DishMapper;
import com.taotao.reggie.service.DishFlavorService;
import com.taotao.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
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;/*** create by 刘鸿涛* 2022/6/8 8:21*/
@SuppressWarnings({"all"})
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {@Autowiredprivate DishFlavorService dishFlavorService;/*** 新增菜品,同时保存对应的口味数据* @param dishDto*/@Transactional@Overridepublic void saveWithFlavor(DishDto dishDto) {//保存菜品的基本信息到菜品表this.save(dishDto);Long dishId = dishDto.getId(); //菜品idList<DishFlavor> flavors = dishDto.getFlavors();flavors.stream().map((item) ->{item.setDishId(dishId);return item;}).collect(Collectors.toList());//保存菜品口味数据到菜品口味表dish_flavordishFlavorService.saveBatch(dishDto.getFlavors());}
}
编写ReggieApplication
完善DishController
功能测试
添加成功