day05
作者:互联网
新增套餐:
@GetMapping("/list") public R<List<Dish>> list(Dish dish){ //构造查询条件 LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId()); //添加条件,查询状态为1(1为起售,0为停售)的菜品 queryWrapper.eq(Dish::getStatus,1); List<Dish> list = dishService.list(queryWrapper); //添加排序条件 return R.success(list); }
这步回显菜品代码倒是没啥好说的 但是一定要注意访问时候的数据是什么 决定他在哪隔层 用什么方法
添加套餐 就是注意那里没有添加哪个
@Autowired private SetmealDishService setmealDishService; @Override public void saveWithDish(SetmealDto setmealDto) { super.save(setmealDto); List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes(); List<SetmealDish> collect = setmealDishes.stream().map((item) -> { Long id = setmealDto.getId(); item.setSetmealId(id); return item; }).collect(Collectors.toList()); setmealDishService.saveBatch(collect); }
套餐信息分页查询
} @GetMapping("/page") public R<Page> list(int page, int pageSize, String name){ Page<Setmeal> pageInfo=new Page<>(page,pageSize); Page<SetmealDto> dtoPage = new Page<>(); //构造查询条件对象 LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.like(name != null, Setmeal::getName, name); //操作数据库 setmealService.page(pageInfo,queryWrapper); BeanUtils.copyProperties(pageInfo,dtoPage,"record"); List<SetmealDto> collect = pageInfo.getRecords().stream().map((item) -> { SetmealDto setmealDto = new SetmealDto(); BeanUtils.copyProperties(item, setmealDto); Long categoryId = item.getCategoryId(); Category byId = categoryService.getById(categoryId); String name1 = byId.getName(); setmealDto.setCategoryName(name1); return setmealDto; }).collect(Collectors.toList()); dtoPage.setRecords(collect); return R.success(dtoPage); }
第一 查询用like、
第二:如果再插入属性找不到插入地方就new一个
删除套餐:
public void removeWithDish(List<Long> ids) { LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>(); queryWrapper.in(Setmeal::getId,ids); queryWrapper.eq(Setmeal::getStatus,1); int count = super.count(queryWrapper); if(count>0){ throw new CustomException("套餐正在售卖中,不能删除"); } super.removeByIds(ids); LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>(); queryWrapper1.in(SetmealDish::getSetmealId,ids); setmealDishService.remove(queryWrapper1); }
短信发送:
标签:queryWrapper,LambdaQueryWrapper,list,setmealDto,item,day05,new 来源: https://www.cnblogs.com/CYueblog/p/16252877.html