🔄Update: 创建 product 相关模块

This commit is contained in:
2025-10-10 22:28:52 +08:00
parent 06009637b4
commit 59feaf0ccd
5 changed files with 140 additions and 0 deletions
@@ -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<Product> 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);
}
}
@@ -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;
}
@@ -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<Product> {
}
@@ -0,0 +1,15 @@
package cn.hezhaohui.pc.service;
import cn.hezhaohui.pc.entity.Product;
import java.util.List;
public interface ProductService {
List<Product> listAll();
Product getById(Long id);
void saveProduct(Product Product);
void updateProduct(Product Product);
void deleteProduct(Long id);
}
@@ -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<Product> 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);
}
}