14、【 商品管理模块开发】——后台商品新增保存、更新、上下架功能开发

简介: 新建ProductManageController类image.png在Controller上添加@Controller 和@RequestMapping("/manage/product")这两个注解。

新建ProductManageController

img_974cfae8c50c3db8d62e6d78a969b1ac.png
image.png

Controller上添加 @Controller@RequestMapping("/manage/product")这两个注解。

@Controller
@RequestMapping("/manage/product")
public class ProductManageController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IFileService iFileService;
}

根据后文中要用的到Service自动注入:

   @Autowired
    private IUserService iUserService;

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IFileService iFileService;

1、修改或者添加商品(原来商品存在则是修改,不存在就是添加)

*Controller:

  //保存商品信息
    @RequestMapping("product_save.do")
    @ResponseBody
    public ServerResponse productSave(HttpSession session, Product product){
        User user=(User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,请先登录");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //增加商品的逻辑方法
            System.out.println("执行。。。");
            return iProductService.saveOrUpdateProduct(product);
        }else {
            return ServerResponse.createByErrorMessage("当前登录者不是管理员,无权限操作");
        }

    }

*Service:

   //修改或者添加商品方法
    ServerResponse saveOrUpdateProduct(Product product);

*ServiceImpl:

 //修改或者添加商品方法
    public ServerResponse saveOrUpdateProduct(Product product){
        if(product!=null){

            if(StringUtils.isNotBlank(product.getSubImages())){
                String[] subImagesArray=product.getSubImages().split(",");
                if(subImagesArray.length>0){
                     product.setMainImage(subImagesArray[0]);
                }

            }
            if(product.getId()!=null){
                //代表是修改商品
                product.setUpdateTime(new Date());
                int rowCount =productMapper.updateByPrimaryKeySelective(product);
                if(rowCount>0){
                    return ServerResponse.createBySuccess("修改商品信息成功");
                }
                    return ServerResponse.createBySuccess("修改商品信息失败");

            }else{
//                System.out.println("开始插入新增商品");
                //代表是新增商品
                product.setCreateTime(new Date());
                product.setUpdateTime(new Date());
                int rowCount=productMapper.insertSelective(product);
//                System.out.println("执行完新增商品");
                if(rowCount>0){
//                    System.out.println("插入新增商品成功");
                    return ServerResponse.createBySuccessMessage("新增商品成功");
                }
                   return ServerResponse.createByErrorMessage("新增商品失败");
            }

        }
        return ServerResponse.createByErrorMessage("新增或者更新产品参数不正确");
    }

其中在修改商品的时候使用的是updateByPrimaryKeySelective方法;
在添加商品的时候使用的是insertSelective方法。这两个方法都是逆向工程生成的,故而不将代码贴出来~

2、上下架功能

*Controller:

    //更新商品的在售状态
    @RequestMapping("set_sale_status.do")
    @ResponseBody
    public ServerResponse setSaleStatus(HttpSession session, Integer productId,Integer status){
        User user=(User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,请先登录");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //增加商品的逻辑方法
            return iProductService.setSaleStatus(productId,status);
        }else {
            return ServerResponse.createByErrorMessage("当前登录者不是管理员,无权限操作");
        }
    }

*Service:

    //更新商品的在售状态
    ServerResponse<String> setSaleStatus(Integer productId,Integer status);

*ServiceImpl:

 //更新商品的在售状态
    public ServerResponse<String> setSaleStatus(Integer productId,Integer status){
        if(productId==null||status==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }

        Product product=new Product();
        product.setId(productId);
        product.setStatus(status);

        int rowCount=productMapper.updateByPrimaryKeySelective(product);
        if(rowCount>0){
            return ServerResponse.createBySuccessMessage("修改商品销售状态成功");
        }
        return ServerResponse.createByErrorMessage("修改商品销售状态失败");
    }

updateByPrimaryKeySelective方法也是使用逆向工程生成的方法,故而不作多展示~

3、接口测试

1、添加商品


img_a311bcc337f99b6848861ba78989719e.png
image.png

2、修改商品信息


img_b74e0678c58eafa8f5515114f59e9449.png
image.png

3、产品下架
img_689089d1223c46acbbe6cfcfe952215d.png
image.png

4、产品上架


img_14231236e3e9ebb877de5f0af650ce3a.png
image.png
相关文章
|
Linux 数据安全/隐私保护 Windows
音视频开发:大华摄像头配置RTSP与RTMP地址访问视频画面
音视频开发:大华摄像头配置RTSP与RTMP地址访问视频画面
4748 0
音视频开发:大华摄像头配置RTSP与RTMP地址访问视频画面
|
9月前
|
机器学习/深度学习 运维 资源调度
深度学习在资源利用率优化中的应用:让服务器更聪明
深度学习在资源利用率优化中的应用:让服务器更聪明
387 6
|
缓存 Java API
基于Spring Boot REST API设计指南
【10月更文挑战第11天】 在构建现代Web应用程序时,RESTful API已成为一种标准,使得不同的应用程序能够通过HTTP协议进行通信,实现资源的创建、读取、更新和删除等操作。Spring Boot作为一个功能强大的框架,能够轻松创建RESTful API。本文将详细介绍如何在Spring Boot中设计和实现高质量的RESTful API。
426 61
|
前端开发 测试技术 数据安全/隐私保护
软件测试 —— 测试用例设计报告
软件测试 —— 测试用例设计报告
412 1
|
机器学习/深度学习 搜索推荐 大数据
大数据与教育:学生表现分析的工具
【10月更文挑战第31天】在数字化时代,大数据成为改善教育质量的重要工具。本文探讨了大数据在学生表现分析中的应用,介绍学习管理系统、智能评估系统、情感分析技术和学习路径优化等工具,帮助教育者更好地理解学生需求,制定个性化教学策略,提升教学效果。尽管面临数据隐私等挑战,大数据仍为教育创新带来巨大机遇。
|
11月前
|
人工智能 自然语言处理 物联网
AI Safeguard联合 CMU,斯坦福提出端侧多模态小模型
随着人工智能的快速发展,多模态大模型(MLLMs)在计算机视觉、自然语言处理和多模态任务中扮演着重要角色。
238 0
|
存储 Java
HashMap之链表转红黑树(树化 )-treefyBin方法源码解读(所有涉及到的方法均有详细解读,欢迎指正)
本文详细解析了Java HashMap中链表转红黑树的机制,包括树化条件(链表长度达8且数组长度≥64)及转换流程,确保高效处理大量数据。
578 1
|
Java 关系型数据库 MySQL
连接池技术:简单而强大的加速数据库访问方法
连接池技术是一种简单而强大的方法,可用于加速数据库访问。在传统的数据库访问过程中,每次与数据库建立连接和关闭连接都需要耗费大量的时间和资源。而连接池技术通过事先建立一组可重复使用的数据库连接,有效地减少了连接和关闭连接的开销。本文将深入探讨连接池技术的工作原理和优势,以及如何正确配置和使用连接池来提高应用程序的性能。无论你是开发人员还是系统管理员,通过了解连接池技术,你将能够更好地利用数据库资源,使系统更加稳定和高效。
1016 0
|
数据安全/隐私保护 Android开发 iOS开发
如何设置APN
设置APN(接入点名称,Access Point Name)是连接互联网或特定网络服务(如彩信、移动数据等)时,设备需要配置的一个重要参数。不同的手机操作系统(如Android、iOS)和不同的移动网络提供商(如中国移动、中国联通、中国电信等)可能有不同的设置步骤。以下是一些基本的步骤和注意事项,用于设置APN:
|
JavaScript 前端开发 开发者
Web Components详解-Shadow DOM基础
Web Components详解-Shadow DOM基础
557 1