java 组织机构树

package com.itstar.utils.tree;

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

/**
 * @Auther: xyy
 * @CreateDate: 2021/3/8 21 06
 * @Description:  树结构模型
 */
public class TreeModel {

    private String id;
    private String text;// 名称
    private String iconCls;// 图标
    private String linkUrl;// 链接地址

    // 状态(closed折叠、open展开)
//	private String state = "closed";
    private List<TreeModel> children;// 孩子节点集合

    public TreeModel() {
        this.children = new ArrayList<TreeModel>();
    }

    public String getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

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

    public String getLinkUrl() {
        return linkUrl;
    }

    public void setLinkUrl(String linkUrl) {
        this.linkUrl = linkUrl;
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public List<TreeModel> getChildren() {
        return children;
    }

    public void setChildren(List<TreeModel> children) {
        this.children = children;
    }

}
 /**
     * 单位部门树(含部门)json
     */
    @RequestMapping("/organTree")
    @ResponseBody
    // 返回的是JSON格式
    public List<TreeModel> deptTree(HttpServletRequest request) {
        // 默认从根节点开始
        String id = "0";
        TreeModel tm = organService.selectTree(id, true);
        return tm.getChildren();
    }
public TreeModel selectTree(String id, boolean containsDept) {
        TreeModel tm = new TreeModel();

        //在此只保留部门或单位的id及name属性,可拓展
        String[] s = new String[] { "getDeptId", "getDeptName" };
        String type = containsDept ? "%" : "unit";
        List<Organ> li = this.selectChildren(id, type);
        this.tree(tm, li, s, containsDept);
        return tm;
    }
private List<Organ> selectChildren(String id, String type) {
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("type", type);
        para.put("parentId", id);
        return organMapper.list(para);
    }
 // 构造组织机构树数据
    private void tree(TreeModel tm, List<Organ> li, String[] s,
                      boolean containsDept) {

        if (!CollectionUtils.isEmpty(li)) {

            for (int i = 0, l = li.size(); i < l; i++) {
                TreeModel ntm = new TreeModel();
                Organ dept = li.get(i);

                TreeUtil.copyModel(ntm, dept, s);// 复制值到TreeModel
                tm.getChildren().add(ntm);// 添加到孩子节点列表

                String type = containsDept ? "%" : "unit";
                List<Organ> list = this.selectChildren(String.valueOf(dept.getDeptId()), type);
                tree(ntm, list, s, containsDept);// 递归,实现无限层级
            }
        }
    }
package com.itstar.utils.tree;

import java.lang.reflect.Method;

/**
 * @Auther: xyy
 * @CreateDate: 2021/3/8 21 08
 * @Description: 树结构处理工具类
 */
public class TreeUtil {

    /**
     * 复制数据到树model
     * @param tm 树model
     * @param o 待复制的对象
     * @param s 变长参数,方法的名称字符串
     *          约定第1个为id,第2个为text,
     *          第3个为linkUrl,第4个为iconCls,
     *          第5个为splitNum
     */
    public static void copyModel(TreeModel tm, Object o, String... s) {
        Class<?> clazz = o.getClass();// 获取类包名
        int l = s.length;

        try {
            if(l - 0 > 0 && s[0] != null) {
                Method id = clazz.getMethod(s[0]);// 约定第1个为id
                tm.setId(String.valueOf(id.invoke(o)));
            }
            if(l - 1 > 0 && s[1] != null) {
                Method text = clazz.getMethod(s[1]);// 约定第2个为text
                tm.setText(String.valueOf(text.invoke(o)));
            }
            if(l - 2 > 0 && s[2] != null) {
                Method url = clazz.getMethod(s[2]);// 约定第3个为funcUrl
                tm.setLinkUrl(String.valueOf(url.invoke(o)));
            }
            if(l - 3 > 0 && s[3] != null) {
                Method cls = clazz.getMethod(s[3]);// 约定第4个为iconCls
                tm.setIconCls(String.valueOf(cls.invoke(o)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

最终实现:

[
    {
        "id": "100",
        "text": "中国科技",
        "iconCls": null,
        "linkUrl": null,
        "children": [
            {
                "id": "101",
                "text": "深圳总公司",
                "iconCls": null,
                "linkUrl": null,
                "children": [
                    {
                        "id": "103",
                        "text": "研发部门",
                        "iconCls": null,
                        "linkUrl": null,
                        "children": []
                    },
                    {
                        "id": "104",
                        "text": "市场部门",
                        "iconCls": null,
                        "linkUrl": null,
                        "children": []
                    },
                    {
                        "id": "105",
                        "text": "测试部门",
                        "iconCls": null,
                        "linkUrl": null,
                        "children": []
                    }
                ]
            },
            {
                "id": "102",
                "text": "长沙分公司",
                "iconCls": null,
                "linkUrl": null,
                "children": [
                    {
                        "id": "108",
                        "text": "市场部门",
                        "iconCls": null,
                        "linkUrl": null,
                        "children": []
                    },
                    {
                        "id": "109",
                        "text": "财务部门",
                        "iconCls": null,
                        "linkUrl": null,
                        "children": []
                    }
                ]
            }
        ]
    }
]

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值