新增菜品
1.实体类DishFlavor
package com.itheima.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; }
Mapper接口DishFlavorMapper
package com.itheima.reggie.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.itheima.reggie.entity.DishFlavor; import org.apache.ibatis.annotations.Mapper; @Mapper public interface DishFlavorMapper extends BaseMapper<DishFlavor> { }
业务层接口DishFlavorService
package com.itheima.reggie.service; import com.baomidou.mybatisplus.extension.service.IService; import com.itheima.reggie.entity.DishFlavor; public interface DishFlavorService extends IService<DishFlavor> { }
业务层实现类DishFlavorServicelmpl
package com.itheima.reggie.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.itheima.reggie.entity.DishFlavor; import com.itheima.reggie.mapper.DishFlavorMapper; import org.springframework.stereotype.Service; @Service public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorMapper, DishFlavor> { }
控制层DishController
//菜品管理 @RestController @RequestMapping("/dish") public class DishController { @Autowired private DishService dishService; @Autowired private DishFlavorService dishFlavorService; }
在CategoryController中编写根据条件查询分类
//根据条件查询分类数据 @GetMapping("/list") public R<List<Category>> list(Category category){ //条件构造器 LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>(); //添加条件 queryWrapper.eq(category.getType()!=null,Category::getType,category.getType()); //添加排序条件 queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime); List<Category> list=categoryService.list(queryWrapper); return R.success(list); }
编写DishDto(DTO,全称为Data Transfer Object,即数据传输对象,一般用于展示层与服务层之间的数据传输。),用于封装页面提交的数据
package com.itheima.reggie.dto; import com.itheima.reggie.entity.Dish; import com.itheima.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; }
新建DishService类
package com.itheima.reggie.service; import com.baomidou.mybatisplus.extension.service.IService; import com.itheima.reggie.dto.DishDto; import com.itheima.reggie.entity.Dish; public interface DishService extends IService<Dish> { //新增菜品,同时插入菜品对应口味数据 需要操作两张表:dish,dish_flavor public void saveWithFlavor(DishDto dishDto);
新建dsihServiceimpl类
package com.itheima.reggie.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 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.DishFlavorService; import com.itheima.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; @Service @Slf4j public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService { @Autowired private DishFlavorService dishFlavorService; //新增菜品 同时保存对应口味数据 @Override @Transactional public void saveWithFlavor(DishDto dishDto) { //保存菜品的基本信息到菜品表dish this.save(dishDto); Long dishId=dishDto.getId();//菜品id List<DishFlavor> flavors=dishDto.getFlavors(); flavors= flavors.stream().map((item)->{ item.setDishId(dishId); return item; }).collect(Collectors.toList()); //保存菜品口味数据到菜品口味表dish_flavor dishFlavorService.saveBatch(flavors); } }
在DishController类编写代码
//新增菜品 @PostMapping public R<String> save(@RequestBody DishDto dishDto){ log.info(dishDto.toString()); dishService.saveWithFlavor(dishDto); return R.success("新增菜品成功!"); }
菜品信息分页查询
菜品分页查询时前端页面和服务端的交互过程:
1、页面(backend/page/food/list.html)发送ajax请求,将分页查询参数(page、pageSize.name)提交到服务端,获取分页数据
2、页面发送请求,请求服务端进行图片下载,用于页面图片展示
开发菜品信息分页查询功能,其实就是在服务端编写代码去处理前端页面发送的这2次请求即可。
在DishController编写分页查询代码
//菜品信息分页 @GetMapping("/page") public R<Page> page(int page,int pageSize,String name){ //构造分页构造器 Page<Dish> pageInfo=new Page<>(page,pageSize); Page<DishDto> dishDtoPage=new Page<>(); //条件构造器 LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>(); //添加过滤条件 queryWrapper.like(name!=null,Dish::getName,name); //排序条件 queryWrapper.orderByDesc(Dish::getUpdateTime); //执行分页查询 dishService.page(pageInfo,queryWrapper); //对象拷贝 BeanUtils.copyProperties(pageInfo,dishDtoPage,"records"); List<Dish> records =pageInfo.getRecords(); List<DishDto> list=records.stream().map(item->{ DishDto dishDto=new DishDto(); BeanUtils.copyProperties(item,dishDto); Long categoryId= item.getCategoryId();//分类id //根据id查询分类对象 Category category = categoryService.getById(categoryId); if (category!=null){ String categoryName = category.getName(); dishDto.setCategoryName(categoryName); } return dishDto; }).collect(Collectors.toList()); dishDtoPage.setRecords(list); return R.success(dishDtoPage); }
修改菜品信息
在开发代码之前,需要梳理一下修改菜品时前端页面(add.html)和服务端的交互过程:
1、页面发送ajax请求,请求服务端获取分类数据,用于菜品分类下拉框中数据展示
2、页面发送ajax请求,请求服务端,根据id查询当前菜品信息,用于菜品信息回显3、页面发送请求,请求服务端进行图片下载,用于页图片回显
4、点击保存按钮,页面发送ajax请求,将修改后的菜品相关数据以json形式提交到服务端
开发修改菜品功能,其实就是在服务端编写代码去处理前端页面发送的这4次请求即可。
在DishService中编写新增和修改代码
//根据id查询菜品信息和对应的口味信息 public DishDto getByIdWithFlavor(Long id); //修改菜品信息,同时更新口味信息 public void updateWithFlavor(DishDto dishDto); }
在DishController中编写
//根据id查询菜品信息和对应的口味信息 @GetMapping("/{id}") public R<DishDto> get(@PathVariable Long id){ DishDto dishDto = dishService.getByIdWithFlavor(id); return R.success(dishDto); } //修改菜品 @PutMapping public R<String> update(@RequestBody DishDto dishDto){ log.info(dishDto.toString()); dishService.updateWithFlavor(dishDto); return R.success("修改菜品成功!"); }
在DishServiceImpl中编写
@Override //根据id查询菜品信息和对应的口味信息 public DishDto getByIdWithFlavor(Long id) { //查询菜品基本信息 Dish dish = this.getById(id); DishDto dishDto=new DishDto(); BeanUtils.copyProperties(dish,dishDto); //查询菜品对应的口味信息 //条件构造器 LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper(); queryWrapper.eq(DishFlavor::getDishId,dish.getId()); List<DishFlavor> flavors = dishFlavorService.list(queryWrapper); dishDto.setFlavors(flavors); return dishDto; } @Override @Transactional public void updateWithFlavor(DishDto dishDto) { //更新dish表基本信息 this.updateById(dishDto); //清理当前菜品对应的口味数据==dish——flavor表的delete操作 LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper(); queryWrapper.eq(DishFlavor::getDishId,dishDto.getId()); dishFlavorService.remove(queryWrapper); //添加当前提交的口味数据==dish——falvor表的insert操作 List<DishFlavor> flavors = dishDto.getFlavors(); flavors= flavors.stream().map((item)->{ item.setDishId(dishDto.getId()); return item; }).collect(Collectors.toList()); dishFlavorService.saveBatch(flavors); } }