无限级菜单的数据组装

无限级菜单的数据组装

Scroll Down

无限级菜单的数据组装方式

对于无限级的菜单设计,我们一般都会设计在一张表里面,里面有他父id与子id关联如
image.png
我们需要的数据格式为:父分类下面对应着多个子分类的list

{
    "status": 200,
    "msg": "OK",
    "data": [
        {
            "subCatList": [
                {
                    "subCatList": [],
                    "name": "蒸蛋糕",
                    "id": 37,
                    "father_id": 11,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "软面包",
                    "id": 38,
                    "father_id": 11,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "脱水蛋糕",
                    "id": 39,
                    "father_id": 11,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "马卡龙",
                    "id": 40,
                    "father_id": 11,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "甜甜圈",
                    "id": 41,
                    "father_id": 11,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "三明治",
                    "id": 42,
                    "father_id": 11,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "铜锣烧",
                    "id": 43,
                    "father_id": 11,
                    "type": 3
                }
            ],
            "name": "蛋糕",
            "id": 11,
            "father_id": 1,
            "type": 2
        },
        {
            "subCatList": [
                {
                    "subCatList": [],
                    "name": "肉松饼",
                    "id": 44,
                    "father_id": 12,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "华夫饼",
                    "id": 45,
                    "father_id": 12,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "沙琪玛",
                    "id": 46,
                    "father_id": 12,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "鸡蛋卷",
                    "id": 47,
                    "father_id": 12,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "蛋饼",
                    "id": 48,
                    "father_id": 12,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "凤梨酥",
                    "id": 49,
                    "father_id": 12,
                    "type": 3
                },
                {
                    "subCatList": [],
                    "name": "手撕面包",
                    "id": 50,
                    "father_id": 12,
                    "type": 3
                }
            ],
            "name": "点心",
            "id": 12,
            "father_id": 1,
            "type": 2
        }
    ]
}

1.递归查询的方式实现

   public List<Long> getDeptChildren(List<Dept> deptList) {
        List<Long> list = new ArrayList<>();
        deptList.forEach(dept -> {
                    
                    if (dept!=null && dept.getEnabled()){
                        //查询有父ID的list
                        List<Dept> depts = deptService.findByPid(dept.getId());
                        if(deptList.size() != 0){
                            //递归调用添加到父list中
                            list.addAll(getDeptChildren(depts));
                        }
                        //没有即 他就上父级元素
                        list.add(dept.getId());
                    }
                }
        );
        return list;
    }

这种方式实现比较简单,但是会去进行多次查询,影响性能

2.一次查询根据父ID组装

    List<Map<String,Object>> result = redisUtils.getList(RedisEnum.CHILD_DIRECTORY.getKey()+fatherId);
        if(CollectionUtils.isEmpty(result)){
            List<Map<String,Object>> catsByFatherId = carouselMapper.getSecondCatsByFatherId(fatherId);
            //组装数据
            for (Map<String, Object> carts : catsByFatherId) {
                List<Map<String,Object>> pocketList= new ArrayList<Map<String,Object>>();
                for (Map<String, Object> cartToAdd : catsByFatherId) {
                    if(cartToAdd.get("father_id").equals(carts.get("id"))){
                        pocketList.add(cartToAdd);
                    }
                }
                carts.put("subCatList",pocketList);
                if (fatherId.equals(carts.get("father_id"))){
                    result.add(carts);
                }
            }
            redisUtils.setList(RedisEnum.CHILD_DIRECTORY.getKey()+fatherId,result);
        }

祝君,好梦!