Java精品項(xiàng)目瑞吉外賣之新增菜品與分頁(yè)查詢篇
一. 新增菜品
1.1需求分析
后臺(tái)系統(tǒng)可以管理分類信息,分類菜品分類和套餐分類。當(dāng)我們?cè)诤笈_(tái)系統(tǒng)添加菜品時(shí)需要選擇一個(gè)菜品分類。
當(dāng)我們?cè)诤笈_(tái)系統(tǒng)中添加一個(gè)套餐時(shí)需要選擇一個(gè)套餐分類,在移動(dòng)端也會(huì)按照菜品分類和套餐分類來(lái)展示對(duì)應(yīng)的菜品和套餐。

同時(shí),在后臺(tái)系統(tǒng)的分類管理頁(yè)面分別添加菜品分類與套餐分類:
添加菜品分類

添加套餐分類

數(shù)據(jù)模型:
涉及一張表Category表:

表對(duì)應(yīng)的數(shù)據(jù)JavaBean為Category.java
Category.java
package com.itheima.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 分類
*/
@Data
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//類型 1 菜品分類 2 套餐分類
private Integer type;
//分類名稱
private String name;
//順序
private Integer sort;
//創(chuàng)建時(shí)間
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
//更新時(shí)間
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
//創(chuàng)建人
@TableField(fill = FieldFill.INSERT)
private Long createUser;
//修改人
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//是否刪除
private Integer isDeleted;
}具體架子參照前面的Employee員工實(shí)體的搭建。
1.2代碼開(kāi)發(fā)
新增菜品分類與套餐分類請(qǐng)求的服務(wù)地址與提交的JSON數(shù)據(jù)結(jié)構(gòu)相同,服務(wù)端只需提供一個(gè)方法即可:
API
| 說(shuō)明 | 值 |
| 請(qǐng)求URL | /category |
| 請(qǐng)求數(shù)據(jù) | { "name": "川菜", "type": "1", "sort": "1" } |
代碼
在CategoryController.java中編寫新增代碼:
package com.itheima.reggie.controller;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author jektong
* @date 2022年05月06日 21:47
*/
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
@Resource
private CategoryService categoryService;
/**
* 新增分類
* @param category
* @return
*/
@PostMapping
public R<String> save(@RequestBody Category category){
log.info("category:{}",category);
categoryService.save(category);
return R.success("新增分類成功");
}
}二. 分類信息分頁(yè)查詢
分頁(yè)查詢與之前的員工信息查詢是一樣的,直接上代碼:
@GetMapping("/page")
public R<Page> page(int page, int pageSize){
// 分頁(yè)構(gòu)造
Page<Category> pageInfo = new Page<Category>(page,pageSize);
// 查詢并排序
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
queryWrapper.orderByAsc(Category::getSort);
// 分頁(yè)查詢
categoryService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}三. 刪除分類
3.1 需求分析
在分類管理列表頁(yè)面,可以對(duì)某個(gè)分類進(jìn)行刪除操作。需要注意的是當(dāng)分類關(guān)聯(lián)了菜品或者套餐時(shí),此分類不允許刪除。

API
| 說(shuō)明 | 值 |
| 請(qǐng)求URL | /category?id= |
需用引入菜品與套餐兩個(gè)實(shí)體:
Dish.java:菜品實(shí)體
package com.itheima.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
菜品
*/
@Data
public class Dish implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//菜品名稱
private String name;
//菜品分類id
private Long categoryId;
//菜品價(jià)格
private BigDecimal price;
//商品碼
private String code;
//圖片
private String image;
//描述信息
private String description;
//0 停售 1 起售
private Integer status;
//順序
private Integer sort;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//是否刪除
private Integer isDeleted;
}Setmeal.java:套餐實(shí)體
package com.itheima.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 套餐
*/
@Data
public class Setmeal implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//分類id
private Long categoryId;
//套餐名稱
private String name;
//套餐價(jià)格
private BigDecimal price;
//狀態(tài) 0:停用 1:啟用
private Integer status;
//編碼
private String code;
//描述信息
private String description;
//圖片
private String image;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//是否刪除
private Integer isDeleted;
}3.2 核心代碼
CategoryServiceImpl.java
package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author jektong
* @date 2022年05月06日 21:44
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Resource
private DishService dishService;
@Resource
private SetmealService setmealService;
/**
* 根據(jù)ID刪除分類,分類之前需要判斷
* @param id
*/
@Override
public void remove(Long id) {
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
// 查詢當(dāng)前分類是否關(guān)聯(lián)了菜品,若關(guān)聯(lián)菜品,拋出異常
dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
int count = dishService.count(dishLambdaQueryWrapper);
if(count > 0){
// 已經(jīng)關(guān)聯(lián)菜品,拋出異常
throw new CustomException("當(dāng)前分類已關(guān)聯(lián)菜品,不可刪除");
}
// 查詢當(dāng)前分類是否關(guān)聯(lián)了套餐,若關(guān)聯(lián)菜品,拋出異常
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
int count1 = setmealService.count(setmealLambdaQueryWrapper);
if(count>0){
// 已經(jīng)關(guān)聯(lián)套餐,拋出異常
throw new CustomException("當(dāng)前分類已關(guān)聯(lián)套餐,不可刪除");
}
// 正常刪除分類
super.removeById(id);
}
}前面自定義異常類中加入:
/**
* 異常處理方法
* @param customException
* @return
*/
@ExceptionHandler(CustomException.class)
public R<String> exceptionHandler(CustomException customException){
log.error(customException.getMessage());
return R.error(customException.getMessage());
}CustomException.java
package com.itheima.reggie.common;
/**
* @author jektong
* @date 2022年05月10日 22:26
*/
public class CustomException extends RuntimeException{
public CustomException(String msg){
super(msg);
}
}四. 修改分類
修改分類很簡(jiǎn)單,根據(jù)分類ID修改就可以了,代碼如下:
@PutMapping
public R<String> update(@RequestBody Category category){
log.info("修改分類信息{}" + category);
categoryService.updateById(category);
return R.success("分類修改成功");
}到此這篇關(guān)于Java精品項(xiàng)目瑞吉外賣之新增菜品與分頁(yè)查詢篇的文章就介紹到這了,更多相關(guān)Java外賣項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合mybatis-plus快速入門超詳細(xì)教程
mybatis-plus 是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,本文給大家分享SpringBoot整合mybatis-plus快速入門超詳細(xì)教程,一起看看吧2021-09-09
Mybatis從3.4.0版本到3.5.7版本的迭代方法實(shí)現(xiàn)
本文主要介紹了Mybatis從3.4.0版本到3.5.7版本的迭代方法實(shí)現(xiàn),包括主要的功能增強(qiáng)、不兼容的更改和修復(fù)的錯(cuò)誤,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
Spring中ContextLoaderListener監(jiān)聽(tīng)詳解
這篇文章主要介紹了Spring中ContextLoaderListener監(jiān)聽(tīng)詳解,SpringMVC啟動(dòng)時(shí)會(huì)啟動(dòng)WebApplicationContext類型的容器,并且會(huì)調(diào)用之前分析的refresh方法,需要的朋友可以參考下2024-01-01
jdk中動(dòng)態(tài)代理異常處理分析:UndeclaredThrowableException
最近在工作中遇到了報(bào)UndeclaredThrowableException的錯(cuò)誤,通過(guò)查找相關(guān)的資料,終于解決了,所以這篇文章主要給大家介紹了關(guān)于jdk中動(dòng)態(tài)代理異常處理分析:UndeclaredThrowableException的相關(guān)資料,需要的朋友可以參考下2018-04-04
Spring mvc是如何實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的前后端的連接操作的?
今天給大家?guī)?lái)的是關(guān)于Spring mvc的相關(guān)知識(shí),文章圍繞著Spring mvc是如何實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的前后端的連接操作的展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06

