定义树对象

package com.ulic.gis.authorityManage.vo;

import java.util.ArrayList;
import java.util.List;


public class GisResourcesVO {
    private Long nodeId;
    private Long pid;
    private Long level;
    private Long roleId;
    private String text; 
    private String icon;
    private List<GisResourcesVO> nodes;
    
    public GisResourcesVO() {
        this.nodes = new ArrayList<GisResourcesVO>();
    }
    
    public static GisResourcesVO createTree(List<GisResourcesVO> nodes) {
        if(nodes == null || nodes.size() < 0){
            return null;
        }
        GisResourcesVO root = new GisResourcesVO();
        for(GisResourcesVO node : nodes){
            
            if(node.getPid()==null){
                root.getNodes().add(node);
            }else{
                addChild(root, node);
            }
        }
        
        //去除空节点
        cleanEmptyNodes(root);
        return root;
    }
    
    private static void addChild(GisResourcesVO node, GisResourcesVO child) {
        for(GisResourcesVO item : node.getNodes()){
            if(item.getNodeId().equals(child.getPid())){
                item.getNodes().add(child);
                break;
            }else{
                if(item.getNodes()!=null && item.getNodes().size()>0){
                    addChild(item, child);
                }
            }
        }
    }
    
    private static void cleanEmptyNodes(GisResourcesVO node) {
        if(node != null){
            for(GisResourcesVO index : node.getNodes()){
                if(index.getNodes()!=null && index.getNodes().size()>0){
                    cleanEmptyNodes(index);
                }else{
                    index.setNodes(null);
                }
            }
        }
    }
    
    public Long getNodeId() {
        return nodeId;
    }

    public void setNodeId(Long nodeId) {
        this.nodeId = nodeId;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public Long getLevel() {
        return level;
    }

    public void setLevel(Long level) {
        this.level = level;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public List<GisResourcesVO> getNodes() {
        return nodes;
    }

    public void setNodes(List<GisResourcesVO> nodes) {
        this.nodes = nodes;
    }

    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }
    
}

应用:

    @Override
    public List<GisResourcesVO> getGisResourcesVOByRoleId(Long roleId) {
        
     

        List<GisResourcesVO> nodes = roleManageDao.getGisResourcesVOByRoleId(roleId);
        
    
        
        GisResourcesVO menuNodetree = GisResourcesVO.createTree(nodes);
        

        
        return menuNodetree.getNodes();
    }
原文地址:https://www.cnblogs.com/yinyl/p/11124923.html