# springboot-delivery-project **Repository Path**: li168tf/springboot-delivery-project ## Basic Information - **Project Name**: springboot-delivery-project - **Description**: springboot初学的外卖项目,有两个版本,正在更新中。。。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-08-31 - **Last Updated**: 2023-09-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: mybatatis-plus, MySQL, SpringBoot, Vue, Element-UI ## README Mysql 8.26 端口:8090 访问后台路径: local:8090/backend/page/login/login.html 访问前台: local:8090/front/page/login.html # 员工管理 ## *1、登录 > 请求网址: http://localhost:8090/employee/login > > 请求方法: POST > > 请求体: {"username":"admin","password":"123456"} ```java @PostMapping("/login") public R login(HttpServletRequest request,@RequestBody Employee employee){ //1、将页面提交的密码password进行md5加密处理 String password = employee.getPassword(); password = DigestUtils.md5DigestAsHex(password.getBytes()); //2、根据页面提交的用户名username查询数据库 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Employee::getUsername,employee.getUsername()); Employee emp = employeeService.getOne(queryWrapper); //3、如果没有查询到则返回登录失败结果 if(emp == null){ return R.error("用户名不存在"); } //4、密码比对,如果不一致则返回登录失败结果 if(!emp.getPassword().equals(password)){ return R.error("密码错误"); } //5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果 if(emp.getStatus() == 0){ return R.error("该账号已禁用"); } //6、登录成功,将员工id存入Session并返回登录成功结果 request.getSession().setAttribute("employee",emp.getId()); return R.success(emp); } ``` ## 2、退出 > 请求网址: http://localhost:8090/employee/logout > > 请求方法: POST ```java @PostMapping("/logout") public R logout(HttpServletRequest request){ //清理Session中保存的当前登录员工的id request.getSession().removeAttribute("employee"); return R.success("退出成功"); } ``` ## 3、新增 > 请求网址: http://localhost:8090/employee > > 请求方法: POST > > 请求体:{ > > ​ "name":"超","phone":"18813370725","sex":"1", > > ​ "idNumber":"440982200104214096","username":"lichao" > > } ```java @PostMapping public R save(HttpServletRequest request,@RequestBody Employee employee){ log.info("新增员工,员工信息:{}",employee.toString()); //设置初始密码123456,需要进行md5加密处理 employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes())); //属于公共字段 /* employee.setCreateTime(LocalDateTime.now()); employee.setUpdateTime(LocalDateTime.now()); //获得当前登录用户的id Long empId = (Long) request.getSession().getAttribute("employee"); employee.setCreateUser(empId); employee.setUpdateUser(empId);*/ employeeService.save(employee); return R.success("新增员工成功"); } ``` ## 4、列表 > 请求网址: http://localhost:8090/employee/page?page=1&pageSize=3 > > 请求方法: GET ```java @GetMapping("/page") public R page(int page, int pageSize, String name){ log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name); //构造分页构造器 Page pageInfo = new Page(page,pageSize); //构造条件构造器 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); //添加过滤条件 queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name); //添加排序条件 (更新时间进行降序,新编辑的在最前面) queryWrapper.orderByDesc(Employee::getUpdateTime); //执行查询 employeeService.page(pageInfo,queryWrapper); return R.success(pageInfo); } ``` ## 5、修改 ### 5.1 查询(数据回显) > 请求网址:http://localhost:8090/employee/1696150239677874178 > > 请求方法: GET ```java @GetMapping("/{id}") public R getById(@PathVariable Long id){ log.info("根据id查询员工信息"); Employee employee = employeeService.getById(id); if(employee != null){ return R.success(employee); } return R.error("没有查询到对应的员工信息"); } ``` ### 5.2 更新 > 请求网址: http://localhost:8090/employee > > 请求方法: PUT > > 请求体:(修改后的数据) > > { > > "id":"1696150239677874178", > > "username":"lichaoren","name":"李超人", > > "password":"e10adc3949ba59abbe56e057f20f883e","phone":"18813370725", > > "sex":"0","idNumber":"440982200104214096","status":1, > > "createTime":"2023-08-28 21:18:25","updateTime":"2023-08-28 21:18:25", > > "createUser":"1","updateUser":"1" > > } ```java @PutMapping public R update(HttpServletRequest request,@RequestBody Employee employee){ log.info(employee.toString()); //属于公共字段 /* Long empId = (Long)request.getSession().getAttribute("employee"); employee.setUpdateTime(LocalDateTime.now()); employee.setUpdateUser(empId);*/ employeeService.updateById(employee); return R.success("员工信息修改成功"); } ``` ## 6、禁用启用 > 请求网址: http://localhost:8090/employee > > 请求方法: PUT > > 请求体:{"id":"1696150239677874178","status":0} ``` 复用修改中的更新 ``` # 分类管理 ## 1、新增 > 请求网址: http://localhost:8090/category > > 请求方法: POST > > 请求体: {"name":"1","type":"1","sort":"1"} (1是菜品,2是套餐) ```java @PostMapping public R save(@RequestBody Category category){ log.info("category:{}",category); categoryService.save(category); return R.success("新增分类成功"); } ``` ## 2、列表 > 请求网址: http://localhost:8090/category/page?page=1&pageSize=10 > > 请求方法: GET ```java @GetMapping("/page") public R page(int page,int pageSize){ //分页构造器 Page pageInfo = new Page<>(page,pageSize); //条件构造器 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); //添加排序条件,根据sort进行排序 queryWrapper.orderByAsc(Category::getSort); //分页查询 categoryService.page(pageInfo,queryWrapper); return R.success(pageInfo); } ``` ## 3、修改 > 请求网址: http://localhost:8090/category > > 请求方法: PUT > > 请求体: > > { > > ​ "id":"1397844263642378242", > > ​ "name":"湘菜hehe", > > ​ "sort":"5" > > } ```java @PutMapping public R update(@RequestBody Category category){ log.info("修改分类信息:{}",category); categoryService.updateById(category); return R.success("修改分类信息成功"); } ``` ## *4、删除 > 请求网址: http://localhost:8090/category?ids=1696174747994386434 > > 请求方法: DELETE ```java @DeleteMapping public R delete(Long ids){ log.info("删除分类,id为:{}",ids); //自定义删除方法 categoryService.remove(ids); return R.success("分类删除成功"); } ``` > 根据id删除分类之前需要进行判断当前菜品或套餐分类是否关联了菜品或套餐 > > ```sql > select * from dish where category_id=?; > select * from setmeal where category_id=?; > delete from category where id=?; > ``` ```java @Autowired private DishService dishService; @Autowired private SetmealService setmealService; @Override public void remove(Long id) { LambdaQueryWrapper dishLambdaQueryWrapper = new LambdaQueryWrapper<>(); //添加查询条件,根据分类id进行查询 dishLambdaQueryWrapper.eq(Dish::getCategoryId,id); int count1 = dishService.count(dishLambdaQueryWrapper); //查询当前分类是否关联了菜品,如果已经关联,抛出一个业务异常 if(count1 > 0){ //已经关联菜品,抛出一个业务异常 throw new CustomException("当前分类关联了菜品,不能删除"); } //查询当前分类是否关联了套餐,如果已经关联,抛出一个业务异常 LambdaQueryWrapper setmealLambdaQueryWrapper = new LambdaQueryWrapper<>(); //添加查询条件,根据分类id进行查询 setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id); int count2 = setmealService.count(setmealLambdaQueryWrapper); if(count2 > 0){ //已经关联套餐,抛出一个业务异常 throw new CustomException("当前分类关联了套餐,不能删除"); } //正常删除分类 super.removeById(id); } ``` # 菜品管理 ## *1、新增 ### 1.1 查询(展示下拉框数据) > 请求网址: http://localhost:8090/category/list?type=1 > > 请求方法: GET ```java @GetMapping("/list") //请求的是请求行参数,不是请求体,不能加@RequestBody public R> list(Category category){ //条件构造器 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); //添加条件 queryWrapper.eq(category.getType() != null,Category::getType,category.getType()); //添加排序条件 queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime); List list = categoryService.list(queryWrapper); return R.success(list); } ``` ### 1.2 上传下载图片 > 请求网址: http://localhost:8090/common/upload > > 请求方法: POST > > 请求体: > > Content-Disposition: form-data; > > name="file"; filename="1.jpg" > > Content-Type: image/jpeg ``` ``` ### *1.3、新增 > 请求网址: http://localhost:8090/dish > > 请求方法: POST > > 请求体: (数据涉及两张表,没有flavors ==》dto ) > > { > "name": "1", > "price": 100, > "code": "", > "image": "7f468a6a-9b1b-4bbf-953a-b606f57e7d51.jpg", > "description": "1", > "status": 1, > "categoryId": "1397844263642378242", > "flavors": [{ > "name": "甜味", > "value": "[\"无糖\",\"少糖\",\"半糖\",\"多糖\",\"全糖\"]", > "showOption": false > }, { > "name": "温度", > "value": "[\"热饮\",\"常温\",\"去冰\",\"少冰\",\"多冰\"]", > "showOption": false > }] > } ```java @PostMapping public R save(@RequestBody DishDto dishDto){ log.info(dishDto.toString()); dishService.saveWithFlavor(dishDto); return R.success("新增菜品成功"); } ``` > 新增菜品,同时保存菜品对应的口味数据,需要操作两张表:dish、dish_flavor ```java @Autowired private DishFlavorService dishFlavorService; @Transactional @Override public void saveWithFlavor(DishDto dishDto) { //保存菜品的基本信息到菜品表dish this.save(dishDto); Long dishId = dishDto.getId();//菜品id //菜品口味 List flavors = dishDto.getFlavors(); flavors = flavors.stream().map((item) -> { //为口味设置对应的菜品 item.setDishId(dishId); return item; }).collect(Collectors.toList()); //保存菜品口味数据到菜品口味表dish_flavor dishFlavorService.saveBatch(flavors); } ``` ## *2、列表 > 请求网址: http://localhost:8090/dish/page?page=1&pageSize=10&name=%E7%B1%B3 > > 请求方法: GET > > 响应页面的数据: > > { > "code": 1, > "msg": null, > "data": { > "records": [{ > "id": "1413385247889891330", > "name": "米饭", > "categoryId": "1413384954989060097", > "price": 200.00, > "code": "", > "image": "ee04a05a-1230-46b6-8ad5-1a95b140fff3.png", > "description": "", > "status": 1, > "sort": 0, > "createTime": "2021-07-09 14:31:17", > "updateTime": "2021-07-11 16:35:26", > "createUser": "1", > "updateUser": "1", > "flavors": [], > "categoryName": "主食", > "copies": null > }], > "total": 1, > "size": 10, > "current": 1, > "orders": [], > "optimizeCountSql": true, > "searchCount": true, > "countId": null, > "maxLimit": null, > "pages": 1 > }, > "map": {} > } > 菜品表没有响应数据需要的分类名称categoryName,需要借助DishDto ```java @Autowired private DishService dishService; @Autowired private CategoryService categoryService; @GetMapping("/page") public R page(int page,int pageSize,String name){ //构造分页构造器对象 Page pageInfo = new Page<>(page,pageSize); //条件构造器 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); //添加过滤条件 queryWrapper.like(name != null,Dish::getName,name); //添加排序条件 queryWrapper.orderByDesc(Dish::getUpdateTime); //执行分页查询 dishService.page(pageInfo,queryWrapper); //构造新的分页构造器 Page dishDtoPage = new Page<>(); //对象拷贝 (不要records,因为没有categoryName) BeanUtils.copyProperties(pageInfo,dishDtoPage,"records"); // 数据虽然没有categoryName,但是有categoryId List records = pageInfo.getRecords(); List 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); } ``` ## *3、修改 ### 3.1 查询(展示下拉框数据) > 复用新增方法的查询 ### *3.2 查询(回写数据) ```java @GetMapping("/{id}") public R get(@PathVariable Long id){ DishDto dishDto = dishService.getByIdWithFlavor(id); return R.success(dishDto); } ``` > 根据id查询菜品信息和对应的口味信息 ```java public DishDto getByIdWithFlavor(Long id) { //查询菜品基本信息,从dish表查询 Dish dish = this.getById(id); DishDto dishDto = new DishDto(); BeanUtils.copyProperties(dish,dishDto); //查询当前菜品对应的口味信息,从dish_flavor表查询 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(DishFlavor::getDishId,dish.getId()); List flavors = dishFlavorService.list(queryWrapper); dishDto.setFlavors(flavors); return dishDto; } ``` ### *3.3 更新 > 请求网址: http://localhost:8090/dish > > 请求方法: PUT > > 请求体: (修改后的数据) > > { > "id": "1696189564025139202", > "name": "2", > "categoryId": "1397844303408574465", > "price": 200, > "code": "", > "image": "7f468a6a-9b1b-4bbf-953a-b606f57e7d51.jpg", > "description": "2", > "status": 1, > "sort": 0, > "createTime": "2023-08-28 23:54:40", > "createUser": "1", > "updateUser": "1", > "flavors": [{ > "id": "1696189564092248065", > "dishId": "1696189564025139202", > "name": "甜味", > "value": "[\"无糖\",\"少糖\",\"半糖\",\"多糖\",\"全糖\"]", > "createTime": "2023-08-28 23:54:40", > "updateTime": "2023-08-28 23:54:40", > "createUser": "1", > "updateUser": "1", > "isDeleted": 0, > "showOption": false > }, { > "id": "1696189564092248066", > "dishId": "1696189564025139202", > "name": "温度", > "value": "[\"热饮\",\"常温\",\"去冰\",\"少冰\",\"多冰\"]", > "createTime": "2023-08-28 23:54:40", > "updateTime": "2023-08-28 23:54:40", > "createUser": "1", > "updateUser": "1", > "isDeleted": 0, > "showOption": false > }], > "categoryName": null, > "copies": null > } > 更新菜品信息,同时更新对应的口味信息 ```java @PutMapping public R update(@RequestBody DishDto dishDto){ log.info(dishDto.toString()); dishService.updateWithFlavor(dishDto); return R.success("新增菜品成功"); } ``` ```java @Override @Transactional public void updateWithFlavor(DishDto dishDto) { //更新dish表基本信息 this.updateById(dishDto); //清理当前菜品对应口味数据---dish_flavor表的delete操作 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.eq(DishFlavor::getDishId,dishDto.getId()); dishFlavorService.remove(queryWrapper); //添加当前提交过来的口味数据---dish_flavor表的insert操作 List flavors = dishDto.getFlavors(); flavors = flavors.stream().map((item) -> { item.setDishId(dishDto.getId()); return item; }).collect(Collectors.toList()); dishFlavorService.saveBatch(flavors); } ``` ## 4、删除 > 请求网址: http://localhost:8090/dish?ids=1696189564025139202 > > 请求方法: DELETE ```java @DeleteMapping public R delete(long ids){ log.info("删除菜品,ids为: {}",ids); dishService.deleteWithFlavor(ids); return R.success("成功删除菜品"); } ``` > 删除菜品的同时,把菜品口味也删掉 ```java @Override public void deleteWithFlavor(long ids) { //先删除菜品表 this.removeById(ids); // 删除菜品口味 LambdaQueryWrapper lambdaQueryWrapper=new LambdaQueryWrapper(); lambdaQueryWrapper.eq(DishFlavor::getDishId,ids); dishFlavorService.remove(lambdaQueryWrapper); } ``` ## 5、停售启售 > 请求网址: http://localhost:8090/dish/status/0?ids=1696223046021517313 > > 请求方法: POST ```java @PostMapping("/status/{status}") public R updateStatus(@PathVariable int status,long ids){ log.info("请求dish的信息有,{} {}",status,ids); Dish dish = dishService.getById(ids); if(dish==null){ throw new CustomException("该菜品不存在"); } dish.setStatus(status); dishService.updateById(dish); return R.success("菜品状态修改成功"); } ``` # 套餐管理 ## 1、新增 ### 1.1 查询(展示套餐下拉框) > 复用分类管理的列表方法 ### 1.2 查询 (展示菜品分类) > 复用分类管理的列表方法 ### 1.3 查询 (展示菜品列表) > 请求网址: http://localhost:8090/dish/list?categoryId=1696222907286523905 > > 请求方法: GET ```java @GetMapping("/list") public R> list(Dish dish){ //构造查询条件 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(dish.getCategoryId() != null ,Dish::getCategoryId,dish.getCategoryId()); //添加条件,查询状态为1(起售状态)的菜品 queryWrapper.eq(Dish::getStatus,1); //添加排序条件 queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime); List list = dishService.list(queryWrapper); return R.success(list); } ``` ### 1.4 新增 > 请求网址: http://localhost:8090/setmeal > > 请求方法: POST > > 请求体: > > { > "name": "1", > "categoryId": "1413386191767674881", > "price": 100, > "code": "", > "image": "179d5a21-b60d-44f1-8980-ca025e115c8a.jpg", > "description": "1", > "dishList": [], > "status": 1, > "idType": "1413386191767674881", > "setmealDishes": [{ > "copies": 1, > "dishId": "1397851099502260226", > "name": "全家福", > "price": 11800 > }, { > "copies": 1, > "dishId": "1413384757047271425", > "name": "王老吉", > "price": 200 > }, { > "copies": 1, > "dishId": "1397860578738352129", > "name": "白切鸡", > "price": 6600 > }] > } ```java @PostMapping public R save(@RequestBody SetmealDto setmealDto){ log.info("套餐信息:{}",setmealDto); setmealService.saveWithDish(setmealDto); return R.success("新增套餐成功"); } ``` > 新增套餐,同时需要保存套餐和菜品的关联关系 > > ```sql > insert into setmeal values (?); > insert into setmeal_dish (dish_id,setmeal_id) value (?,?); > ``` ```java @Transactional public void saveWithDish(SetmealDto setmealDto) { //保存套餐的基本信息,操作setmeal,执行insert操作 this.save(setmealDto); List setmealDishes = setmealDto.getSetmealDishes(); setmealDishes.stream().map((item) -> { item.setSetmealId(setmealDto.getId()); return item; }).collect(Collectors.toList()); //保存套餐和菜品的关联信息,操作setmeal_dish,执行insert操作 setmealDishService.saveBatch(setmealDishes); } ``` ## 2、列表 > 请求网址: http://localhost:8090/setmeal/page?page=1&pageSize=10 > > 请求方法: GET > > 请求体有 categoryName ,需要使用DTO ```java @GetMapping("/page") public R page(int page,int pageSize,String name){ // 分页构造器 Page pageInfo=new Page(page,pageSize); // 条件构造器 LambdaQueryWrapper lambdaQueryWrapper=new LambdaQueryWrapper(); // like%name% 条件 lambdaQueryWrapper.like(name!=null,Setmeal::getName,name); setmealService.page(pageInfo,lambdaQueryWrapper); //新的分页构造器,含有分类名称categoryName Page dtoPage=new Page<>(page,pageSize); //复制pageInfo BeanUtils.copyProperties(pageInfo,dtoPage,"records"); List records = pageInfo.getRecords(); List list = records.stream().map((item) -> { // 创建新的数据传输对象 SetmealDto setmealDto = new SetmealDto(); //复制 Setmeal BeanUtils.copyProperties(item, setmealDto); Long categoryId = item.getCategoryId(); //根据id获取分类表,再获取分类名称 Category category = categoryService.getById(categoryId); if (category != null) { String categoryName = category.getName(); setmealDto.setCategoryName(categoryName); } return setmealDto; }).collect(Collectors.toList()); //把数据设置给返回值数据dtoPage dtoPage.setRecords(list); return R.success(dtoPage); } ``` ## 3、修改 ### 3.1 查询(下拉框) ### 3.2 查询 (展示菜品分类) ### 3.3 查询(展示菜品列表) ### 3.4 查询 (表单回写) > 请求网址: http://localhost:8090/setmeal/1696521458126237698 > > 请求方法: GET ```java @Override public SetmealDto getSetmealWithCategoryName(long ids) { // 新载体 SetmealDto setmealDto = new SetmealDto(); //查套餐表 Setmeal setmeal = this.getById(ids); //复制 BeanUtils.copyProperties(setmeal,setmealDto); //查setmealDish表 ,得到 菜品集合 LambdaQueryWrapper lambdaQueryWrapper=new LambdaQueryWrapper(); lambdaQueryWrapper.eq(SetmealDish::getDishId,ids); List list = setmealDishService.list(lambdaQueryWrapper); // 赋值 setmealDto.setSetmealDishes(list); // 获取分类id,再查分类表 ,得到categoryName ,再赋值 Long categoryId = setmeal.getCategoryId(); Category category = categoryService.getById(categoryId); String categoryName = category.getName(); setmealDto.setCategoryName(categoryName); return setmealDto; } ``` ### 3.5 更新 > 请求网址: http://localhost:8090/setmeal > > 请求方法: PUT ``` ``` ## 4、删除 > 请求网址: http://localhost:8090/setmeal?ids=1415580119015145474 > > 请求方法: DELETE > 删除套餐,同时删除相应的菜品 > > ```sql > delete from setmeal where id=?; > delete from setmeal_dish where setmeal_id=?; > ``` ```java @DeleteMapping public R deleteById(@RequestParam List ids){ setmealService.deleteWithDish(ids); return R.success("成功删除套餐"); } ``` ```java @Transactional @Override public void deleteWithDish(List ids) { //查询套餐状态,确定是否可用删除 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.in(Setmeal::getId,ids); queryWrapper.eq(Setmeal::getStatus,1); int count = this.count(queryWrapper); if(count > 0){ //如果不能删除,抛出一个业务异常 throw new CustomException("套餐正在售卖中,不能删除"); } //删除套餐 this.removeByIds(ids); //删除对应的菜品 LambdaQueryWrapper lambdaQueryWrapper=new LambdaQueryWrapper(); lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids); setmealDishService.remove(lambdaQueryWrapper); } ``` ## 5、启售停售 > 请求网址: http://localhost:8090/setmeal/status/0?ids=1696438029514735618 > > 请求方法: POST ```java @PostMapping("/status/{status}") public R updateStatus(@PathVariable int status,@RequestParam List ids){ log.info("请求套餐id的信息有,{},{}",ids,status); for (int i = 0; i < ids.size(); i++) { Long id = ids.get(i); Setmeal setmeal = setmealService.getById(id); if(setmeal==null){ throw new CustomException("该套餐不存在"); } setmeal.setStatus(status); setmealService.updateById(setmeal); } return R.success("套餐状态修改成功"); ```