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

相关文章
|
人工智能 物联网 机器人
『GitHub项目圈选17』推荐5款本周 火火火 的AI开源项目
『GitHub项目圈选17』推荐5款本周 火火火 的AI开源项目
2790 1
|
存储 XML NoSQL
提高代码质量,避免内存泄漏:深入探索Valgrind工具
提高代码质量,避免内存泄漏:深入探索Valgrind工具
|
11月前
|
人工智能 缓存 Shell
Qoder 发布首个自进化的智能体:看 Quest 如何重构了 Quest
Quest 是一款实现真正自主编程的AI代理,通过Agentic上下文管理、精简Bash工具链、Spec→Coding→Verify闭环及动态Skills系统,让AI端到端交付可运行产物。它不依赖人工调试,支持自进化与多模型协同,专为未来大模型而设计。(239字)
Qoder 发布首个自进化的智能体:看 Quest 如何重构了 Quest
|
存储 数据挖掘 BI
API数据源:轻松接入各类业务系统数据
在数字化转型中,企业面临多样化的数据需求。Quick BI推出API数据源功能,支持广泛的数据接入,包括实时天气、电商交易及内部业务数据,极大丰富了可分析数据范围。该功能提供灵活的连接方式(抽取和直连模式)、多元授权机制(基础认证、前置请求)和自动化数据解析,降低了操作门槛,提升了配置效率。通过动态Token获取等最佳实践,确保数据安全与实时性,满足企业具体业务需求。了解更多,请访问Quick BI官方文档或瓴羊官网。
1098 77
|
12月前
|
人工智能 JSON API
从MCP到PTC Anthropic回归Code Execution路线,AiPy的范式被再次验证
Anthropic从MCP到Programmatic Tool Calling的演进,实则是对“上下文爆炸”问题的修正,仍属“上下文工程”范畴。而AiPy早于Claude Code提出Python-use范式,主张“Code is Agent”,通过代码直接交互环境,实现“万物互联、万物编程”。相较MCP/PTC依赖预定义工具,Python-use更具扩展性与灵活性,兼容API、包调用及本地执行,早在2024年8月即实现命令级代码执行,领先Skills两月。CodeAct理念与其高度一致,但本质仍是工具注册模式。Python-use范式直击Agent核心:大模型与环境数据的无限连接能力
|
JavaScript Unix Linux
nvm与node.js的安装指南
通过以上步骤,你可以在各种操作系统上成功安装NVM和Node.js,从而在不同的项目中灵活切换Node.js版本。这种灵活性对于管理不同项目的环境依赖而言是非常重要的。
4399 11
|
缓存 NoSQL 中间件
Redis,分布式缓存演化之路
本文介绍了基于Redis的分布式缓存演化,探讨了分布式锁和缓存一致性问题及其解决方案。首先分析了本地缓存和分布式缓存的区别与优劣,接着深入讲解了分布式远程缓存带来的并发、缓存失效(穿透、雪崩、击穿)等问题及应对策略。文章还详细描述了如何使用Redis实现分布式锁,确保高并发场景下的数据一致性和系统稳定性。最后,通过双写模式和失效模式讨论了缓存一致性问题,并提出了多种解决方案,如引入Canal中间件等。希望这些内容能为读者在设计分布式缓存系统时提供有价值的参考。感谢您的阅读!
677 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云盘,确保高性能存储。
|
IDE 开发工具 iOS开发
Python的IDE选择
【7月更文挑战第4天】Python的IDE选择
856 59