欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java精品項(xiàng)目瑞吉外賣之新增菜品與分頁查詢篇

 更新時(shí)間:2022年05月12日 17:08:26   作者:爪哇斗羅  
這篇文章主要為大家詳細(xì)介紹了java精品項(xiàng)目-瑞吉外賣訂餐系統(tǒng),此項(xiàng)目過大,分為多章獨(dú)立講解,本篇內(nèi)容為新增菜品和分頁查詢功能的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一. 新增菜品

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ì)按照菜品分類和套餐分類來展示對(duì)應(yīng)的菜品和套餐。

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

添加菜品分類

添加套餐分類

數(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代碼開發(fā)

新增菜品分類與套餐分類請(qǐng)求的服務(wù)地址與提交的JSON數(shù)據(jù)結(jié)構(gòu)相同,服務(wù)端只需提供一個(gè)方法即可:

API

說明
請(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("新增分類成功");
    }
}

二. 分類信息分頁查詢

分頁查詢與之前的員工信息查詢是一樣的,直接上代碼:

@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 需求分析

在分類管理列表頁面,可以對(duì)某個(gè)分類進(jìn)行刪除操作。需要注意的是當(dāng)分類關(guān)聯(lián)了菜品或者套餐時(shí),此分類不允許刪除。

API

說明
請(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)目瑞吉外賣之新增菜品與分頁查詢篇的文章就介紹到這了,更多相關(guān)Java外賣項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • flink進(jìn)階富函數(shù)生命周期介紹

    flink進(jìn)階富函數(shù)生命周期介紹

    這篇文章主要為大家介紹了flink進(jìn)階富函數(shù)生命周期的舉例介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • java Thumbnails 圖片處理的使用

    java Thumbnails 圖片處理的使用

    這篇文章主要介紹了java Thumbnails 圖片處理的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • java圖形界面編程之模擬血壓計(jì)

    java圖形界面編程之模擬血壓計(jì)

    本文主要介紹了java基于圖形處理的模擬血壓計(jì),創(chuàng)新實(shí)驗(yàn)項(xiàng)目的部分代碼,作為平時(shí)練習(xí)用。
    2014-02-02
  • Java的List集合框架之ArrayList詳解

    Java的List集合框架之ArrayList詳解

    這篇文章主要介紹了Java的List集合框架之ArrayList詳解,ArrayList默認(rèn)容量為10(構(gòu)造方法未指定初始容量為0),擴(kuò)容是利用位運(yùn)算(右移一位)和直接相加進(jìn)行1.5倍擴(kuò)容,需要的朋友可以參考下
    2023-11-11
  • java?kafka如何動(dòng)態(tài)設(shè)置用戶讀寫權(quán)限

    java?kafka如何動(dòng)態(tài)設(shè)置用戶讀寫權(quán)限

    這篇文章主要介紹了java?kafka如何動(dòng)態(tài)設(shè)置用戶讀寫權(quán)限問題,具有很好的參考家價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Java基于Tcp/ip連接的多人交互聊天室

    Java基于Tcp/ip連接的多人交互聊天室

    這篇文章主要為大家詳細(xì)介紹了Java基于Tcp/ip連接的多人交互聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • SpringCloud微服務(wù)架構(gòu)升級(jí)匯總

    SpringCloud微服務(wù)架構(gòu)升級(jí)匯總

    這篇文章主要介紹了SpringCloud微服務(wù)架構(gòu)升級(jí)匯總,它提倡將單一應(yīng)用程序劃分成一組小的服務(wù),服務(wù)之間互相協(xié)調(diào)、互相配合,為用戶提供最終價(jià)值,需要的朋友可以參考下
    2019-06-06
  • java.lang.OutOfMemoryError: Metaspace異常解決的方法

    java.lang.OutOfMemoryError: Metaspace異常解決的方法

    這篇文章主要介紹了java.lang.OutOfMemoryError: Metaspace異常解決的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java中基于Nacos實(shí)現(xiàn)Sentinel規(guī)則持久化詳解

    Java中基于Nacos實(shí)現(xiàn)Sentinel規(guī)則持久化詳解

    這篇文章主要介紹了Java中基于Nacos實(shí)現(xiàn)Sentinel規(guī)則持久化詳解,Sentinel Dashboard中添加的規(guī)則數(shù)據(jù)存儲(chǔ)在內(nèi)存,微服務(wù)停掉規(guī)則數(shù)據(jù)就消失,在?產(chǎn)環(huán)境下不合適,我們可以將Sentinel規(guī)則數(shù)據(jù)持久化到Nacos配置中?,讓微服務(wù)從Nacos獲取規(guī)則數(shù)據(jù),需要的朋友可以參考下
    2023-09-09
  • MyBatis注解式開發(fā)映射語句詳解

    MyBatis注解式開發(fā)映射語句詳解

    這幾年來注解開發(fā)越來越流行,Mybatis也可以使用注解開發(fā)方式,這樣我們就可以減少編寫Mapper映射文件了。我們先圍繞一些基本的CRUD來學(xué)習(xí),再學(xué)習(xí)復(fù)雜映射多表操作
    2023-02-02

最新評(píng)論