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
相关文章
|
11月前
|
SQL 小程序 JavaScript
【易售小程序项目】小程序首页(展示商品、商品搜索、商品分类搜索)【后端基于若依管理系统开发】
【易售小程序项目】小程序首页(展示商品、商品搜索、商品分类搜索)【后端基于若依管理系统开发】
52 0
|
11月前
|
SQL 小程序 前端开发
【易售小程序项目】商品详情展示+评论、评论展示、评论点赞+商品收藏【后端基于若依管理系统开发】
【易售小程序项目】商品详情展示+评论、评论展示、评论点赞+商品收藏【后端基于若依管理系统开发】
104 0
|
26天前
|
前端开发 数据库
SpringBoot+Vue实现商品不能重复加入购物车、购物车中展示商品的信息、删除商品重点提示等操作。如何点击图片实现图片放大
这篇文章介绍了如何在SpringBoot+Vue框架下实现购物车功能,包括防止商品重复加入、展示商品信息、删除商品时的提示,以及点击图片放大的前端实现。
SpringBoot+Vue实现商品不能重复加入购物车、购物车中展示商品的信息、删除商品重点提示等操作。如何点击图片实现图片放大
|
4月前
|
JavaScript
基础购物车功能
基础购物车功能
|
11月前
|
开发者
【 uniapp - 黑马优购 | 购物车页面(1)】如何创建购物车编译模式、 商品列表区域实现
【 uniapp - 黑马优购 | 购物车页面(1)】如何创建购物车编译模式、 商品列表区域实现
207 0
|
4月前
|
小程序 IDE API
社区每周丨小程序基础库更新至 2.8.21及小程序商品新增商品营销功能(9.11-9.15)
社区每周丨小程序基础库更新至 2.8.21及小程序商品新增商品营销功能(9.11-9.15)
168 11
|
10月前
|
JSON 缓存 API
如何使用商品详情API接口来获取想要的商品数据?
在这篇文章中,我将详细介绍如何使用商品详情API接口来获取想要的商品数据。首先,我们需要了解API接口的基本概念和使用方法。然后,我们将探讨如何通过API接口获取商品数据,并给出示例代码。最后,我们将讨论如何优化API接口的使用,以提高获取商品数据的效率。
|
11月前
|
小程序 前端开发
【易售小程序项目】修改“我的”界面前端实现;查看、重新编辑、下架自己发布的商品【后端基于若依管理系统开发】
【易售小程序项目】修改“我的”界面前端实现;查看、重新编辑、下架自己发布的商品【后端基于若依管理系统开发】
78 0
电商API分享:获得淘宝商品评论、商品评论问答列表 分页显示 代码展示
电商API分享:获得淘宝商品评论、商品评论问答列表 分页显示 代码展示
|
JSON 前端开发 Java
基于Springboot外卖系统14:菜品新增模块+多个数据表操作+文件上传下载复用
后台系统中可以管理菜品信息,通过新增功能来添加一个新的菜品,在添加菜品时需要选择当前菜品所属的菜品分类,并且需要上传菜品图片,在移动端会按照菜品分类来展示对应的菜品信息 。
243 0