18、【 商品管理模块开发】——前台商品详情、列表、搜索、动态排序功能开发

简介: 1、接口编写:在portal包下新建ProductController类:image.pngimage.png1、前台查询商品详情接口:*Controller: //前台查询商品详情接口 @RequestMapping("detail.

1、接口编写:

在portal包下新建ProductController类:

img_8d31c185d753a836caf20d456e124d19.png
image.png

img_3a3ade8f55058091a9b121a63c0268f6.png
image.png

1、前台查询商品详情接口:

*Controller:

    //前台查询商品详情接口
    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse<ProductDetailVo> detail(Integer productId){
        return iProductService.getProductDetail(productId);
    }

*Service:

    //前台商品详情查询
    ServerResponse<ProductDetailVo> getProductDetail(Integer productId);

*ServiceImpl:

    //前台商品详情查询
    public ServerResponse<ProductDetailVo> getProductDetail(Integer productId){
        if(productId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Product product=productMapper.selectByPrimaryKey(productId);
        if(product==null){
            return ServerResponse.createByErrorMessage("商品已下架或者删除");
        }
        if(product.getStatus() != Const.ProductStatusEnum.ON_SALE.getCode()){
            return ServerResponse.createByErrorMessage("商品已下架或者删除");
        }
        ProductDetailVo productDetailVo=assembleProductDetailVo(product);
        return  ServerResponse.createBySuccess(productDetailVo);

    }

其中ProductDetailVo productDetailVo=assembleProductDetailVo(product);这一行代码在
15、【 商品管理模块开发】——后台获取商品详情功能开发及PropertiesUtil配置工具,DateTimeUtil时间处理工具开发中有讲,不清楚可以到该片文章中查看。
在上面*ServiceImpl使用的selectByPrimaryKey方法是使用逆向工程生成的,故不展示。

2、前台查询商品列表接口

*Controller:

  //前台查询商品列表接口
    @RequestMapping("list.do")
    @ResponseBody
    //商品详情列表分页
    public ServerResponse<PageInfo> list(@RequestParam(value = "keyword",required = false) String keyword,
            @RequestParam(value = "categoryId",required = false)Integer categoryId,
            @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
            @RequestParam(value = "orderBy",defaultValue = "") String orderBy){

        return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
    }

*Service:

//前台商品分页(根据关键字搜索)
     ServerResponse<PageInfo> getProductByKeywordCategory(String keyword,Integer categoryId,int pageNum,int pageSize,String orderBy);

*ServiceImpl:

 //前台商品分页(根据关键字搜索)
    public ServerResponse<PageInfo> getProductByKeywordCategory(String keyword,Integer categoryId,int pageNum,int pageSize,String orderBy){
        if(StringUtils.isBlank(keyword) && categoryId ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }

        List<Integer> categoryIdList=new ArrayList<>();
        if(categoryId !=null){
            Category category=categoryMapper.selectByPrimaryKey(categoryId);

            if(category == null &&StringUtils.isBlank(keyword)){
                //没有该分类,并且还没有关键字,这个时候返回一个空的集合,不能返回报错
                PageHelper.startPage(pageNum,pageSize);
                List<ProductDetailVo> productDetailVoList =Lists.newArrayList();
                PageInfo pageInfo =new PageInfo(productDetailVoList);
                return ServerResponse.createBySuccess(pageInfo);
            }
            categoryIdList = iCategoryService.selectCategoryAndChildrenById(category.getId()).getData();
        }

        if(StringUtils.isNotBlank(keyword)){
            keyword =new StringBuilder().append("%").append(keyword).append("%").toString();
        }

        PageHelper.startPage(pageNum,pageSize);
        //排序处理
        if(StringUtils.isNotBlank(orderBy)){
            if(Const.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
                String[] orderByArray=orderBy.split("_");
                PageHelper.orderBy(orderByArray[0]+" "+orderByArray[1]);
            }
        }
        List<Product> productList=productMapper.selectByNameAndCategoryIds(StringUtils.isBlank(keyword)?null:keyword,categoryIdList.size()==0?null:categoryIdList);

        List<ProductListVo> productListVoList=Lists.newArrayList();
        for(Product product : productList){
            ProductListVo productListVo=assembleProductListVo(product);
            productListVoList.add(productListVo);
        }

        //开始分页
        PageInfo pageInfo=new PageInfo(productList);

        pageInfo.setList(productListVoList);

        return ServerResponse.createBySuccess(pageInfo);

    }

上面我们用到了我们自己定义的两个方法,下面将对应代码展示一下
selectCategoryAndChildrenById:

 public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){

        //调用递归算法
        Set<Category> categorySet= Sets.newHashSet();
        finChildCategory(categorySet,categoryId);


        List<Integer> categoryIdList= Lists.newArrayList();
        if(categoryId !=null){
            for(Category categoryItem : categorySet){
                categoryIdList.add(categoryItem.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }

selectByNameAndCategoryIds:

<!--根据商品名字和Id查询商品-->
  <select id="selectByNameAndCategoryIds" resultMap="BaseResultMap" parameterType="map">
    select
    <include refid="Base_Column_List"/>
    from mmall_product
    where status=1
    <if test="productName != null">
      and name like #{productName}
    </if>
    <if test="categoryList != null">
      and category_id in
      <foreach item="item" index="index" open="(" separator="," close=")" collection="categoryList">
        #{item}
      </foreach>
    </if>
  </select>

2、接口测试:

1、前台商品查询:
img_76318cf90f460256ceef0606cc76f23f.png
image.png
2、前台商品列表查询:

其中orderBy=后面对应的是我们需要排序的方式。

img_68f1962a8bb1d370cb63e5eaa76b9b07.png

img_2cdba84231dec109a3d9f253c02b85f6.png
image.png

相关文章
|
4月前
|
安全 API PHP
PHP 8.2 新特性解析:更简洁、更强大的现代PHP
PHP 8.2 新特性解析:更简洁、更强大的现代PHP
301 115
|
人工智能 物联网 机器人
『GitHub项目圈选17』推荐5款本周 火火火 的AI开源项目
『GitHub项目圈选17』推荐5款本周 火火火 的AI开源项目
2181 1
|
缓存 NoSQL 中间件
Redis,分布式缓存演化之路
本文介绍了基于Redis的分布式缓存演化,探讨了分布式锁和缓存一致性问题及其解决方案。首先分析了本地缓存和分布式缓存的区别与优劣,接着深入讲解了分布式远程缓存带来的并发、缓存失效(穿透、雪崩、击穿)等问题及应对策略。文章还详细描述了如何使用Redis实现分布式锁,确保高并发场景下的数据一致性和系统稳定性。最后,通过双写模式和失效模式讨论了缓存一致性问题,并提出了多种解决方案,如引入Canal中间件等。希望这些内容能为读者在设计分布式缓存系统时提供有价值的参考。感谢您的阅读!
431 6
Redis,分布式缓存演化之路
|
存储 弹性计算 网络协议
深度对比阿里云服务器ECS通用型g7、g7a、g8i、g8y、g8ise和g8a性能对比
阿里云通用型ECS云服务器(g7、g7a、g8i、g8y、g8ise和g8a)具有1:4的CPU内存比,适合多种应用场景。配置从2核8G到128核512G不等,基于第三代神龙架构或CIPU架构,采用Intel、AMD及自研倚天710处理器,主频在2.55 GHz至3.7 GHz之间。支持IPv4/IPv6,具备高网络收发包PPS能力,适用于企业级应用、数据库、Web服务、AI训练、音视频处理等场景。I/O优化实例仅支持ESSD云盘,确保高性能存储。
|
NoSQL Ubuntu Java
在Ubuntu下安装Redis
【1月更文挑战第6天】在Ubuntu下安装Redis
994 103
|
安全 关系型数据库 MySQL
【Python】已解决:pymysql.err.OperationalError:(2003 “Can’t connect to MySQL server on ‘localhost’ ([WinEr
【Python】已解决:pymysql.err.OperationalError:(2003 “Can’t connect to MySQL server on ‘localhost’ ([WinEr
2425 1
|
中间件 数据挖掘 API
ERP系统的系统集成与接口管理:实现高效协同
【7月更文挑战第29天】 ERP系统的系统集成与接口管理:实现高效协同
1297 0
|
网络协议 网络架构
【计网·湖科大·思科】实验六 IP数据报的发送和转发流程、默认路由和特定主机路由
【计网·湖科大·思科】实验六 IP数据报的发送和转发流程、默认路由和特定主机路由
532 0
|
数据采集
BurpSuite2021 -- 爬虫使用
BurpSuite2021 -- 爬虫使用
359 0
|
设计模式 缓存 Java
23种设计模式,代理模式的概念优缺点以及JAVA代码举例
4月更文挑战第7天】代理模式是一种常用的软件设计模式,它为其他对象提供一种代理以控制对这个对象的访问。这种模式创建具有原始对象相同接口的对象,从而使代理对象在访问者和目标对象之间作为一个中介。
244 0

热门文章

最新文章