From 59feaf0ccd9545bed2367bb5f043d7cf90bf0835 Mon Sep 17 00:00:00 2001 From: Wonder Date: Fri, 10 Oct 2025 22:28:52 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=84Update:=20=E5=88=9B=E5=BB=BA=20p?= =?UTF-8?q?roduct=20=E7=9B=B8=E5=85=B3=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pc/controller/ProductController.java | 49 +++++++++++++++++++ .../java/cn/hezhaohui/pc/entity/Product.java | 20 ++++++++ .../cn/hezhaohui/pc/mapper/ProductMapper.java | 9 ++++ .../hezhaohui/pc/service/ProductService.java | 15 ++++++ .../pc/service/impl/ProductServiceImpl.java | 47 ++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 src/main/java/cn/hezhaohui/pc/controller/ProductController.java create mode 100644 src/main/java/cn/hezhaohui/pc/entity/Product.java create mode 100644 src/main/java/cn/hezhaohui/pc/mapper/ProductMapper.java create mode 100644 src/main/java/cn/hezhaohui/pc/service/ProductService.java create mode 100644 src/main/java/cn/hezhaohui/pc/service/impl/ProductServiceImpl.java diff --git a/src/main/java/cn/hezhaohui/pc/controller/ProductController.java b/src/main/java/cn/hezhaohui/pc/controller/ProductController.java new file mode 100644 index 0000000..c375d48 --- /dev/null +++ b/src/main/java/cn/hezhaohui/pc/controller/ProductController.java @@ -0,0 +1,49 @@ +package cn.hezhaohui.pc.controller; + +import cn.hezhaohui.pc.entity.Product; +import cn.hezhaohui.pc.service.ProductService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/product") +@Tag(name = "商品") +public class ProductController { + + @Resource + private ProductService productService; + + @GetMapping + @Operation(summary = "列出所有商品") + public List listAll() { + return productService.listAll(); + } + + @GetMapping("/{id}") + @Operation(summary = "通过id查找商品") + public Product findById(@PathVariable Long id) { + return productService.getById(id); + } + + @PostMapping + @Operation(summary = "添加商品") + public void add(@RequestBody Product product) { + productService.saveProduct(product); + } + + @PutMapping + @Operation(summary = "更新商品") + public void update(@RequestBody Product product) { + productService.updateProduct(product); + } + + @DeleteMapping("/{id}") + @Operation(summary = "删除商品") + public void delete(@PathVariable Long id) { + productService.deleteProduct(id); + } +} diff --git a/src/main/java/cn/hezhaohui/pc/entity/Product.java b/src/main/java/cn/hezhaohui/pc/entity/Product.java new file mode 100644 index 0000000..d944e41 --- /dev/null +++ b/src/main/java/cn/hezhaohui/pc/entity/Product.java @@ -0,0 +1,20 @@ +package cn.hezhaohui.pc.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@TableName("products") +public class Product { + @TableId(type = IdType.AUTO) + private Long id; + private String name; + private String description; + private Double price; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/src/main/java/cn/hezhaohui/pc/mapper/ProductMapper.java b/src/main/java/cn/hezhaohui/pc/mapper/ProductMapper.java new file mode 100644 index 0000000..0eb039b --- /dev/null +++ b/src/main/java/cn/hezhaohui/pc/mapper/ProductMapper.java @@ -0,0 +1,9 @@ +package cn.hezhaohui.pc.mapper; + +import cn.hezhaohui.pc.entity.Product; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface ProductMapper extends BaseMapper { +} diff --git a/src/main/java/cn/hezhaohui/pc/service/ProductService.java b/src/main/java/cn/hezhaohui/pc/service/ProductService.java new file mode 100644 index 0000000..885f59c --- /dev/null +++ b/src/main/java/cn/hezhaohui/pc/service/ProductService.java @@ -0,0 +1,15 @@ +package cn.hezhaohui.pc.service; + + + +import cn.hezhaohui.pc.entity.Product; + +import java.util.List; + +public interface ProductService { + List listAll(); + Product getById(Long id); + void saveProduct(Product Product); + void updateProduct(Product Product); + void deleteProduct(Long id); +} diff --git a/src/main/java/cn/hezhaohui/pc/service/impl/ProductServiceImpl.java b/src/main/java/cn/hezhaohui/pc/service/impl/ProductServiceImpl.java new file mode 100644 index 0000000..583397b --- /dev/null +++ b/src/main/java/cn/hezhaohui/pc/service/impl/ProductServiceImpl.java @@ -0,0 +1,47 @@ +package cn.hezhaohui.pc.service.impl; + +import cn.hezhaohui.pc.entity.Product; +import cn.hezhaohui.pc.mapper.ProductMapper; +import cn.hezhaohui.pc.service.ProductService; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.util.List; + +@Service +public class ProductServiceImpl implements ProductService { + + @Resource + private ProductMapper productMapper; + + @Override + public List listAll() { + return productMapper.selectList(null); + } + + @Override + public Product getById(Long id) { + return productMapper.selectById(id); + } + + @Override + public void saveProduct(Product Product) { + LocalDateTime now = LocalDateTime.now(); + Product.setCreatedAt(now); + Product.setUpdatedAt(now); + productMapper.insert(Product); + } + + @Override + public void updateProduct(Product Product) { + LocalDateTime now = LocalDateTime.now(); + Product.setUpdatedAt(now); + productMapper.updateById(Product); + } + + @Override + public void deleteProduct(Long id) { + productMapper.deleteById(id); + } +}