Dto对象

Dto全程Data Transfer Object是用来传输数据用的。

比如具有菜单表、口味表,但是前端在添加新菜时,不能只添加一个菜还应该带上口味。因此这时候就需要Dto对象将菜单和口味组合起来进行传输。

@Data
public class DishDto extends Dish {

    private List<DishFlavor> flavors = new ArrayList<>();

    private String categoryName;

    private Integer copies;
}

事务机制

当我们插入菜品时,应该也插入口味数据。任何一个出错了,另外一个的数据都不应该被插入进去。因此这就需要事务机制进行处理了。

@PostMapping
public R<String> saveDish(@RequestBody DishDto dishDto) {
    dishService.saveDishWithFlavor(dishDto);
    return R.success("添加成功");
}
@Transactional
@Override
public void saveDishWithFlavor(DishDto dishDto) {
    // 保存菜品
    this.save(dishDto);

    // 保存口味
    Long dishId = dishDto.getId();
    List<DishFlavor> flavors = dishDto.getFlavors().stream().map((item) -> {
        item.setDishId(dishId);
        return item;
    }).collect(Collectors.toList());
    dishFlavorService.saveBatch(flavors);
}

查询许多dishDto

@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name) {
    // 找到dishPage
    Page<Dish> pageInfo = new Page<>(page, pageSize);
    LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
    dishLambdaQueryWrapper.like(name != null,Dish::getName, name);
    dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
    Page<Dish> ipage = dishService.page(pageInfo, dishLambdaQueryWrapper);

    // 转移到dishDto配置上来
    Page<DishDto> dishPage = new Page<>(page, pageSize);
    BeanUtils.copyProperties(pageInfo, dishPage, "records");
    List<DishDto> dishDtos = ipage.getRecords().stream().map((item) -> {
        DishDto dishDto = new DishDto();
        // 拷贝其他属性
        BeanUtils.copyProperties(item, dishDto);
        // 设置菜品类别
        Long categoryId = item.getCategoryId();
        Category category = categoryService.getById(categoryId);
        dishDto.setCategoryName(category.getName());
        return dishDto;
    }).toList();
    dishPage.setRecords(dishDtos);
    return R.success(dishPage);
}

查询一个菜品

@GetMapping("/{id}")
public R<DishDto> findById(@PathVariable Long id) {
    // 查询dish
    Dish dish = dishService.getById(id);

    // 查询口味
    LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
    List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);

    // 查询类别
    LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
    categoryLambdaQueryWrapper.eq(Category::getId, dish.getCategoryId());
    Category category = categoryService.getOne(categoryLambdaQueryWrapper);

    // 进行组合
    DishDto dishDto = new DishDto();
    BeanUtils.copyProperties(dish, dishDto);
    dishDto.setCategoryName(category.getName());
    dishDto.setFlavors(list);
    return R.success(dishDto);
}

更新一个菜品

@Transactional
@Override
public void updateDishWithFlavor(DishDto dishDto) {
    // 更新dish
    this.updateById(dishDto);
    // 更新口味,清理口味
    LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
    dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
    dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
    // 新增口味
    List<DishFlavor> flavors = dishDto.getFlavors();
    flavors.stream().map((item)->{
        item.setDishId(dishDto.getId());
        return item;
    }).collect(Collectors.toList());
    dishFlavorService.saveBatch(flavors);
}