一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

JAVA根据数据库表内容生产树结构JSON数据的实例代码

时间:2017-01-09 编辑:简简单单 来源:一聚教程网

1、利用场景

  组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段

2、构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据)

List> trees =newArrayList>();
tests.add(newTest("0","","关于本人"));
tests.add(newTest("1","0","技术学习"));
tests.add(newTest("2","0","兴趣"));
tests.add(newTest("3","1","JAVA"));
tests.add(newTest("4","1","oracle"));
tests.add(newTest("5","1","spring"));
tests.add(newTest("6","1","springmvc"));
tests.add(newTest("7","1","fastdfs"));
tests.add(newTest("8","1","linux"));
tests.add(newTest("9","2","骑行"));
tests.add(newTest("10","2","吃喝玩乐"));
tests.add(newTest("11","2","学习"));
tests.add(newTest("12","3","String"));
tests.add(newTest("13","4","sql"));
tests.add(newTest("14","5","ioc"));
tests.add(newTest("15","5","aop"));
tests.add(newTest("16","1","等等"));
tests.add(newTest("17","2","等等"));
tests.add(newTest("18","3","等等"));
tests.add(newTest("19","4","等等"));
tests.add(newTest("20","5","等等"));

3、源码

Tree.java

packagepers.kangxu.datautils.bean.tree;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
importcom.alibaba.fastjson.JSON;
/**
 * tree TODO
 *
 * @author kangxu2 2017-1-7
 *
 */
publicclassTree {
  /**
   * 节点ID
   */
  privateString id;
  /**
   * 显示节点文本
   */
  privateString text;
  /**
   * 节点状态,open closed
   */
  privateString state ="open";
  /**
   * 节点是否被选中 true false
   */
  privatebooleanchecked =false;
  /**
   * 节点属性
   */
  privateList> attributes;
  /**
   * 节点的子节点
   */
  privateList> children =newArrayList>();
  /**
   * 父ID
   */
  privateString parentId;
  /**
   * 是否有父节点
   */
  privatebooleanisParent =false;
  /**
   * 是否有子节点
   */
  privatebooleanisChildren =false;
  publicString getId() {
    returnid;
  }
  publicvoidsetId(String id) {
    this.id = id;
  }
  publicString getText() {
    returntext;
  }
  publicvoidsetText(String text) {
    this.text = text;
  }
  publicString getState() {
    returnstate;
  }
  publicvoidsetState(String state) {
    this.state = state;
  }
  publicbooleanisChecked() {
    returnchecked;
  }
  publicvoidsetChecked(booleanchecked) {
    this.checked = checked;
  }
  publicList> getAttributes() {
    returnattributes;
  }
  publicvoidsetAttributes(List> attributes) {
    this.attributes = attributes;
  }
  publicList> getChildren() {
    returnchildren;
  }
  publicvoidsetChildren(List> children) {
    this.children = children;
  }
  publicbooleanisParent() {
    returnisParent;
  }
  publicvoidsetParent(booleanisParent) {
    this.isParent = isParent;
  }
  publicbooleanisChildren() {
    returnisChildren;
  }
  publicvoidsetChildren(booleanisChildren) {
    this.isChildren = isChildren;
  }
  publicString getParentId() {
    returnparentId;
  }
  publicvoidsetParentId(String parentId) {
    this.parentId = parentId;
  }
  publicTree(String id, String text, String state,booleanchecked,
      List> attributes, List> children,
      booleanisParent,booleanisChildren, String parentID) {
    super();
    this.id = id;
    this.text = text;
    this.state = state;
    this.checked = checked;
    this.attributes = attributes;
    this.children = children;
    this.isParent = isParent;
    this.isChildren = isChildren;
    this.parentId = parentID;
  }
  publicTree() {
    super();
  }
  @Override
  publicString toString() {
    returnJSON.toJSONString(this);
  }
}

BuildTree.java

packagepers.kangxu.datautils.common.tree;
importjava.util.ArrayList;
importjava.util.List;
importpers.kangxu.datautils.bean.tree.Tree;
/**
 * 构建tree
 * TODO
 *
 * @author kangxu2 2017-1-7
 *
 */
publicclassBuildTree {
  /**
   *
   * TODO
   *
   * @author kangxu2 2017-1-7
   *
   * @param nodes
   * @return
   */
  publicstatic Tree build(List> nodes) {
    if(nodes ==null){
      returnnull;
    }
    List> topNodes =newArrayList>();
    for(Tree children : nodes) {
      String pid = children.getParentId();
      if(pid ==null||"".equals(pid)) {
        topNodes.add(children);
        continue;
      }
      for(Tree parent : nodes) {
        String id = parent.getId();
        if(id !=null&& id.equals(pid)) {
          parent.getChildren().add(children);
          children.setParent(true);
          parent.setChildren(true);
          continue;
        }
      }
    }
    Tree root =newTree();
    if(topNodes.size() ==0) {
      root = topNodes.get(0);
    }else{
      root.setId("-1");
      root.setParentId("");
      root.setParent(false);
      root.setChildren(true);
      root.setChecked(true);
      root.setChildren(topNodes);
      root.setText("顶级节点");
    }
    returnroot;
  }
}

BuildTreeTester.java

packagepers.kangxu.datautils.test;
importjava.util.ArrayList;
importjava.util.List;
importpers.kangxu.datautils.bean.tree.Tree;
importpers.kangxu.datautils.common.tree.BuildTree;
publicclassBuildTreeTester {
  publicstaticvoidmain(String[] args) {
    List> trees =newArrayList>();
    List tests =newArrayList();
    tests.add(newTest("0","","关于本人"));
    tests.add(newTest("1","0","技术学习"));
    tests.add(newTest("2","0","兴趣"));
    tests.add(newTest("3","1","JAVA"));
    tests.add(newTest("4","1","oracle"));
    tests.add(newTest("5","1","spring"));
    tests.add(newTest("6","1","springmvc"));
    tests.add(newTest("7","1","fastdfs"));
    tests.add(newTest("8","1","linux"));
    tests.add(newTest("9","2","骑行"));
    tests.add(newTest("10","2","吃喝玩乐"));
    tests.add(newTest("11","2","学习"));
    tests.add(newTest("12","3","String"));
    tests.add(newTest("13","4","sql"));
    tests.add(newTest("14","5","ioc"));
    tests.add(newTest("15","5","aop"));
    tests.add(newTest("16","1","等等"));
    tests.add(newTest("17","2","等等"));
    tests.add(newTest("18","3","等等"));
    tests.add(newTest("19","4","等等"));
    tests.add(newTest("20","5","等等"));
    for(Test test : tests) {
      Tree tree =newTree();
      tree.setId(test.getId());
      tree.setParentId(test.getPid());
      tree.setText(test.getText());
      trees.add(tree);
    }
    Tree t = BuildTree.build(trees);
    System.out.println(t);
  }
}
classTest {
  privateString id;
  privateString pid;
  privateString text;
  publicString getId() {
    returnid;
  }
  publicvoidsetId(String id) {
    this.id = id;
  }
  publicString getPid() {
    returnpid;
  }
  publicvoidsetPid(String pid) {
    this.pid = pid;
  }
  publicString getText() {
    returntext;
  }
  publicvoidsetText(String text) {
    this.text = text;
  }
  publicTest(String id, String pid, String text) {
    super();
    this.id = id;
    this.pid = pid;
    this.text = text;
  }
  publicTest() {
    super();
  }
  @Override
  publicString toString() {
    return"Test [id="+ id +", pid="+ pid +", text="+ text +"]";
  }
}

4、运行结果

JSON数据:

{
  "checked":true,
  "children": [
    {
      "checked":false,
      "children": [
        {
          "checked":false,
          "children": [
            {
              "checked":false,
              "children": [
                {
                  "checked":false,
                  "children": [],
                  "id":"12",
                  "parent":true,
                  "parentId":"3",
                  "state":"open",
                  "text":"String"
                },
                {
                  "checked":false,
                  "children": [],
                  "id":"18",
                  "parent":true,
                  "parentId":"3",
                  "state":"open",
                  "text":"等等"
                }
              ],
              "id":"3",
              "parent":true,
              "parentId":"1",
              "state":"open",
              "text":"JAVA"
            },
            {
              "checked":false,
              "children": [
                {
                  "checked":false,
                  "children": [],
                  "id":"13",
                  "parent":true,
                  "parentId":"4",
                  "state":"open",
                  "text":"sql"
                },
                {
                  "checked":false,
                  "children": [],
                  "id":"19",
                  "parent":true,
                  "parentId":"4",
                  "state":"open",
                  "text":"等等"
                }
              ],
              "id":"4",
              "parent":true,
              "parentId":"1",
              "state":"open",
              "text":"oracle"
            },
            {
              "checked":false,
              "children": [
                {
                  "checked":false,
                  "children": [],
                  "id":"14",
                  "parent":true,
                  "parentId":"5",
                  "state":"open",
                  "text":"ioc"
                },
                {
                  "checked":false,
                  "children": [],
                  "id":"15",
                  "parent":true,
                  "parentId":"5",
                  "state":"open",
                  "text":"aop"
                },
                {
                  "checked":false,
                  "children": [],
                  "id":"20",
                  "parent":true,
                  "parentId":"5",
                  "state":"open",
                  "text":"等等"
                }
              ],
              "id":"5",
              "parent":true,
              "parentId":"1",
              "state":"open",
              "text":"spring"
            },
            {
              "checked":false,
              "children": [],
              "id":"6",
              "parent":true,
              "parentId":"1",
              "state":"open",
              "text":"springmvc"
            },
            {
              "checked":false,
              "children": [],
              "id":"7",
              "parent":true,
              "parentId":"1",
              "state":"open",
              "text":"fastdfs"
            },
            {
              "checked":false,
              "children": [],
              "id":"8",
              "parent":true,
              "parentId":"1",
              "state":"open",
              "text":"linux"
            },
            {
              "checked":false,
              "children": [],
              "id":"16",
              "parent":true,
              "parentId":"1",
              "state":"open",
              "text":"等等"
            }
          ],
          "id":"1",
          "parent":true,
          "parentId":"0",
          "state":"open",
          "text":"技术学习"
        },
        {
          "checked":false,
          "children": [
            {
              "checked":false,
              "children": [],
              "id":"9",
              "parent":true,
              "parentId":"2",
              "state":"open",
              "text":"骑行"
            },
            {
              "checked":false,
              "children": [],
              "id":"10",
              "parent":true,
              "parentId":"2",
              "state":"open",
              "text":"吃喝玩乐"
            },
            {
              "checked":false,
              "children": [],
              "id":"11",
              "parent":true,
              "parentId":"2",
              "state":"open",
              "text":"学习"
            },
            {
              "checked":false,
              "children": [],
              "id":"17",
              "parent":true,
              "parentId":"2",
              "state":"open",
              "text":"等等"
            }
          ],
          "id":"2",
          "parent":true,
          "parentId":"0",
          "state":"open",
          "text":"兴趣"
        }
      ],
      "id":"0",
      "parent":false,
      "parentId":"",
      "state":"open",
      "text":"关于本人"
    }
  ],
  "id":"-1",
  "parent":false,
  "parentId":"",
  "state":"open",
  "text":"顶级节点"
}

热门栏目