蒼穹外賣--新增菜品

1.需求分析和設計

產品原型

業務規則:

菜品名稱必須是唯一的

菜品必須屬于某個分類下,不能單獨存在

新增菜品時可以根據情況選擇菜品的口味

每個菜品必須對應一張圖片

接口設計:

根據類型查詢分類(已完成)

文件上傳

新增菜品

根據類型查詢分類

數據庫設計(dish菜品表和dish_flavor口味表)

2.代碼開發

OssConfiguration代碼:

package com.sky.config;import com.sky.properties.AliOssProperties;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 配置類,用于創建AliOssUtil對象*/
@Configuration//注解,表示這是員工配置類
@Slf4j
public class OssConfiguration {@Bean//交給spring容器管理@ConditionalOnMissingBean//保證Spring容器里只有員工Util對象public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){log.info("開始創建阿里云文件上傳工具類對象:{}",aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());//讀取配置文件內的數據,然后給配置類賦值}
}

CommonController代碼:

package com.sky.controller.admin;import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.util.UUID;/*** 通用接口*/
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {@Autowiredprivate AliOssUtil aliOssUtil;/*** 文件上傳* @param file* @return*/@PostMapping("/upload")@ApiOperation("文件上傳")public Result<String> upload(MultipartFile file){//參數名要和前端的保持一致log.info("文件上傳:{}",file);try {//原始文件String originalFilename = file.getOriginalFilename();//截取原始文件名的后綴String extension = originalFilename.substring(originalFilename.lastIndexOf("."));//構造新文件名稱String objectName = UUID.randomUUID().toString() + extension;//文件的請求路徑String filePath = aliOssUtil.upload(file.getBytes(),objectName);return Result.success(filePath);} catch (IOException e) {log.info("文件上傳失敗:{}",e);}return Result.error(MessageConstant.UPLOAD_FAILED);}
}

DishController代碼:

package com.sky.controller.admin;import com.sky.dto.DishDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;/*** 菜品管理*/
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理接口")
@Slf4j
public class DishController {@Autowiredprivate DishService dishService;/*** 新增菜品* @param dishDTO* @return*/@PostMapping@ApiOperation("新增菜品")public Result save(@RequestBody DishDTO dishDTO){log.info("新增菜品:{}",dishDTO);dishService.saveWithFlavor(dishDTO);return Result.success();}
}

DishService代碼:

package com.sky.service;import com.sky.dto.DishDTO;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;@Service
public interface DishService {/*** 新增菜品和對應的口味* @param dishDTO*/void saveWithFlavor(DishDTO dishDTO);
}

DishServiceImpl代碼:

package com.sky.service.impl;import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service
@Slf4j
public class DishServiceImpl implements DishService {@Autowiredprivate DishMapper dishMapper;@Autowiredprivate DishFlavorMapper dishFlavorMapper;/*** 新增菜品和對應的口味* @param dishDTO*/@Transactionalpublic void saveWithFlavor(DishDTO dishDTO) {Dish dish = new Dish();BeanUtils.copyProperties(dishDTO,dish);//向菜品表插入1條數據dishMapper.insert(dish);//獲取inset語句生成的主鍵值Long dishId = dish.getId();//向口味表插入n條數據List<DishFlavor> flavors = dishDTO.getFlavors();if(flavors != null && flavors.size() > 0){//是否有口味數據flavors.forEach(dishFlavor -> {dishFlavor.setDishId(dishId);});dishFlavorMapper.insertBatch(flavors);}}
}

DishMapper代碼:

package com.sky.mapper;import com.sky.annotation.AutoFill;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;@Mapper
public interface DishMapper {/*** 根據分類id查詢菜品數量* @param categoryId* @return*/@Select("select count(id) from dish where category_id = #{categoryId}")Integer countByCategoryId(Long categoryId);/*** 新增菜品數據* @param dish*/@AutoFill(value = OperationType.INSERT)void insert(Dish dish);
}

DishFlavorMapper代碼:

package com.sky.mapper;import com.sky.entity.DishFlavor;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface DishFlavorMapper {/*** 批量插入口味數據* @param flavors*/void insertBatch(List<DishFlavor> flavors);
}

DishMapper.xml代碼

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.DishMapper"><insert id="insert" useGeneratedKeys="true" keyProperty="id">/*useGeneratedKeys="true"表示剛剛插入所生成的主鍵值,keyProperty="id"將id返回值給id*/INSERT INTO dish (name,category_id,price,image,description,create_time,update_time,create_user,update_user,status)VALUES(#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})</insert>
</mapper>

DishFlavorMapper.xml代碼:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.DishFlavorMapper"><insert id="insertBatch">INSERT INTO dish_flavor(dish_id,name,value) VALUES<foreach collection="flavors" item="df" separator=",">(#{df.dishId},#{df.name},#{df.value})</foreach></insert>
</mapper>

yml文件代碼:

  alioss:endpoint: ${sky.alioss.endpoint}access-key-id: ${sky.alioss.access-key-id}access-key-secret: ${sky.alioss.access-key-secret}bucket-name: ${sky.alioss.bucket-name}alioss:endpoint: oss-cn-beijing.aliyuncs.comaccess-key-id: LTAI5tPeFLzsPPT8gG3LPW64access-key-secret: U6k1brOZ8gaOIXv3nXbulGTUzy6Pd7bucket-name: sky-itcast

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/83038.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/83038.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/83038.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何高效集成MySQL數據到金蝶云星空

MySQL數據集成到金蝶云星空&#xff1a;SC采購入庫-深圳天一-OK案例分享 在企業信息化建設中&#xff0c;數據的高效流轉和準確對接是實現業務流程自動化的關鍵。本文將聚焦于一個具體的系統對接集成案例——“SC采購入庫-深圳天一-OK”&#xff0c;詳細探討如何通過輕易云數據…

【springcloud學習(dalston.sr1)】使用Feign實現接口調用(八)

該系列項目整體介紹及源代碼請參照前面寫的一篇文章【springcloud學習(dalston.sr1)】項目整體介紹&#xff08;含源代碼&#xff09;&#xff08;一&#xff09; &#xff08;一&#xff09;Feign的理解 前面文章【springcloud學習(dalston.sr1)】服務消費者通過restTemplat…

SpringbBoot nginx代理獲取用戶真實IP

為了演示多級代理場景&#xff0c;我們分配了以下服務器資源&#xff1a; 10.1.9.98&#xff1a;充當客戶端10.0.3.137&#xff1a;一級代理10.0.4.105&#xff1a;二級代理10.0.4.129&#xff1a;三級代理10.0.4.120&#xff1a;服務器端 各級代理配置 以下是各級代理的基本配…

實驗九視圖索引

設計性實驗 1. 創建視圖V_A包括學號&#xff0c;姓名&#xff0c;性別&#xff0c;課程號&#xff0c;課程名、成績&#xff1b; 一個語句把學號103 課程號3-105 的姓名改為陸君茹1&#xff0c;性別為女 &#xff0c;然后查看學生表的信息變化&#xff0c;再把上述數據改為原…

typeof運算符和深拷貝

typeof運算符 識別所有值類型識別函數判斷是否是引用類型&#xff08;不可再細分&#xff09; //判斷所有值類型 let a; typeof a //undefined const strabc; typeof str //string const n100; typeof n //number const …

NAT/代理服務器/內網穿透

目錄 一 NAT技術 二 內網穿透/內網打洞 三 代理服務器 一 NAT技術 跨網絡傳輸的時候&#xff0c;私網不能直接訪問公網&#xff0c;就引入了NAT能講私網轉換為公網進行訪問&#xff0c;主要解決IPv4(2^32)地址不足的問題。 1. NAT原理 當某個內網想訪問公網&#xff0c;就必…

Git的安裝和配置(idea中配置Git)

一、Git的下載和安裝 前提條件&#xff1a;IntelliJ IDEA 版本是2023.3 &#xff0c;那么配置 Git 時推薦使用 Git 2.40.x 或更高版本 下載地址&#xff1a;CNPM Binaries Mirror 操作&#xff1a;打開鏈接 → 滾動到頁面底部 → 選擇2.40.x或更高版本的 .exe 文件&#xf…

【教程】Docker更換存儲位置

轉載請注明出處&#xff1a;小鋒學長生活大爆炸[xfxuezhagn.cn] 如果本文幫助到了你&#xff0c;歡迎[點贊、收藏、關注]哦~ 目錄 背景說明 更換教程 1. 停止 Docker 服務 2. 創建新的存儲目錄 3. 編輯 Docker 配置文件 4. 遷移已有數據到新位置 5. 啟動 Docker 服務 6…

PostgreSQL 配置設置函數

PostgreSQL 配置設置函數 PostgreSQL 提供了一組配置設置函數&#xff08;Configuration Settings Functions&#xff09;&#xff0c;用于查詢和修改數據庫服務器的運行時配置參數。這些函數為數據庫管理員提供了動態管理數據庫配置的能力&#xff0c;無需重啟數據庫服務。 …

sql server 2019 將單用戶狀態修改為多用戶狀態

記錄兩種將單用戶狀態修改為多用戶狀態&#xff0c;我曾經成功過的方法&#xff0c;供參考 第一種方法 USE master; GO -- 終止所有活動連接 DECLARE kill_connections NVARCHAR(MAX) ; SELECT kill_connections KILL CAST(session_id AS NVARCHAR(10)) ; FROM sys.dm_ex…

主機A向主機B發送一個長度為L字節的文件,假設TCP的MSS為1460字節,則在TCP的序號不重復使用的前提下,L的最大值是多少?

&#x1f4d8;題干回顧&#xff1a; 主機A向主機B發送一個長度為L字節的文件&#xff0c;假設TCP的MSS為1460字節&#xff0c;則在TCP的序號不重復使用的前提下&#xff0c;L的最大值是多少&#xff1f; 這個問題關鍵在于“TCP序號不重復使用”。 ? 正確答案是&#xff1a;D.…

一次因校時服務器異常引起的性能差異分析

一次因校時服務器異常引起的性能差異分析 一.背景知識1. **TSC 頻率**:硬件級高精度計時2. **gettimeofday**:用戶態時間接口3. **adjtimex**:系統時鐘的軟件校準4. **`clock_adjtime(CLOCK_REALTIME, {modes=ADJ_TICK})`**: 用于修改系統時鐘中斷間隔(`tick` 值)。5. 關系…

acwing 4275. Dijkstra序列

題目背景 輸入 輸出 完整代碼 #include<bits/stdc.h> using namespace std; int n,m,k,a[1010],dist[1010],g[1010][1010],st[1010];int dij(int u){memset(st,0,sizeof st);memset(dist,0x3f,sizeof dist);dist[u]0;for(int i0;i<n;i){int ta[i];for(int j1;j<n;…

[思維模式-37]:什么是事?什么是物?什么事物?如何通過數學的方法闡述事物?

一、基本概念 1、事&#xff08;Event) “事”通常指的是人類在社會生活中的各種活動、行為、事件或情況&#xff0c;具有動態性和過程性&#xff0c;強調的是一種變化、發展或相互作用的流程。 特點 動態性&#xff1a;“事”往往涉及一系列的動作、變化和發展過程。例如&a…

Linux常用命令40——alias設置命令別名

在使用Linux或macOS日常開發中&#xff0c;熟悉一些基本的命令有助于提高工作效率&#xff0c;alias命令來自英文單詞alias&#xff0c;中文譯為“別名”&#xff0c;其功能是設置命令別名信息。我們可以使用alias將一些較長的命令進行簡寫&#xff0c;往往幾十個字符的命令會變…

310. 最小高度樹

題目 樹是一個無向圖&#xff0c;其中任何兩個頂點只通過一條路徑連接。 換句話說&#xff0c;任何一個沒有簡單環路的連通圖都是一棵樹。 給你一棵包含 n 個節點的樹&#xff0c;標記為 0 到 n - 1 。給定數字 n 和一個有 n - 1 條無向邊的 edges 列表&#xff08;每一個邊都…

Axure 縱向滾動隱藏滾動條 Axure 滑動開關(屬性開關)on-off

文章目錄 I 滑動開關(屬性開關)操作說明block 矩形操作說明round小圓圈操作說明on-off 屬性開關組合操作說明II Axure 縱向滾動隱藏滾動條思路包含圖片的動態面板1操作說明包含動態面板的頂級動態面板I 滑動開關(屬性開關)操作說明 block 矩形操作說明 在畫布中添加一個矩形…

MySQL之基礎事務

目錄 引言&#xff1a; 什么是事務&#xff1f; 事務和鎖 mysql數據庫控制臺事務的幾個重要操作指令&#xff08;transaction.sql&#xff09; 1、事物操作示意圖&#xff1a; 2.事務的隔離級別 四種隔離級別&#xff1a; 總結一下隔離指令 1. 查看當前隔離級別?? …

VS Code 重磅更新:全新 MCP 服務器發現中心上線

目前各種 MCP 客戶端層出不窮&#xff0c;但是安裝 MCP 服務卻格外繁瑣&#xff0c;尤其 VS Code 中無界面化的 MCP 服務配置方式&#xff0c;效率較低。 Copilot MCP 是一個 VS Code 插件&#xff0c;在今天發布的新版本中&#xff0c;插件支持了自動發現與安裝開源 MCP 服務…

智能家居“心臟“升級戰:GD25Q127CSIG國產芯片如何重構家庭物聯生態

在智能家居設備出貨量突破10億臺的2023年&#xff0c;家庭網關正經歷著前所未有的技術革新。作為連接云端與終端設備的中樞神經&#xff0c;智能網關的存儲芯片選擇直接決定著整個智能生態系統的運行效率。在這場技術升級浪潮中&#xff0c;兆易創新GD25Q127CSIG串行閃存芯片主…