Welcome

首页 / 软件开发 / JAVA / JAVA根据数据库表内容生产树结构JSON数据

JAVA根据数据库表内容生产树结构JSON数据2017-01-08 博客园 kangxu1、利用场景

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

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

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

Tree.java

1 package pers.kangxu.datautils.bean.tree;2 3 import java.util.ArrayList;4 import java.util.List;5 import java.util.Map;6 7 import com.alibaba.fastjson.JSON;8 9 /** 10* tree TODO <br> 11*12* @author kangxu2 2017-1-7 13*14*/ 15 public class Tree<T> { 16 /** 17* 节点ID 18*/ 19 private String id; 20 /** 21* 显示节点文本 22*/ 23 private String text; 24 /** 25* 节点状态,open closed 26*/ 27 private String state = "open"; 28 /** 29* 节点是否被选中 true false 30*/ 31 private boolean checked = false; 32 /** 33* 节点属性 34*/ 35 private List<Map<String, Object>> attributes; 36 /** 37* 节点的子节点 38*/ 39 private List<Tree<T>> children = new ArrayList<Tree<T>>(); 4041 /** 42* 父ID 43*/ 44 private String parentId; 45 /** 46* 是否有父节点 47*/ 48 private boolean isParent = false; 49 /** 50* 是否有子节点 51*/ 52 private boolean isChildren = false; 5354 public String getId() { 55 return id; 56 } 5758 public void setId(String id) { 59 this.id = id; 60 } 6162 public String getText() { 63 return text; 64 } 6566 public void setText(String text) { 67 this.text = text; 68 } 6970 public String getState() { 71 return state; 72 } 7374 public void setState(String state) { 75 this.state = state; 76 } 7778 public boolean isChecked() { 79 return checked; 80 } 8182 public void setChecked(boolean checked) { 83 this.checked = checked; 84 } 8586 public List<Map<String, Object>> getAttributes() { 87 return attributes; 88 } 8990 public void setAttributes(List<Map<String, Object>> attributes) { 91 this.attributes = attributes; 92 } 9394 public List<Tree<T>> getChildren() { 95 return children; 96 } 9798 public void setChildren(List<Tree<T>> children) { 99 this.children = children;100 }101 102 public boolean isParent() {103 return isParent;104 }105 106 public void setParent(boolean isParent) {107 this.isParent = isParent;108 }109 110 public boolean isChildren() {111 return isChildren;112 }113 114 public void setChildren(boolean isChildren) {115 this.isChildren = isChildren;116 }117 118 public String getParentId() {119 return parentId;120 }121 122 public void setParentId(String parentId) {123 this.parentId = parentId;124 }125 126 public Tree(String id, String text, String state, boolean checked,127 List<Map<String, Object>> attributes, List<Tree<T>> children,128 boolean isParent, boolean isChildren, String parentID) {129 super();130 this.id = id;131 this.text = text;132 this.state = state;133 this.checked = checked;134 this.attributes = attributes;135 this.children = children;136 this.isParent = isParent;137 this.isChildren = isChildren;138 this.parentId = parentID;139 }140 141 public Tree() {142 super();143 }144 145 @Override146 public String toString() {147 148 return JSON.toJSONString(this);149 }150 151 }
BuildTree.java

 1 package pers.kangxu.datautils.common.tree; 23 import java.util.ArrayList; 4 import java.util.List; 56 import pers.kangxu.datautils.bean.tree.Tree; 78 /** 9* 构建tree10* TODO11* <br>12* @author kangxu2 2017-1-713*14*/15 public class BuildTree {16 17 /**18* 19* TODO20* <br>21* @author kangxu2 2017-1-722*23* @param nodes24* @return25*/26 public static <T> Tree<T> build(List<Tree<T>> nodes) {27 28 if(nodes == null){29 return null;30 }31 List<Tree<T>> topNodes = new ArrayList<Tree<T>>();32 33 for (Tree<T> children : nodes) {34 35 String pid = children.getParentId();36 if (pid == null || "".equals(pid)) {37 topNodes.add(children);38 39 continue;40 }41 42 for (Tree<T> parent : nodes) {43 String id = parent.getId();44 if (id != null && id.equals(pid)) {45 parent.getChildren().add(children);46 children.setParent(true);47 parent.setChildren(true);48 49 continue;50 }51 }52 53 }54 55 Tree<T> root = new Tree<T>();56 if (topNodes.size() == 0) {57 root = topNodes.get(0);58 } else {59 root.setId("-1");60 root.setParentId("");61 root.setParent(false);62 root.setChildren(true);63 root.setChecked(true);64 root.setChildren(topNodes);65 root.setText("顶级节点");66 67 }68 69 return root;70 }71 72 }
BuildTreeTester.java

 1 package pers.kangxu.datautils.test; 23 import java.util.ArrayList; 4 import java.util.List; 56 import pers.kangxu.datautils.bean.tree.Tree; 7 import pers.kangxu.datautils.common.tree.BuildTree; 89 public class BuildTreeTester {10 11 public static void main(String[] args) {12 13 14 List<Tree<Test>> trees = new ArrayList<Tree<Test>>();15 List<Test> tests = new ArrayList<Test>();16 tests.add(new Test("0", "", "关于本人"));17 tests.add(new Test("1", "0", "技术学习"));18 tests.add(new Test("2", "0", "兴趣"));19 tests.add(new Test("3", "1", "JAVA"));20 tests.add(new Test("4", "1", "oracle"));21 tests.add(new Test("5", "1", "spring"));22 tests.add(new Test("6", "1", "springmvc"));23 tests.add(new Test("7", "1", "fastdfs"));24 tests.add(new Test("8", "1", "linux"));25 tests.add(new Test("9", "2", "骑行"));26 tests.add(new Test("10", "2", "吃喝玩乐"));27 tests.add(new Test("11", "2", "学习"));28 tests.add(new Test("12", "3", "String"));29 tests.add(new Test("13", "4", "sql"));30 tests.add(new Test("14", "5", "ioc"));31 tests.add(new Test("15", "5", "aop"));32 tests.add(new Test("16", "1", "等等"));33 tests.add(new Test("17", "2", "等等"));34 tests.add(new Test("18", "3", "等等"));35 tests.add(new Test("19", "4", "等等"));36 tests.add(new Test("20", "5", "等等"));37 38 for (Test test : tests) {39 Tree<Test> tree = new Tree<Test>();40 tree.setId(test.getId());41 tree.setParentId(test.getPid());42 tree.setText(test.getText());43 44 trees.add(tree);45 }46 47 Tree<Test> t = BuildTree.build(trees);48 System.out.println(t);49 }50 }51 52 class Test {53 54 private String id;55 private String pid;56 private String text;57 58 public String getId() {59 return id;60 }61 62 public void setId(String id) {63 this.id = id;64 }65 66 public String getPid() {67 return pid;68 }69 70 public void setPid(String pid) {71 this.pid = pid;72 }73 74 public String getText() {75 return text;76 }77 78 public void setText(String text) {79 this.text = text;80 }81 82 public Test(String id, String pid, String text) {83 super();84 this.id = id;85 this.pid = pid;86 this.text = text;87 }88 89 public Test() {90 super();91 }92 93 @Override94 public String toString() {95 return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]";96 }97 98 }
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": "顶级节点"}
学习无止尽,代码我疯狂

原文: