优品商城-商品类别、商品、商品订单

本文档展示了使用Spring Boot实现商品类别、商品及商品订单的增删改查功能。涉及Mapper、Service、Controller各层组件,包括MyBatis映射、业务逻辑处理和RESTful API设计。此外,还包含了商品的分页查询和FastDFS文件上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 商品类别(增删改查)

在这里插入图片描述

1.1 GoodsCategoryMapper

package com.xzy.goods.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzy.common.entity.GoodsCategory;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface GoodsCategoryMapper extends BaseMapper<GoodsCategory> {
}

1.2 GoodsCategoryService

package com.xzy.goods.service;

import com.xzy.common.entity.GoodsCategory;
import com.xzy.goods.dao.GoodsCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 添加
     *
     * @param title
     * @param descp
     * @return
     */
    public int add(String title, String descp) {
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setTitle(title);
        goodsCategory.setDescp(descp);
        int row = goodsCategoryMapper.insert(goodsCategory);
        return row;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(String id) {
        return goodsCategoryMapper.deleteById(id);
    }

    /**
     * @param id    修改的条件
     * @param title 修改数据
     * @param descp 修改数据
     * @return
     */
    public int update(String id, String title,String descp) {
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setId(id);
        goodsCategory.setTitle(title);
        goodsCategory.setDescp(descp);
        return goodsCategoryMapper.updateById(goodsCategory);
    }

    /**
     * 查询
     * @return
     */
    public List<GoodsCategory> listALl() {
        return goodsCategoryMapper.selectList(null);
    }
}

1.3 GoodsCategoryController

package com.xzy.goods.controller;

import com.xzy.common.entity.GoodsCategory;
import com.xzy.common.entity.vo.Response;
import com.xzy.common.entity.vo.ResponseCode;
import com.xzy.goods.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/goodsCategory")
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService = new GoodsCategoryService();

    /**
     * 添加商品类别
     * @param title 类别名字
     * @param descp 类别描述
     * @return
     */
    @PostMapping("/add")
    public Response add(String title,String descp) {
        int row = goodsCategoryService.add(title, descp);
        Response response = new Response();
        if(row>0){
            response.code(ResponseCode.SUCCESS).msg("添加成功");
        }else {
            response.code(ResponseCode.ERROR).msg("添加失败");
        }
        return response;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Response delete(String id){
        Response response = new Response();
        int row = goodsCategoryService.delete(id);
        if(row>0){
            response.code(ResponseCode.SUCCESS).msg("商品类别删除成功");
        }else{
            response.code(ResponseCode.ERROR).msg("商品类别删除失败");
        }
        return response;
    }

    /**
     * 修改
     * @param id 条件
     * @param title 修改标题
     * @param descp 修改描述
     * @return
     */
    @PostMapping("/update")
    public Response update(String id, String title,String descp){
        Response response = new Response();
        int row = goodsCategoryService.update(id,title,descp);
        if(row>0){
            response.code(ResponseCode.SUCCESS).msg("商品类别修改成功");
        }else{
            response.code(ResponseCode.ERROR).msg("商品类别修改失败");
        }
        return response;
    }

    /**
     * 查询所有商品类别
     * @return
     */
    @GetMapping("/listAll")
    public Response listAll(){
        Response response = new Response();
        List<GoodsCategory> goodsCategoriesList = goodsCategoryService.listALl();
        response.code(ResponseCode.SUCCESS).msg("成功").data(goodsCategoriesList);
        return response;
    }
}

2 商品(增删改查)

在这里插入图片描述

2.1 GoodsMapper

package com.xzy.goods.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzy.common.entity.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
    List<Goods> selectListByTitleAndPlatform(@Param("title") String title, @Param("platform") Integer platform, @Param("start") Long start, @Param("size") Long size);
    Long selectCountByTitleAndPlatform(@Param("title") String title, @Param("platform") Integer platform);
}

2.2 GoodsService

package com.xzy.goods.service;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.xzy.common.entity.Goods;
import com.xzy.common.util.FdfsUtils;
import com.xzy.goods.dao.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    public List<Goods> getIndexData() {
        return goodsMapper.selectList(null);
    }

    /**
     * 添加商品
     *
     * @param title        商品标题
     * @param descp        商品描述
     * @param originPrice  商品原价(分)
     * @param currentPrice 现价(分)
     * @param image        图片
     * @param detail       详情
     * @param cid          分类id
     * @param stock        库存
     * @return
     */
    public int add(String title, String descp, Long originPrice, Long currentPrice, MultipartFile image, String detail, String cid, Long stock) throws IOException {
        //上传商品图片
        String fullPath = FdfsUtils.upLoadFile(fastFileStorageClient, image.getInputStream(), image.getOriginalFilename(), image.getSize());
        Goods goods = new Goods();
        goods.setTitle(title);
        goods.setDescp(descp);
        goods.setOriginPrice(originPrice);
        goods.setCurrentPrice(currentPrice);
        goods.setImageUrl(fullPath);
        goods.setDetail(detail);
        goods.setCId(cid);
        goods.setStock(stock);
        return goodsMapper.insert(goods);
    }


    public int deleteById(String id) {
        return goodsMapper.deleteById(id);
    }

    /**
     * 修改
     *
     * @param id           条件
     * @param title        商品标题
     * @param descp        商品描述
     * @param originPrice  商品原价(分)
     * @param currentPrice 商品现价(分)
     * @param detail       商品详情
     * @param status       商品状态 0:待审批  1:以上架  2:已拒绝
     * @param cId          商品分类id
     * @param gmtModify    商品修改日期
     * @return
     */
    public int update(String id, String title, String descp, Long originPrice, Long currentPrice, String detail, Integer status, String cId, Date gmtModify) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setTitle(title);
        goods.setDescp(descp);
        goods.setOriginPrice(originPrice);
        goods.setCurrentPrice(currentPrice);
        goods.setDetail(detail);
        goods.setStatus(status);
        goods.setCId(cId);
        Date date = new Date();
        goods.setGmtModify(date);

        return goodsMapper.updateById(goods);
    }

    /**
     * 查询
     *
     * @param title    标题
     * @param platform 客户端平台(0:管理系统,1:小程序)
     * @return
     */
    public List<Goods> listGoods(String title, Integer platform, Long current, Long size) {
        Long start = (current - 1) * size;//计算开始位置
        return goodsMapper.selectListByTitleAndPlatform(title, platform, start, size);
    }

    /**
     * 查询条数
     * @param title 标题
     * @param platform 平台
     * @return
     */
    public Long selectCount(String title, Integer platform){
        return goodsMapper.selectCountByTitleAndPlatform(title,platform);
    }
}

2.3 GoodsController

package com.xzy.goods.controller;


import com.xzy.common.entity.Goods;
import com.xzy.common.entity.vo.PageResponse;
import com.xzy.common.entity.vo.Response;
import com.xzy.common.entity.vo.ResponseCode;
import com.xzy.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/indexData")
    public Response indexData(String title) {
        Response response = new Response();
        List<Goods> goodsList = goodsService.getIndexData();
        response.code(ResponseCode.SUCCESS).msg("成功").data(goodsList);
        return response;
    }

    /**
     * 添加商品
     *
     * @param title        商品标题
     * @param descp        商品描述
     * @param originPrice  商品原价(分)
     * @param currentPrice 现价(分)
     * @param image        图片
     * @param detail       详情
     * @param cid          分类id
     * @param stock        库存
     * @return
     */
    @PostMapping("/add")
    public Response add(String title, String descp, Long originPrice, Long currentPrice, MultipartFile image, String detail, String cid, Long stock) throws IOException {
        Response response = new Response();
        int row = goodsService.add(title, descp, originPrice, currentPrice, image, detail, cid, stock);
        if (row > 0) {
            response.code(ResponseCode.SUCCESS).msg("商品入库成功");
        } else {
            response.code(ResponseCode.ERROR).msg("商品入库失败");
        }
        return response;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Response delete(String id) {
        Response response = new Response();
        int row = goodsService.deleteById(id);
        if (row > 0) {
            response.code(ResponseCode.SUCCESS).msg("商品删除成功");
        } else {
            response.code(ResponseCode.ERROR).msg("商品删除失败");
        }
        return response;
    }

    /**
     * 修改
     *
     * @param id           条件
     * @param title        商品标题
     * @param descp        商品描述
     * @param originPrice  商品原价(分)
     * @param currentPrice 商品现价(分)
     * @param detail       商品详情
     * @param status       商品状态 0:待审批  1:以上架  2:已拒绝
     * @param cId          商品分类id
     * @param gmtModify    商品修改日期
     * @return
     */
    @PostMapping("/update")
    public Response update(String id, String title, String descp, Long originPrice, Long currentPrice, String detail, Integer status, String cId, Date gmtModify) {
        Response response = new Response();
        int row = goodsService.update(id, title, descp, originPrice, currentPrice, detail, status, cId, gmtModify);
        if (row > 0) {
            response.code(ResponseCode.SUCCESS).msg("商品修改成功");
        } else {
            response.code(ResponseCode.ERROR).msg("商品修改失败");
        }
        return response;
    }

    /**
     * 查询
     *
     * @param title    标题
     * @param platform 客户端平台(0:管理系统,1:小程序)
     * @param current 当前页码
     * @param size 每页条数
     * @return
     */
    @GetMapping("/list")
    public PageResponse listGoods(String title, Integer platform, Long current, Long size) {
        Long count = goodsService.selectCount(title, platform);
        List<Goods> goodList = goodsService.listGoods(title, platform,current,size);
        PageResponse pageResponse = new PageResponse();
        pageResponse.code(ResponseCode.SUCCESS).msg("成功").data(goodList).count(count);
        return pageResponse;
    }
}

2.4 PageResponse

package com.xzy.common.entity.vo;

import lombok.Data;

/**
 * 封装用于分页的Response
 */
@Data
public class PageResponse {
    private Integer code;//响应码
    private String msg;//响应信息
    private Long count;//总条数
    private Object data;//数据


    public PageResponse code(Integer code) {
        this.code = code;
        return this;
    }


    public PageResponse msg(String msg) {
        this.msg = msg;
        return this;
    }


    public PageResponse count(Long count) {
        this.count = count;
        return this;
    }

    public PageResponse data(Object data) {
        this.data = data;
        return this;
    }
}

3 商品订单(增删改查)

在这里插入图片描述

3.1 GoodsOrderMapper

package com.xzy.goods.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzy.common.entity.GoodsOrder;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface GoodsOrderMapper extends BaseMapper<GoodsOrder> {
}

3.2 GoodsOrderService

package com.xzy.goods.service;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzy.common.entity.Address;
import com.xzy.common.entity.Goods;
import com.xzy.common.entity.GoodsOrder;
import com.xzy.common.entity.Member;
import com.xzy.goods.dao.AddressMapper;
import com.xzy.goods.dao.GoodsMapper;
import com.xzy.goods.dao.GoodsOrderMapper;
import com.xzy.um.dao.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;

@Service
public class GoodsOrderService {
    @Autowired
    private GoodsOrderMapper goodsOrderMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * @param memberId  用户Id
     * @param goodsId   商品Id
     * @param buyNumber 购买数量
     * @return 受影响行数
     */
    @Transient
    public int add(String memberId, String goodsId, Long buyNumber) {
        //1. 查询用户(从Token令牌获取用户id)
        Member member = memberMapper.selectById(memberId);
        //2. 查询用户对应地址
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("u_id", memberId);
        Address address = addressMapper.selectOne(addressQueryWrapper);
        //3. 查询商品信息
        Goods goods = goodsMapper.selectById(goodsId);
        //4. 修改商品库存
        goods.setStock(goods.getStock() - buyNumber);
        goodsMapper.updateById(goods);
        //5. 创建订单
        GoodsOrder goodOrder = new GoodsOrder();
        goodOrder.setUId(member.getId());
        goodOrder.setUName(member.getNickname());
        goodOrder.setGId(goods.getId());
        goodOrder.setGTitle(goods.getTitle());
        goodOrder.setAddrId(address.getId());
        goodOrder.setBuyNumber(buyNumber);
        goodOrder.setCurrentPrice(goods.getCurrentPrice());
        goodOrder.setTotalPrice(goods.getCurrentPrice() * buyNumber);
        goodOrder.setStatus(0);//0表示未支付
        Date currentDate = new Date();
        goodOrder.setGmtModify(currentDate);
        goodOrder.setGmtModify(currentDate);

        return goodsOrderMapper.insert(goodOrder);
    }
}

3.3 GoodsOrderController

package com.xzy.goods.controller;

import com.xzy.common.entity.vo.Response;
import com.xzy.common.entity.vo.ResponseCode;
import com.xzy.common.util.JwtUtils;
import com.xzy.goods.service.GoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order")
@CrossOrigin
public class GoodsOrderController {
    @Autowired
    private GoodsOrderService goodsOrderService;

    @PostMapping("/add")
    public Response add(String goodsId, Long buyNumber, HttpServletRequest request){
        String memberId = JwtUtils.getIdByJwtToken(request);//(从Token令牌获取用户id)
        Response response = new Response();
        int row = goodsOrderService.add(memberId,goodsId,buyNumber);
        if(row > 0){
            response.code(ResponseCode.SUCCESS).msg("创建订单成功");
        }else{
            response.code(ResponseCode.ERROR).msg("创建订单失败");
        }
        return response;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值