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;
}
AI 代码解读

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

   @Autowired
    private IUserService iUserService;

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IFileService iFileService;
AI 代码解读

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("当前登录者不是管理员,无权限操作");
        }

    }
AI 代码解读

*Service:

   //修改或者添加商品方法
    ServerResponse saveOrUpdateProduct(Product product);
AI 代码解读

*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("新增或者更新产品参数不正确");
    }
AI 代码解读

其中在修改商品的时候使用的是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("当前登录者不是管理员,无权限操作");
        }
    }
AI 代码解读

*Service:

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

*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("修改商品销售状态失败");
    }
AI 代码解读

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
相关文章
IDEA创建maven项目过慢,一直卡在resolving dependencies...的解决办法
作为一个从事 Java 开发的程序员,每天离不开ide的帮助。一开始学习java的时候基本都是使用eclipse进行开发, 后来接触了idea,发现是真的香,比eclipse好用太多了,能够大大提升开发的效率。
4871 0
IDEA创建maven项目过慢,一直卡在resolving dependencies...的解决办法
深度学习在资源利用率优化中的应用:让服务器更聪明
深度学习在资源利用率优化中的应用:让服务器更聪明
226 6
|
9月前
|
HashMap之链表转红黑树(树化 )-treefyBin方法源码解读(所有涉及到的方法均有详细解读,欢迎指正)
本文详细解析了Java HashMap中链表转红黑树的机制,包括树化条件(链表长度达8且数组长度≥64)及转换流程,确保高效处理大量数据。
327 1
|
9月前
|
基于Spring Boot REST API设计指南
【10月更文挑战第11天】 在构建现代Web应用程序时,RESTful API已成为一种标准,使得不同的应用程序能够通过HTTP协议进行通信,实现资源的创建、读取、更新和删除等操作。Spring Boot作为一个功能强大的框架,能够轻松创建RESTful API。本文将详细介绍如何在Spring Boot中设计和实现高质量的RESTful API。
315 61
AI Safeguard联合 CMU,斯坦福提出端侧多模态小模型
随着人工智能的快速发展,多模态大模型(MLLMs)在计算机视觉、自然语言处理和多模态任务中扮演着重要角色。
164 0
大数据与教育:学生表现分析的工具
【10月更文挑战第31天】在数字化时代,大数据成为改善教育质量的重要工具。本文探讨了大数据在学生表现分析中的应用,介绍学习管理系统、智能评估系统、情感分析技术和学习路径优化等工具,帮助教育者更好地理解学生需求,制定个性化教学策略,提升教学效果。尽管面临数据隐私等挑战,大数据仍为教育创新带来巨大机遇。
连接池技术:简单而强大的加速数据库访问方法
连接池技术是一种简单而强大的方法,可用于加速数据库访问。在传统的数据库访问过程中,每次与数据库建立连接和关闭连接都需要耗费大量的时间和资源。而连接池技术通过事先建立一组可重复使用的数据库连接,有效地减少了连接和关闭连接的开销。本文将深入探讨连接池技术的工作原理和优势,以及如何正确配置和使用连接池来提高应用程序的性能。无论你是开发人员还是系统管理员,通过了解连接池技术,你将能够更好地利用数据库资源,使系统更加稳定和高效。
855 0
阿里云如何使用DNS解析域名
阿里云DNS是一个域名解析服务,你可以使用阿里云的DNS服务来管理和解析域名。
1720 11
AI 助理能为我做什么?
阿里云AI助理融合了大模型能力,聚焦于开发者在阿里云平台上的关键需求,提供包括云产品咨询、权益活动推荐、下单购买引导、云资源查询与诊断等服务,助力开发者快速解决问题,提高业务效率,让云上开发之旅更加顺畅。
750 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问