本篇文章主要讲解JAVA 根据数据库表内容生产树结构JSON数据,文章内容主要包括关于JAVA,JSON等,请参考。
1、利用场景
组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段
2、构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据)
> trees = new ArrayList>(); 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; java.util.ArrayList; 4 import java.util.List; 5 import java.util.Map; com.alibaba.fastjson.JSON; * tree TODO <br> 11 * kangxu2 2017-1-7 13 * Tree<T> { * 节点ID String id; * 显示节点文本 String text; * 节点状态,open closed String state = "open"; * 节点是否被选中 true false checked = false; * 节点属性 List<Map<String, Object>> attributes; * 节点的子节点 List<Tree<T>> children = new ArrayList<Tree<T>>(); * 父ID String parentId; * 是否有父节点 isParent = false; * 是否有子节点 isChildren = false; String getId() { 55 return id; 56 } setId(String id) { 59 this.id = id; 60 } String getText() { 63 return text; 64 } setText(String text) { 67 this.text = text; 68 } String getState() { 71 return state; 72 } setState(String state) { 75 this.state = state; 76 } isChecked() { 79 return checked; 80 } setChecked(boolean checked) { 83 this.checked = checked; 84 } List<Map<String, Object>> getAttributes() { 87 return attributes; 88 } setAttributes(List<Map<String, Object>> attributes) { 91 this.attributes = attributes; 92 } List<Tree<T>> getChildren() { 95 return children; 96 } setChildren(List<Tree<T>> children) { 99 this.children = children; 100 } isParent() { 103 return isParent; 104 } setParent(boolean isParent) { 107 this.isParent = isParent; 108 } isChildren() { 111 return isChildren; 112 } setChildren(boolean isChildren) { 115 this.isChildren = isChildren; 116 } String getParentId() { 119 return parentId; 120 } setParentId(String parentId) { 123 this.parentId = parentId; 124 } 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 } Tree() { 142 super(); 143 } 144 145 @Override 146 public String toString() { JSON.toJSONString(this); 149 } 150 151 }