JAVA树形目录结构表并将其拼接成树形JSON

表结构:

CREATE TABLE `t_organization` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键id',
  `org_id` int(11) NOT NULL COMMENT 'id',
  `org_name` varchar(50) NOT NULL COMMENT '名称',
  `parent_id` int(11) NOT NULL COMMENT '父id',
  `type_level` int(11) NOT NULL COMMENT '级别',
  `is_deleted` tinyint(4) DEFAULT '0' COMMENT '是否删除',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='组织架构表';

示例数据:

JAVA树形目录结构表并将其拼接成树形JSON_第1张图片

效果:

[
    {
      "children": [
        {
          "children": [
            {
              "children": [
                {
                  "id": 101010101,
                  "name": "家居百货",
                  "parentId": 1010101,
                  "typeLevel": 4
                }
              ],
              "id": 1010101,
              "name": "家居家纺",
              "parentId": 10101,
              "typeLevel": 3
            },
            {
              "id": 1010102,
              "name": "美妆个护",
              "parentId": 10101,
              "typeLevel": 3
            }
          ],
          "id": 10101,
          "name": "家居日百",
          "parentId": 101,
          "typeLevel": 2
        },
        {
          "id": 10102,
          "name": "服饰鞋包",
          "parentId": 101,
          "typeLevel": 2
        }
      ],
      "id": 101,
      "name": "特卖事业部",
      "parentId": 0,
      "typeLevel": 1
    },
    {
      "children": [
        {
          "children": [
            {
              "children": [
                {
                  "id": 102010101,
                  "name": "商品开发一组",
                  "parentId": 1020101,
                  "typeLevel": 4
                }
              ],
              "id": 1020101,
              "name": "超品一部",
              "parentId": 10201,
              "typeLevel": 3
            }
          ],
          "id": 10201,
          "name": "合资业务",
          "parentId": 102,
          "typeLevel": 2
        }
      ],
      "id": 102,
      "name": "超品事业部",
      "parentId": 0,
      "typeLevel": 1
    }
  ]

Java代码:

创建树形结构的json的类:

import com.alibaba.fastjson.JSONArray;
import com.study.multisource.entity.Node;

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

/**
 * @author lh
 * @date 2020/8/5 10:04
 */
public class TreeBuilder {
    List nodes = new ArrayList<>();

    public TreeBuilder(List nodes){
        this.nodes = nodes;
    }

    public String buildJSONTree() {
        List treeNodes = buildTree();
        String json = JSONArray.toJSONString(treeNodes);
        return json;

    }

    // 构建树形结构
    public List buildTree() {
        List treeNodes = new ArrayList<>();
        List rootNodes = getRootNodes();
        for (Node rootNode : rootNodes) {
            buildChildNodes(rootNode);
            treeNodes.add(rootNode);
        }
        return treeNodes;
    }

    // 递归子节点
    public void buildChildNodes(Node node) {
        List children = getChildNodes(node);
        if (!children.isEmpty()) {
            for (Node child : children) {
                buildChildNodes(child);
            }
            node.setChildren(children);
        }
    }

    // 获取父节点下所有的子节点
    public List getChildNodes(Node pnode) {
        List childNodes = new ArrayList<>();
        for (Node n : nodes) {
            if (pnode.getId().equals(n.getParentId())) {
                childNodes.add(n);
            }
        }
        return childNodes;
    }
    // 判断是否为根节点,根节点的父节点id(parentId)不可能为任何一个节点的id
    public boolean rootNode(Node node) {
        boolean isRootNode = true;
        for (Node n : nodes) {
            if (node.getParentId().equals(n.getId())) {
                isRootNode = false;
                break;
            }
        }
        return isRootNode;
    }

    // 获取集合中所有的根节点
    private List getRootNodes() {
        List rootNodes = new ArrayList<>();
        for (Node n : nodes) {
            if (rootNode(n)) {
                rootNodes.add(n);
            }
        }
        return rootNodes;
    }
}

Node类:

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

/**
 * @author lh
 * @date 2020/8/4 18:16
 */
@Data
@ToString
public class Node implements Serializable {
    private Integer id;
    private String name;
    private Integer parentId;
    private Integer typeLevel;

    private List children;

    public Node() {

    }

    public Node(Integer parentId,Integer id, String name,Integer typeLevel) {
        this.parentId = parentId;
        this.typeLevel = typeLevel;
        this.id = id;
        this.name = name;
    }

}

使用:

String jsonStr = new TreeBuilder(nodeList).buildJSONTree();

nodeList为数据库中查出来的数据转化的Node类型

你可能感兴趣的:(Java)