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

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

添加套餐分類

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

表對應(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)建時間
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
//更新時間
@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員工實體的搭建。
1.2代碼開發(fā)
新增菜品分類與套餐分類請求的服務(wù)地址與提交的JSON數(shù)據(jù)結(jié)構(gòu)相同,服務(wù)端只需提供一個方法即可:
API
| 說明 | 值 |
| 請求URL | /category |
| 請求數(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("新增分類成功");
}
}二. 分類信息分頁查詢
分頁查詢與之前的員工信息查詢是一樣的,直接上代碼:
@GetMapping("/page")
public R<Page> page(int page, int pageSize){
// 分頁構(gòu)造
Page<Category> pageInfo = new Page<Category>(page,pageSize);
// 查詢并排序
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
queryWrapper.orderByAsc(Category::getSort);
// 分頁查詢
categoryService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}三. 刪除分類
3.1 需求分析
在分類管理列表頁面,可以對某個分類進(jìn)行刪除操作。需要注意的是當(dāng)分類關(guān)聯(lián)了菜品或者套餐時,此分類不允許刪除。

API
| 說明 | 值 |
| 請求URL | /category?id= |
需用引入菜品與套餐兩個實體:
Dish.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 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;
//菜品價格
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:套餐實體
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;
//套餐價格
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);
}
}四. 修改分類
修改分類很簡單,根據(jù)分類ID修改就可以了,代碼如下:
@PutMapping
public R<String> update(@RequestBody Category category){
log.info("修改分類信息{}" + category);
categoryService.updateById(category);
return R.success("分類修改成功");
}到此這篇關(guān)于Java精品項目瑞吉外賣之新增菜品與分頁查詢篇的文章就介紹到這了,更多相關(guān)Java外賣項目內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java?kafka如何動態(tài)設(shè)置用戶讀寫權(quán)限
這篇文章主要介紹了java?kafka如何動態(tài)設(shè)置用戶讀寫權(quán)限問題,具有很好的參考家價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
java.lang.OutOfMemoryError: Metaspace異常解決的方法
這篇文章主要介紹了java.lang.OutOfMemoryError: Metaspace異常解決的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解
這篇文章主要介紹了Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解,Sentinel Dashboard中添加的規(guī)則數(shù)據(jù)存儲在內(nèi)存,微服務(wù)停掉規(guī)則數(shù)據(jù)就消失,在?產(chǎn)環(huán)境下不合適,我們可以將Sentinel規(guī)則數(shù)據(jù)持久化到Nacos配置中?,讓微服務(wù)從Nacos獲取規(guī)則數(shù)據(jù),需要的朋友可以參考下2023-09-09

