zoukankan      html  css  js  c++  java
  • 子父级菜单查询

    1、表结构数据

    2、实体类定义

     1 public class ParentHMClassify {
     2 
     3     private Integer id;
     4 
     5     private String classifyName;
     6 
     7     private Integer parentId;
     8 
     9     private Integer sort;
    10     /**
    11      * 子集合
    12      */
    13     List<ParentHMClassify> childList;
    14 }

    3、查询方法

     1     @Autowired
     2     private HMClassifyMapperCustom classifyMapperCustom;
     3 
     4     @Test
     5     public void query() {
     6         // 查询 db
     7         List<ParentHMClassify> classifyList1 = classifyMapperCustom.getClassifyList1();
     8         // 存放根节点信息
     9         List<ParentHMClassify> rootList = new ArrayList<>();
    10         for (ParentHMClassify parent : classifyList1) {
    11             if (parent.getParentId() == 0) {
    12                 // 添加根节点
    13                 rootList.add(parent);
    14                 // 找到根节点下面的子节点菜单
    15                 findChilds(parent, classifyList1);
    16             }
    17         }
    18         System.out.println("result:" + JSONObject.toJSONString(rootList));
    19     }
    20 
    21     /**
    22      * 递归查询子节点
    23      *
    24      * @param root
    25      * @param list
    26      */
    27     public void findChilds(ParentHMClassify root, List<ParentHMClassify> list) {
    28         System.out.println("查找子节点...");
    29         List<ParentHMClassify> childList = new ArrayList<>();
    30         for (ParentHMClassify menu : list) {
    31             if (root.getId() == menu.getParentId()) {
    32                 childList.add(menu);
    33             }
    34         }
    35         // 子节点不存在,就不需要在遍历子节点中的节点了 直接返回
    36         if (childList.size() == 0) return;
    37         // 设置父节点的子节点列表
    38         root.setChildList(childList);
    39         // 若子节点存在,接着递归调用该方法,查询子节点的子节点
    40         for (ParentHMClassify childs : childList) {
    41             findChilds(childs, list);
    42         }
    43     }

    4、子父级菜单数据结构为:

      1 [
      2     {
      3         "classifyName":"VIP相关模板",
      4         "id":4,
      5         "parentId":0,
      6         "sort":1
      7     },
      8     {
      9         "childList":[
     10             {
     11                 "childList":[
     12                     {
     13                         "classifyName":"材料遗失声明模板",
     14                         "id":9,
     15                         "parentId":6,
     16                         "sort":1
     17                     },
     18                     {
     19                         "classifyName":"345345",
     20                         "id":36,
     21                         "parentId":6,
     22                         "sort":2
     23                     }
     24                 ],
     25                 "classifyName":"档案科",
     26                 "id":6,
     27                 "parentId":5,
     28                 "sort":1
     29             },
     30             {
     31                 "childList":[
     32                     {
     33                         "childList":[
     34                             {
     35                                 "classifyName":"公正委托书模板",
     36                                 "id":13,
     37                                 "parentId":10,
     38                                 "sort":1
     39                             },
     40                             {
     41                                 "classifyName":"交房委托书",
     42                                 "id":14,
     43                                 "parentId":10,
     44                                 "sort":2
     45                             },
     46                             {
     47                                 "classifyName":"未到场补充协议",
     48                                 "id":15,
     49                                 "parentId":10,
     50                                 "sort":3
     51                             },
     52                             {
     53                                 "classifyName":"未到场授权委托书",
     54                                 "id":16,
     55                                 "parentId":10,
     56                                 "sort":4
     57                             }
     58                         ],
     59                         "classifyName":"买卖材料模板",
     60                         "id":10,
     61                         "parentId":7,
     62                         "sort":1
     63                     },
     64                     {
     65                         "classifyName":"租赁材料模板",
     66                         "id":11,
     67                         "parentId":7,
     68                         "sort":2
     69                     }
     70                 ],
     71                 "classifyName":"签约部",
     72                 "id":7,
     73                 "parentId":5,
     74                 "sort":2
     75             },
     76             {
     77                 "childList":[
     78                     {
     79                         "classifyName":"收入证明模板",
     80                         "id":12,
     81                         "parentId":8,
     82                         "sort":1
     83                     }
     84                 ],
     85                 "classifyName":"贷款部",
     86                 "id":8,
     87                 "parentId":5,
     88                 "sort":3
     89             }
     90         ],
     91         "classifyName":"交易服务中心",
     92         "id":5,
     93         "parentId":0,
     94         "sort":2
     95     },
     96     {
     97         "childList":[
     98             {
     99                 "childList":[
    100                     {
    101                         "childList":[
    102                             {
    103                                 "classifyName":"四级菜单",
    104                                 "id":41,
    105                                 "parentId":40,
    106                                 "sort":1
    107                             }
    108                         ],
    109                         "classifyName":"三级菜单",
    110                         "id":40,
    111                         "parentId":39,
    112                         "sort":2
    113                     }
    114                 ],
    115                 "classifyName":"二级菜单",
    116                 "id":39,
    117                 "parentId":38,
    118                 "sort":1
    119             },
    120             {
    121                 "childList":[
    122                     {
    123                         "childList":[
    124                             {
    125                                 "classifyName":"四级菜单-1",
    126                                 "id":44,
    127                                 "parentId":43,
    128                                 "sort":1
    129                             }
    130                         ],
    131                         "classifyName":"三级菜单-1",
    132                         "id":43,
    133                         "parentId":42,
    134                         "sort":1
    135                     }
    136                 ],
    137                 "classifyName":"二级菜单-1",
    138                 "id":42,
    139                 "parentId":38,
    140                 "sort":2
    141             }
    142         ],
    143         "classifyName":"一级菜单",
    144         "id":38,
    145         "parentId":0,
    146         "sort":3
    147     }
    148 ]

    5、总结

    先查询所有菜单集合数据,然后遍历菜单集合获取根节点集合,然后调用查询子节点方法并在子节点方法中进行递归查询。如果当前节点没有子节点,直接return 不需要在进行递归查询。相反如果当前节点有子节点则一直进行递归查询。

  • 相关阅读:
    vue项目页面空白
    vue使用nprogress页面加载进度条
    如何让一个div居于页面正中间
    Django项目使用七牛云存储图片
    Celery ValueError: not enough values to unpack (expected 3, got 0)的解决方案
    网站ico那点事儿
    SpringBoot2.0 + SpringCloud Eureka搭建高可用注册中心(Eureka之三)
    SpringBoot2.0+SpringCloud Eureka构建服务治理(Eureka之一)
    SpringBoot2.0+SpringCloud Eureka搭建高可用注册中心(Eureka之二)
    Synchronized重入锁
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/15476900.html
Copyright © 2011-2022 走看看