需求分析
当菜品数据很多时,用分页的形式来展示列表数据
代码开发
- 页面发送ajax请求,将分页查询参数提交到服务端,获取分页数据
- 页面发送请求,请求服务端进行图片下载,用于页面图片展示
构造分页
注意:
这里不能直接返回,否则分页查询显示的信息不是完整的。比如要展示菜品名称,返回的数据是没有菜品名称的。
后端返回的数据中,也需要有categoryName
这个数据
package com.springboot.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.reggie.common.R;
import com.springboot.reggie.dto.DishDto;
import com.springboot.reggie.entity.Category;
import com.springboot.reggie.entity.Dish;
import com.springboot.reggie.entity.DishFlavor;
import com.springboot.reggie.service.CategoryService;
import com.springboot.reggie.service.DishFlavorService;
import com.springboot.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 {@Autowiredprivate DishService dishService;//菜品服务@Autowiredprivate DishFlavorService dishFlavorService;//菜品口味@Autowiredprivate CategoryService categoryService;//菜品分类@PostMappingpublic R<String> save(@RequestBody DishDto dishDto)//记得封装要加RequestBody注解{log.info(dishDto.toString());dishService.saveWithFlavor(dishDto);return R.success("新增菜品成功...");}/*** 菜品信息分页查询* @param page* @param pageSize* @param name* @return*/@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);//将上面pageInfo对象中的属性拷贝到dishDtoPage中去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);}String categoryName = category.getName();dishDto.setCategoryName(categoryName);return dishDto;}).collect(Collectors.toList());// List<DishDto> list = null;dishDtoPage.setRecords(list);return R.success(pageInfo);}}