Java数据封装成树形结构,多级

话不多说直接上菜

1,实体类

/**
 * @ClassName SysTagConf
 * @Description TODO
 * @Author shenWB
 * @Date 2019/5/31 10:12
 * @Version 1.0
 **/@Data
public class SysTagConf implements java.io.Serializable{
    private String rowGuid;         //唯一标识
    private String name;            //标签名称
    private String opType;          //授权类型 0全部 1目录清单 2实施清单 3办理项
    private String useLevel;        //使用层级 0不限 2省级 3地市级 4区县级
    private float sort;            //排序
    private String parentGuid;      //父节点标识
    private String bakNote;         //备注
    private String createUId;       //创建人ID
    private String createUName;     //创建人名称
    private String createTime;      //创建时间
    private String updateUId;       //更新人ID
    private String updateUName;     //更新人名称
    private String updateTime;      //更新时间
    private String parentName;      //父节点名称
    private List childList;

}

2,业务模块serviceImpl

/**--------------------数据封装树结构代码---------------------------*/
@Override
public List tagConfTreeList(String deptId, List opType) {
    SysDepartment sysDepartment = departmentDao.selectDepartmentByUserId(deptId);
    String useLevel = null;//行使层级
    if(sysDepartment != null){
        if(sysDepartment.getUseLevel() != null && !"".equals(sysDepartment.getUseLevel())) {
            useLevel = sysDepartment.getUseLevel();//行使层级赋值
        }
    }
    List sysTagConfList = sysTagConfDao.tagConfTreeList(useLevel,opType); //查询所有数据
    List rootList = new ArrayList<>(); //根节点对象存放到这里
    List bodyList = new ArrayList<>(); //其他节点存放到这里,可以包含根节点
    for (SysTagConf sysTagConf: sysTagConfList) {
        if(sysTagConf != null) {
            if (sysTagConf.getParentGuid().equals("")) {
                rootList.add(sysTagConf);//所有父节点数据放进去
            } else {
                bodyList.add(sysTagConf); //其他节点数据也放进去
            }
        }
    }
    List stc = getTree(rootList,bodyList);//组装的树返给前端sb
    return stc;
}
public List getTree(List rootList,List bodyList){
    if(bodyList != null && !bodyList.isEmpty()){
        //声明一个map,用来过滤已操作过的数据
        Map map = Maps.newHashMapWithExpectedSize(bodyList.size());
        rootList.forEach(beanTree -> getChild(beanTree,map,bodyList));
        return rootList;
    }
    return null;
}

public void getChild(SysTagConf treeDto,Map map,List bodyList){
    List childList = Lists.newArrayList();
    bodyList.stream()
            .filter(c -> !map.containsKey(c.getRowGuid()))
            .filter(c ->c.getParentGuid().equals(treeDto.getRowGuid()))
            .forEach(c ->{
                map.put(c.getRowGuid(),c.getParentGuid());
                getChild(c,map,bodyList);
                childList.add(c);
            });
    treeDto.setChildList(childList);//子数据集
}
/**--------------------到此结束---------------------------*/

3返回效果

{
    "code": 0,
    "message": "操作成功",
    "data": [
        {
            "rowGuid": "1",
            "name": "1",
            "opType": "1",
            "useLevel": "1",
            "sort": 1,
            "parentGuid": "",
            "bakNote": "1",
            "createUId": "1",
            "createUName": "1",
            "createTime": "1",
            "updateUId": "1",
            "updateUName": "1",
            "updateTime": "1",
            "parentName": null,
            "childList": [
                {
                    "rowGuid": "2",
                    "name": "2",
                    "opType": "1",
                    "useLevel": "1",
                    "sort": 2,
                    "parentGuid": "1",
                    "bakNote": "2",
                    "createUId": "2",
                    "createUName": "2",
                    "createTime": "2",
                    "updateUId": "2",
                    "updateUName": "2",
                    "updateTime": "2",
                    "parentName": null,
                    "childList": [
                        {
                            "rowGuid": "3",
                            "name": "3",
                            "opType": "1",
                            "useLevel": "1",
                            "sort": 3,
                            "parentGuid": "2",
                            "bakNote": "3",
                            "createUId": "3",
                            "createUName": "3",
                            "createTime": "3",
                            "updateUId": "3",
                            "updateUName": "3",
                            "updateTime": "3",
                            "parentName": null,
                            "childList": []
                        }
                    ]
                }
            ]
        }
    ],
    "messageId": null
}

 

 

 

 

你可能感兴趣的:(Java数据封装成树形结构,多级)