java根据数据库表内容生产树结构json数据的方法
程序员文章站
2024-03-08 14:37:10
1、利用场景
组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段
2、构造数据(以下数据并不是组织机构数据,而纯...
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() == 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("*节点"); } 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": 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": "*节点" }
以上就是小编为大家带来的java根据数据库表内容生产树结构json数据的方法全部内容了,希望大家多多支持~