当前位置: 首页 > news >正文

13、【分类模块管理】——查询节点和递归查找功能开发

该接口是我们查询出所有的结点个子节点,在查询的时候利用父节点parentId属性来进行递归查询,当子节点不再有的时候,我们就结束递归查询,然后将查询到的结果全部返回给客户端。关于在首先我们判断登陆者是否是管理员,我们在10、【分类模块管理】——添加分类接口开发有说明。
controller

 //查询当前节点和子节点
    @RequestMapping("get_children_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue ="0" )Integer categoryId){
        //验证用户是否登录
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请先登录");
        }
        //校验是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){

            //查询当前节点的Id和递归子节点的Id
            return  iCategoryService.selectCategoryAndChildrenById(categoryId);

        }else{
            return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
        }
    }

server:

 //递归查询查询该节点和子节点的Id
    ServerResponse selectCategoryAndChildrenById(Integer categoryId);

serverImpl:

 /**
     * 递归查询查询该节点和子节点的Id
     * @param categoryId
     * @return
     */
    public ServerResponse 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);
    }

    //递归算法算出子节点
    private Set<Category> finChildCategory(Set<Category> categorySet,Integer categoryId){
        Category category=categoryMapper.selectByPrimaryKey(categoryId);
        if(category !=null){
            categorySet.add(category);
        }
        //查找子节点,递归算法一定要有一个退出条件,当子节点不再有的时候,就跳出递归
        List<Category> categoryList=categoryMapper.selectCategoryChildrenByParentId(categoryId);
        for(Category categoryItem:categoryList){
            finChildCategory(categorySet,categoryItem.getId());
        }
        return categorySet;
    }

Mapper:

//    通过父结点查询同级字节点的信息
    List<Category> selectCategoryChildrenByParentId(Integer parentId);

Mapper.xml:

  <select id="selectCategoryChildrenByParentId" resultMap="BaseResultMap" parameterType="int">
  select
  <include refid="Base_Column_List"/>
    from  mmall_category
    where parent_id=#{parentId}

  </select>

接口测试:


img_db8c2cd5dc7af9cee07bc89ee30455a9.png
image.png

相关文章:

  • 初识Mpvue
  • mongodb基础(1)
  • 不学无数——个人搭建的博客已经好了,欢迎访问
  • (五)IO流之ByteArrayInput/OutputStream
  • Java 之 Map 的键,值多重排序问题解决方案
  • 西安高校加紧人工智能学科布局
  • CentOS搭建samba文件共享传输服务
  • 容器服务kubernetes弹性伸缩高级用法
  • TERMIOS详解【转】
  • 求连续数字的和------------------------------用while的算法思想
  • Zabbix3.0基础教程之二:item、trigger、action、graph配置
  • BZOJ2216: [Poi2011]Lightning Conductor(DP 决策单调性)
  • 递增链表的插入
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • Linux 环境变量的配置解决(-bash: jps: command not found)问题
  • 【Leetcode】104. 二叉树的最大深度
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 2017-09-12 前端日报
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Angular 响应式表单之下拉框
  • CSS实用技巧干货
  • css系列之关于字体的事
  • leetcode-27. Remove Element
  • linux安装openssl、swoole等扩展的具体步骤
  • Python - 闭包Closure
  • Python实现BT种子转化为磁力链接【实战】
  • vue 配置sass、scss全局变量
  • vue-loader 源码解析系列之 selector
  • 编写符合Python风格的对象
  • 算法---两个栈实现一个队列
  • 我看到的前端
  • 优秀架构师必须掌握的架构思维
  • 由插件封装引出的一丢丢思考
  • 原生 js 实现移动端 Touch 滑动反弹
  • 正则表达式
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • kubernetes资源对象--ingress
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • ​ssh免密码登录设置及问题总结
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​批处理文件中的errorlevel用法
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • (1)(1.13) SiK无线电高级配置(五)
  • (1)SpringCloud 整合Python
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (Java数据结构)ArrayList
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (六)Hibernate的二级缓存
  • (转载)深入super,看Python如何解决钻石继承难题
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .Net CF下精确的计时器
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...