树形结构(递归)增强

TreesUtils:树形工具类,根据父子ID形成树形结构

package cn.cityworks.utils;

import cn.cityworks.domain.Organization;


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author: zsx
 * @date: 2019/4/16 0016
 */
public class TreesUtils {

    /**
     *  @Description  //TODO
     *  @Param  nodes  :所有的节点列表
     */
    public  static List  data(List nodes)  {
        ArrayList rootNode  =  new  ArrayList();
        first:for  (Object  obj  :  nodes)
            if (Map.class.isAssignableFrom(obj.getClass())) {
                Map<String, Object> map = (Map) obj;
                if (map.get("pid") != null && map.get("pid").toString().equals("0")) {
                    rootNode.add(obj);
                }

            } else {


                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor("pid", obj.getClass());
                    Method get = pd.getReadMethod();
                    String pid = null;

                    pid = (String)get.invoke(obj);
                    if ("0".equals(pid)) {
                        rootNode.add(obj);
                    }

                } catch (InvocationTargetException|IntrospectionException|IllegalAccessException e) {
                    e.printStackTrace();
                    continue first;
                }

            }
        second:for  (Object  obj  :  rootNode)  {
            String id="";
            List<Object>  child  = null;
            if (Map.class.isAssignableFrom(obj.getClass())) {
                if (((Map)obj).get("id") != null && ((Map)obj).get("id").toString().equals("0")) {
                    id=((Map)obj).get("id").toString();
                    child  =getChild(id, nodes);
                }
                ((Map)obj).put("childs",child);

            }else {
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor("id", obj.getClass());
                    Method get = pd.getReadMethod();
                    String pid = null;

                    id = (String)get.invoke(obj);
                    child  = getChild(id, nodes);
                    pd = new PropertyDescriptor("childs", obj.getClass());
                    Method seter = pd.getWriteMethod();
                    seter.invoke(obj,child);


                } catch (InvocationTargetException|IntrospectionException|IllegalAccessException e) {
                    e.printStackTrace();
                    continue second;
                }


            }
        }
        return  rootNode;
    }
    
    /**
     *  @Description  //TODO  获取根节点的子节点
     */
    public static List  getChild(String  id,  List<?>  allNode)  {
        //存放子菜单的集合
        ArrayList listChild  =  new  ArrayList<>();
        first:for  (Object  obj  :  allNode)  {
            if(Map.class.isAssignableFrom(obj.getClass())){
                Map<String, Object> map = (Map) obj;
                if (map.get("pid") != null && map.get("pid").toString().equals(id)) {
                    listChild.add(obj);
                }

            }else{


                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor("pid", obj.getClass());
                    Method get = pd.getReadMethod();
                    String pid = null;

                    pid = (String)get.invoke(obj);
                    if (id.equals(pid)) {
                        listChild.add(obj);
                    }

                } catch (InvocationTargetException|IntrospectionException|IllegalAccessException e) {
                    e.printStackTrace();
                    continue first;
                }


            }

        }
        //递归(递归容易栈溢出,后期可以考虑尾递归优化)
        second:for  (Object  obj  :  listChild)  {

            String idTemp="";
            List<Object>  child  = new ArrayList<>();
            if (Map.class.isAssignableFrom(obj.getClass())) {
                if (((Map)obj).get("id") != null && ((Map)obj).get("id").toString().equals("0")) {
                    id=((Map)obj).get("id").toString();
                    child  =getChild(idTemp, allNode);
                }
                ((Map)obj).put("childs",child);

            }else {
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor("id", obj.getClass());
                    Method get = pd.getReadMethod();
                    idTemp = (String)get.invoke(obj);
                    child  = getChild(idTemp, allNode);
                    pd = new PropertyDescriptor("childs", obj.getClass());
                    Method seter = pd.getWriteMethod();
                    seter.invoke(obj,child);


                } catch (InvocationTargetException|IntrospectionException|IllegalAccessException e) {
                    e.printStackTrace();
                    continue second;
                }


            }
        }
        if  (listChild.size()  ==  0)  {
            return  null;
        }
        return  listChild;
    }


}

Organization实体类:主要根据pid。childs。和自身ID去实现树形

package cn.cityworks.domain;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

public class Organization {

    private String id;//-------------id
    private String name;
    private String website;
    private String brief;
    private String path;
    private String telephone;
    @ElementCollection//(fetch=FetchType.EAGER)
    private List<String> types;
    private String color;
    private String ocode;
    private String pid;// --------------树形PID
    //获取菜单
    private List<Organization> childs=new ArrayList<Organization>();//--------------树形
    private int show;

    /**
     * 对应oracle表中的id
     */
    private String orclid;



    public String getPid() {
        return pid;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWebsite() {
        return website;
    }

    public int getShow() {
        return this.show;
    }

    public void setShow(int show) {
        this.show = show;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getBrief() {
        return brief;
    }

    public void setBrief(String brief) {
        this.brief = brief;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Transient
    public List<String> getTypes() {
        return types;
    }

    public void setTypes(List<String> types) {
        this.types = types;
    }

    public String getOcode() {
        return ocode;
    }

    public void setOcode(String ocode) {
        this.ocode = ocode;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }



    public String getOrclid()
    {
        return orclid;
    }

    public void setOrclid(String orclid)
    {
        this.orclid = orclid;
    }

}

最终调用:data为树形结构

     List<Organization> orgs = orgService.findAll();
	   return TreesUtils().data(orgs);
    

或者

     //树形
            List<Map<String,Object>> client=session.createSQLQuery(sqlStr).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
            //返回树形
            return new TreesUtils().data(client);

若想使用TreesUtils,只需更换实体类即可

可以整理2种类型的数,如下

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/IT_Java_Roy/article/details/89382989
今日推荐