个人名片:
博主:酒徒?. 专栏:瑞吉外卖 个人简介:沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本项目基于B站黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis plus开发核心技术的真java实战项目。
视频链接【黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis plus开发核心技术的真java实战项目】 https://www.bilibili.com/video/BV13a411q753? 点击观看
一、页面显示
1.显示全部
分析:setmeal地址,GET方式,page地址,page,pageSize属性
package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
@Autowired
private ISetmealService setmealService;
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
Page<Setmeal> pageInfo = new Page<>(page, pageSize);
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
setmealService.page(pageInfo, queryWrapper);
return R.success(pageInfo);
}
}
在此查询页面,发现一点小问题,套餐分类没有内容。也就是categoryName。 修改后代码如下
package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
@Autowired
private ISetmealService setmealService;
@Autowired
private ICategoryService categoryService;
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
Page<Setmeal> setmealPage = new Page<>(page, pageSize);
Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
setmealService.page(setmealPage, queryWrapper);
BeanUtils.copyProperties(setmealPage, setmealDtoPage);
List<Setmeal> records = setmealPage.getRecords();
List<SetmealDto> dtoList = records.stream().map((item) -> {
LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Category::getId, item.getCategoryId());
Category category = categoryService.getOne(wrapper);
SetmealDto setmealDto = new SetmealDto();
setmealDto.setCategoryName(category.getName());
return setmealDto;
}).collect(Collectors.toList());
setmealDtoPage.setRecords(dtoList);
return R.success(setmealDtoPage);
}
}
2.输入框查询
相对于全部查询来说,多了一个name属性
if (name != null){
queryWrapper.like(Setmeal::getName, name);
}
完整代码如下:
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
Page<Setmeal> setmealPage = new Page<>(page, pageSize);
Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
if (name != null){
queryWrapper.like(Setmeal::getName, name);
}
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
setmealService.page(setmealPage, queryWrapper);
BeanUtils.copyProperties(setmealPage, setmealDtoPage);
List<Setmeal> records = setmealPage.getRecords();
List<SetmealDto> dtoList = records.stream().map((item) -> {
LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Category::getId, item.getCategoryId());
Category category = categoryService.getOne(wrapper);
SetmealDto setmealDto = new SetmealDto();
setmealDto.setCategoryName(category.getName());
BeanUtils.copyProperties(item, setmealDto);
return setmealDto;
}).collect(Collectors.toList());
setmealDtoPage.setRecords(dtoList);
return R.success(setmealDtoPage);
}
二、新建套餐
1.套餐菜品
分析:dish地址,GET方式,list地址,categoryId属性
@GetMapping("/list")
public R<List<Dish>> list(@PathParam("categoryId") Long categoryId){
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Dish::getCategoryId, categoryId)
.eq(Dish::getStatus, 1)
.orderByAsc(Dish::getSort)
.orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(queryWrapper);
return R.success(list);
}
2.保存
分析:setmeal地址,POST方式
@PostMapping
public R<String> setmeal(HttpServletRequest request, @RequestBody SetmealDto setmealDto){
setmealDto.setCreateTime(LocalDateTime.now());
setmealDto.setUpdateTime(LocalDateTime.now());
Long empId = (Long) request.getSession().getAttribute("employee");
setmealDto.setCreateUser(empId);
setmealDto.setUpdateUser(empId);
setmealService.saveWithSetmealDto(request,setmealDto);
return R.success("新增套餐成功");
}
在ISetmealService中创建saveWithSetmealDto方法
void saveWithSetmealDto(HttpServletRequest request, SetmealDto setmealDto);
在ISetmealServiceImpl中实现方法
@PostMapping
public R<String> setmeal(HttpServletRequest request, @RequestBody SetmealDto setmealDto){
setmealDto.setCreateTime(LocalDateTime.now());
setmealDto.setUpdateTime(LocalDateTime.now());
Long empId = (Long) request.getSession().getAttribute("employee");
setmealDto.setCreateUser(empId);
setmealDto.setUpdateUser(empId);
setmealService.saveWithSetmealDto(request,setmealDto);
return R.success("新增套餐成功");
}
三、修改
1.回显数据
分析:setmeal地址,GET方式,id地址 页面包括套餐的基本信息,和套餐菜品。也就是Setmeal和SetmealDish。即SetmealDto。
@GetMapping("/{id}")
public R<SetmealDto> list(@PathVariable Long id){
log.info("id:{}",id);
SetmealDto setmealDto = setmealService.getSetmealDto(id);
return R.success(setmealDto);
}
在ISetmealService中创建getSetmealDto方法
SetmealDto getSetmealDto(Long id);
在ISetmealServiceImpl中实现此方法
@Override
public SetmealDto getSetmealDto(Long id) {
Setmeal setmeal = setmealService.getById(id);
SetmealDto setmealDto = new SetmealDto();
BeanUtils.copyProperties(setmeal, setmealDto);
LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SetmealDish::getSetmealId, id);
List<SetmealDish> list = setmealDishService.list(queryWrapper);
setmealDto.setSetmealDishes(list);
return setmealDto;
}
2.保存
分析:setmeal地址,PUT方式 (注意:和前面修改菜品相同_)_查询数据库中的setmeal_dish表,发现表格为空。所以修改套餐菜品就是保存套餐菜品到setmeal_dish表中,但要注意先删除原有的套餐菜品.
@PutMapping
public R<String> setmeals(HttpServletRequest request, @RequestBody SetmealDto setmealDto){
log.info("setmealDto:{}",setmealDto);
setmealDto.setUpdateTime(LocalDateTime.now());
setmealService.updateWithSetmealDto(request, setmealDto);
return R.success("修改成功");
}
在ISetmealService中创建getSetmealDto方法
void updateWithSetmealDto(HttpServletRequest request, SetmealDto setmealDto);
在ISetmealServiceImpl中实现此方法
@Override
public void updateWithSetmealDto(HttpServletRequest request, SetmealDto setmealDto) {
setmealService.updateById(setmealDto);
LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
setmealDishService.remove(queryWrapper);
List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
setmealDishes = setmealDishes.stream().map((item) -> {
item.setSetmealId(setmealDto.getId());
item.setCreateTime(LocalDateTime.now());
item.setUpdateTime(LocalDateTime.now());
Long empId = (Long) request.getSession().getAttribute("employee");
item.setCreateUser(empId);
item.setUpdateUser(empId);
return item;
}).collect(Collectors.toList());
setmealDishService.saveBatch(setmealDishes);
}
四、停售与起售
1.单个
分析:setmeal地址,POST方式,status,{status}地址,ids属性
@PostMapping("/status/{status}")
public R<String> status(@PathVariable int status, @PathParam("ids") Long ids){
log.info("status,{},ids:{}",status,ids);
Setmeal setmeal = setmealService.getById(ids);
setmeal.setStatus(status);
setmealService.updateById(setmeal);
if (status == 0){
return R.success("停售成功");
}else if (status == 1){
return R.success("起售成功");
}else {
return R.success("状态码异常");
}
}
2.批量
相对于单个而言,需要查询多个ids。 与菜品管理中修改完全相同。(这里就不解释了)
@PostMapping("/status/{status}")
public R<String> status(@PathVariable int status, @RequestParam("ids") List<Long> ids){
log.info("status,{},ids:{}",status,ids);
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(Setmeal::getId, ids);
List<Setmeal> list = setmealService.list(queryWrapper);
for (Setmeal setmeal : list) {
setmeal.setStatus(status);
setmealService.updateById(setmeal);
}
if (status == 0){
return R.success("停售成功");
}else if (status == 1){
return R.success("起售成功");
}else {
return R.error("状态码异常");
}
}
五、删除
1.单个
分析:setmeal地址,DELETE方式,ids属性
@DeleteMapping
public R<String> setmeal(@PathParam("ids") Long ids){
setmealService.removeById(ids);
return R.success("删除成功");
}
2.批量
相对于单个而言,需要查询多个ids。这里就不多说了。
@DeleteMapping
public R<String> setmeal(@RequestParam("ids") List<Long> ids){
setmealService.removeByIds(ids);
return R.success("删除成功");
}
3.菜品删除
检查数据库,发现套餐菜品没有删除。
@DeleteMapping
public R<String> setmeal(@RequestParam("ids") List<Long> ids){
LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SetmealDish::getSetmealId, ids);
setmealDishService.remove(queryWrapper);
setmealService.removeByIds(ids);
return R.success("删除成功");
}
4.状态判断
老是粗心,刚刚删除时看到在起售状态竟然也成功删除了。做出修改。
@DeleteMapping
public R<String> setmeal(@RequestParam("ids") List<Long> ids){
LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
wrapper.in(Setmeal::getId, ids);
List<Setmeal> list = setmealService.list();
boolean flag = true;
for (Setmeal setmeal : list) {
if (setmeal.getStatus() == 1){
flag = false;
}
}
if (flag){
LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SetmealDish::getSetmealId, ids);
setmealDishService.remove(queryWrapper);
setmealService.removeByIds(ids);
return R.success("删除成功");
}else {
return R.error("存在正在售卖套餐,删除失败");
}
}
|