The rear end of the data tree structure package

thought:

Get List Database return header, through the collection of data to map the encapsulated set, then call menuList class method implementation tools MenuTreeUtil tree

Tools MenuTreeUtil categories:

     Get parent node menuList Method: traversing return header database, get the parent node data according pid, then call menuChild method to get the child nodes, then set the map to the package list set;

     Getting child nodes menuChild Method: traversing the database returns the set, according to an attribute child node DID acquired entity class object dept of the package, and then recursively acquire a child node, and encapsulates the map set, and the set list to the map set package;

step:

I. entity class

@SuppressWarnings("serial")
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class Dept implements Serializable {

    private Integer dId;
    private String dName;
    private String db_source;
    private Integer pid;
    private List<Dept> chilren;   //子节点


}

II. Tools

package com.zhengxh.microservice.util;

import com.zhengxh.microservice.entity.Dept;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class MenuTreeUtil {

    public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
    public List<Dept> menuCommon;

    public List<Object> menuList(List<Dept> menu){ //controller层调用的方法   ,并将数据以list的形式返回
        this.menuCommon = menu;
        List<Object> list = new ArrayList<Object>();
        for (Dept x : menu) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            if(x.getPid().equals(0)){
                mapArr.put("id", x.getDId());
                mapArr.put("tree_name", x.getDName());
                mapArr.put("parent_source", x.getDb_source());
                mapArr.put("Parent_id", x.getPid());
                mapArr.put("child", menuChild(x.getDId()));
                list.add(mapArr);
            }
        }
        return list;
    }

    public List<Object> menuChild(Integer id){ //子集查找遍历
        List<Object> lists = new ArrayList<Object>();
        for(Dept a : menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getPid().equals(id)){
                childArray.put("id", a.getDId());
                childArray.put("tree_name", a.getDName());
                childArray.put("parent_source", a.getDb_source());
                childArray.put("Parent_id", a.getPid());
                childArray.put("child", menuChild(a.getDId()));
                lists.add(childArray);
            }
        }
        return lists;
    }

}

Three .controller call:

  @PostMapping(value = "/findTree")
    public Object findTree() {

        Map<String, Object> returnMap = new HashMap();
        List<Dept> lists =  deptService.getList();
        MenuTreeUtil menuTree = new MenuTreeUtil();
        List<Dept> lt = new ArrayList<Dept>();
        for (int i = 0; i < lists.size(); i++) {
            Dept t = new Dept();
            t.setDId((Integer) lists.get(i).getDId());
            t.setDName((String) lists.get(i).getDName());
            t.setDb_source((String) lists.get(i).getDb_source());
            t.setPid((Integer) lists.get(i).getPid());
            lt.add(t);
        }
        System.out.println(lt);
        List<Object> menuList = menuTree.menuList(lt);
        returnMap.put("list", menuList);
        return returnMap;
    }

 

He published 193 original articles · won praise 30 · views 110 000 +

Guess you like

Origin blog.csdn.net/yiye2017zhangmu/article/details/100264029