1、利用场景

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

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

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

3、源码

Tree.java

 package pers.kangxu.datautils.bean.tree;

 import java.util.ArrayList;
import java.util.List;
import java.util.Map; import com.alibaba.fastjson.JSON; /**
* tree TODO <br>
*
* @author kangxu2 2017-1-7
*
*/
public class Tree<T> {
/**
* 节点ID
*/
private String id;
/**
* 显示节点文本
*/
private String text;
/**
* 节点状态,open closed
*/
private String state = "open";
/**
* 节点是否被选中 true false
*/
private boolean checked = false;
/**
* 节点属性
*/
private List<Map<String, Object>> attributes;
/**
* 节点的子节点
*/
private List<Tree<T>> children = new ArrayList<Tree<T>>(); /**
* 父ID
*/
private String parentId;
/**
* 是否有父节点
*/
private boolean isParent = false;
/**
* 是否有子节点
*/
private boolean isChildren = false; 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 getState() {
return state;
} public void setState(String state) {
this.state = state;
} public boolean isChecked() {
return checked;
} public void setChecked(boolean checked) {
this.checked = checked;
} public List<Map<String, Object>> getAttributes() {
return attributes;
} public void setAttributes(List<Map<String, Object>> attributes) {
this.attributes = attributes;
} public List<Tree<T>> getChildren() {
return children;
} public void setChildren(List<Tree<T>> children) {
this.children = children;
} public boolean isParent() {
return isParent;
} public void setParent(boolean isParent) {
this.isParent = isParent;
} public boolean isChildren() {
return isChildren;
} public void setChildren(boolean isChildren) {
this.isChildren = isChildren;
} public String getParentId() {
return parentId;
} public void setParentId(String parentId) {
this.parentId = parentId;
} public Tree(String id, String text, String state, boolean checked,
List<Map<String, Object>> attributes, List<Tree<T>> children,
boolean isParent, boolean isChildren, 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;
} public Tree() {
super();
} @Override
public String toString() { return JSON.toJSONString(this);
} }

BuildTree.java

 package pers.kangxu.datautils.common.tree;

 import java.util.ArrayList;
import java.util.List; import pers.kangxu.datautils.bean.tree.Tree; /**
* 构建tree
* TODO
* <br>
* @author kangxu2 2017-1-7
*
*/
public class BuildTree { /**
*
* TODO
* <br>
* @author kangxu2 2017-1-7
*
* @param nodes
* @return
*/
public static <T> Tree<T> build(List<Tree<T>> nodes) { if(nodes == null){
return null;
}
List<Tree<T>> topNodes = new ArrayList<Tree<T>>(); for (Tree<T> children : nodes) { String pid = children.getParentId();
if (pid == null || "".equals(pid)) {
topNodes.add(children); continue;
} for (Tree<T> parent : nodes) {
String id = parent.getId();
if (id != null && id.equals(pid)) {
parent.getChildren().add(children);
children.setParent(true);
parent.setChildren(true); continue;
}
} } Tree<T> root = new Tree<T>();
if (topNodes.size() == 1) {
root = topNodes.get(0);
} else {
root.setId("-1");
root.setParentId("");
root.setParent(false);
root.setChildren(true);
root.setChecked(true);
root.setChildren(topNodes);
root.setText("顶级节点"); } return root;
} }

BuildTreeTester.java

 package pers.kangxu.datautils.test;

 import java.util.ArrayList;
import java.util.List; import pers.kangxu.datautils.bean.tree.Tree;
import pers.kangxu.datautils.common.tree.BuildTree; public class BuildTreeTester { public static void main(String[] args) { List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
List<Test> tests = new ArrayList<Test>();
tests.add(new Test("0", "", "关于本人"));
tests.add(new Test("1", "0", "技术学习"));
tests.add(new Test("2", "0", "兴趣"));
tests.add(new Test("3", "1", "JAVA"));
tests.add(new Test("4", "1", "oracle"));
tests.add(new Test("5", "1", "spring"));
tests.add(new Test("6", "1", "springmvc"));
tests.add(new Test("7", "1", "fastdfs"));
tests.add(new Test("8", "1", "linux"));
tests.add(new Test("9", "2", "骑行"));
tests.add(new Test("10", "2", "吃喝玩乐"));
tests.add(new Test("11", "2", "学习"));
tests.add(new Test("12", "3", "String"));
tests.add(new Test("13", "4", "sql"));
tests.add(new Test("14", "5", "ioc"));
tests.add(new Test("15", "5", "aop"));
tests.add(new Test("16", "1", "等等"));
tests.add(new Test("17", "2", "等等"));
tests.add(new Test("18", "3", "等等"));
tests.add(new Test("19", "4", "等等"));
tests.add(new Test("20", "5", "等等")); for (Test test : tests) {
Tree<Test> tree = new Tree<Test>();
tree.setId(test.getId());
tree.setParentId(test.getPid());
tree.setText(test.getText()); trees.add(tree);
} Tree<Test> t = BuildTree.build(trees);
System.out.println(t);
}
} class Test { private String id;
private String pid;
private String text; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getPid() {
return pid;
} public void setPid(String pid) {
this.pid = pid;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} public Test(String id, String pid, String text) {
super();
this.id = id;
this.pid = pid;
this.text = text;
} public Test() {
super();
} @Override
public String toString() {
return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]";
} }

4、运行结果

JSON数据:

 {
"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":"关于本人"
}

JAVA 根据数据库表内容生产树结构JSON数据的更多相关文章

  1. Java获取数据库表 字段 存储的部分数据

    在浏览器页面,选中图片(可多选) >单击删除按钮. 重点是, 本数据库表TabHeBeiTianQi中 存在 同一id,对应的picLocalPath字段  存储了多张图片,图片地址用   逗号 ...

  2. Java 导出数据库表信息生成Word文档

    一.前言 最近看见朋友写了一个导出数据库生成word文档的业务,感觉很有意思,研究了一下,这里也拿出来与大家分享一波~ 先来看看生成的word文档效果吧 下面我们也来一起简单的实现吧 二.Java 导 ...

  3. 第二百七十八节,MySQL数据库-表内容操作

    MySQL数据库-表内容操作 1.表内容增加 insert into 表 (列名,列名...) values (值,值,值...); 添加表内容添加一条数据 insert into 表 (列名,列名. ...

  4. SQL通过Datatable更新数据库表内容

    SQL通过Datatable更新数据库表内容   //要注意的一点是在Select语句中要选择的列中必须包含主键的列,此外不支持多表连接查询 DataTable dt = new DataTable( ...

  5. MySQL数据库-表内容操作

    1.表内容增加 insert into 表 (列名,列名...) values (值,值,值...); 添加表内容添加一条数据 insert into 表 (列名,列名...) values (值,值 ...

  6. Java 和 Python 解析动态 key 的 JSON 数据

    一.概述 解析JSON过程中,什么情况都可能遇到.遇到特殊的情况,不会怎么办?肯定不是设计的问题,一定是你的姿势不对. 有这样一种JSON需要解析: { "b3444533f6544&quo ...

  7. Oracle 数据库表中已有重复数据添加唯一键(唯一约束)

    Oracle 数据库表中已有重复数据添加唯一键(唯一约束) 问题描述 以 demo 举例,模拟真实场景. 表 TEST_TABLE 有如下字段和数据:id 是主键,code 没有设置键和索引 ID C ...

  8. java通过url调用远程接口返回json数据

    java通过url调用远程接口返回json数据,有用户名和密码验证, 转自 https://blog.csdn.net/wanglong1990421/article/details/78815856 ...

  9. (转)java读取数据库表信息,子段

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

随机推荐

  1. mysql innoDB 挂了的临时解决方案

    Mysql InnoDB: Error: checksum mismatch by Mattias Hemmingsson on December 23, 2013 in Linux • 6 Comm ...

  2. Hibernate框架的基本搭建(一个小的java project的测试向数据库中插入和查询数据的功能)

    Hibernate介绍:Hibernate是一种“对象-关系型数据映射组件”,它使用映射文件将对象(object)与关系型数据(Relational)相关联,在Hibernate中映射文件通常以&qu ...

  3. UIAlertController弹出提示框

    #import "RootViewController.h" #import "RootView.h" #define kColor arc4random() ...

  4. JAVA基础学习day25--Socket基础二-多线程

    一.上传图片 1.1.示例 /* 上传图片 */ import java.net.*; import java.io.*; import java.util.*; import java.text.* ...

  5. 【原】xcode5.0升级5.1遇到的clang: error: unknown argument: '-fobj-arc'错误

    XCODE5.0升到XCODE5.1后LLVM也从5.0升到5.1,工程报下面的错误了: clang: error: unknown argument: '-fobj-arc' [-Wunused-c ...

  6. java网络---再论URL & URI

    关于URL 和URI的关系,在本系列的第二篇:java网络---基本web概念 中已经简述了. 这里重复一点,就是URI包含URL,或者说URI是父类,URL就是子类的概念. 本篇再来详述这2个概念. ...

  7. 评价网站UI--北京邮电大学首页

    首页给人的第一个印象是直观简洁,色调舒适.背景为北邮校内特色建筑美景,令人赏心悦目. 从布局看,结构层次分明.“北京邮电大学”及其校徽清晰明朗却不显突兀.并列着四个大标题:“北邮概况”.“学在北邮”. ...

  8. SAM4E单片机之旅——17、通过UART进行标准IO

    交互还是很有必要的,而且使用键盘和显示器的交互效率还是很高的.当然,可以直接使用UART进行字符的输入和输出.但是又何必浪费了C的标准输入输出的格式控制之类的功能呢? 这次内容就是使用scanf() ...

  9. android之旋转的刻度盘

    这是在学习android的Canvas绘图技巧时做的一个实例.主要用的核心方法就是canvas.save,canvas.rotate, canvas.translate以及canvas.restore ...

  10. 连接Oracle远程数据库错误:ORA-12541,ORA-12514,ORA-01017的解决方法!

    1.出现如下错误:ORA-12541:TNS:no listener,如下图所示: 错误原因是我们没有开启Listener监听器服务,解决方法是在服务中开启这个服务,如下图所示. 2.出现如下错误:O ...