利用java+mysql递归实现拼接树形JSON列表的方法示例
程序员文章站
2023-12-22 09:15:21
前言
本文给大家介绍的是关于利用java+mysql递归实现拼接树形json列表的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:
我们在做java...
前言
本文给大家介绍的是关于利用java+mysql递归实现拼接树形json列表的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:
我们在做java web项目时,前端控件例如国家-省-市-区-县等树形列表,常常需要多级树形json数据
例如:
[ { "name": "商品目录", "pid": "-1", "id": "1", "children": [ { "name": "日用品", "pid": "1", "id": "11", "children": [ { "name": "洗发水", "pid": "11", "id": "111", "children": [ { "name": "霸王", "pid": "111", "id": "1111", "children": [] } ] } ] }, { "name": "食品", "pid": "1", "id": "12", "children": [] } ] } ]
整体思路分为两步,第一步获取目录及其所有子目录,获取后的列表形式如下:
[ {"id":"1","pid":"-1","name":"商品目录"}, {"id":"11","pid":"1","name":"日用品"}, {"id":"12","pid":"1","name":"食品"}, {"id":"111","pid":"11","name":"洗发水"}, {"id":"1111","pid":"111","name":"霸王"} ]
第二步,利用递归思想拼装该数据,拼装方法的工具类如下:
package *.*.*; import net.sf.json.jsonarray; import java.util.arraylist; import java.util.list; /** * 构造目录json树 * created by fukang on 2017/5/26 0026. */ public class treebuilder { list<node> nodes = new arraylist<>(); public string buildtree(list<node> nodes) { treebuilder treebuilder = new treebuilder(nodes); return treebuilder.buildjsontree(); } public treebuilder() { } public treebuilder(list<node> nodes) { super(); this.nodes = nodes; } // 构建json树形结构 public string buildjsontree() { list<node> nodetree = buildtree(); jsonarray jsonarray = jsonarray.fromobject(nodetree); return jsonarray.tostring(); } // 构建树形结构 public list<node> buildtree() { list<node> treenodes = new arraylist<>(); list<node> rootnodes = getrootnodes(); for (node rootnode : rootnodes) { buildchildnodes(rootnode); treenodes.add(rootnode); } return treenodes; } // 递归子节点 public void buildchildnodes(node node) { list<node> children = getchildnodes(node); if (!children.isempty()) { for (node child : children) { buildchildnodes(child); } node.setchildren(children); } } // 获取父节点下所有的子节点 public list<node> getchildnodes(node pnode) { list<node> childnodes = new arraylist<>(); for (node n : nodes) { if (pnode.getid().equals(n.getpid())) { childnodes.add(n); } } return childnodes; } // 判断是否为根节点 public boolean rootnode(node node) { boolean isrootnode = true; for (node n : nodes) { if (node.getpid().equals(n.getid())) { isrootnode = false; break; } } return isrootnode; } // 获取集合中所有的根节点 public list<node> getrootnodes() { list<node> rootnodes = new arraylist<>(); for (node n : nodes) { if (rootnode(n)) { rootnodes.add(n); } } return rootnodes; } public static class node { private string id; private string pid; private string name; private list<node> children; public node() { } public node(string id, string pid, string name) { super(); this.id = id; this.pid = pid; this.name = name; } 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 getname() { return name; } public void setname(string name) { this.name = name; } public list<node> getchildren() { return children; } public void setchildren(list<node> children) { this.children = children; } } }
在controller中的调用方法是:
@requestmapping("/bulidjsontree") @responsebody public string buildjsontree(httpservletrequest request) { // 获取全部目录节点 list<node> nodes = igoodsdirsvc.getalldirlist(); // 拼装树形json字符串 string json = new treebuilder().buildtree(nodes); return json; }
其中igoodsdirsvc.getalldirlist()
方法需要将取到的数据转为node类型:
string hql = "select id as id,pid as pid,name as name from directory"; query query = factory.getcurrentsession().createquery(hql) .setresulttransformer(transformers.aliastobean(treebuilder.node.class)); return query.list();
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。