欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用递归算法结合数据库解析成Java树形结构的代码解析

程序员文章站 2024-02-25 08:06:46
1、准备表结构及对应的表数据 a、表结构: create table tb_tree ( cid number not null, cname varch...

1、准备表结构及对应的表数据

a、表结构:

create table tb_tree
(
cid number not null,
cname varchar2(50),
pid number //父节点
)

b、表数据:

insert into tb_tree (cid, cname, pid) values (1, '中国', 0);
insert into tb_tree (cid, cname, pid) values (2, '北京市', 1);
insert into tb_tree (cid, cname, pid) values (3, '广东省', 1);
insert into tb_tree (cid, cname, pid) values (4, '上海市', 1);
insert into tb_tree (cid, cname, pid) values (5, '广州市', 3);
insert into tb_tree (cid, cname, pid) values (6, '深圳市', 3);
insert into tb_tree (cid, cname, pid) values (7, '海珠区', 5);
insert into tb_tree (cid, cname, pid) values (8, '天河区', 5);
insert into tb_tree (cid, cname, pid) values (9, '福田区', 6);
insert into tb_tree (cid, cname, pid) values (10, '南山区', 6);
insert into tb_tree (cid, cname, pid) values (11, '密云县', 2);
insert into tb_tree (cid, cname, pid) values (12, '浦东', 4);

2、treenode对象,对应tb_tree

public class treenode implements serializable {
private integer cid;
private string cname;
private integer pid;
private list nodes = new arraylist();
public treenode() {
}
//getter、setter省略
}

3、测试数据

public class treenodetest {
@test
public void loadtree() throws exception{
system.out.println(jsonutils.javatojson(recursivetree(1)));
}
/**
* 递归算法解析成树形结构
*
* @param cid
* @return
* @author jiqinlin
*/
public treenode recursivetree(int cid) {
//根据cid获取节点对象(select * from tb_tree t where t.cid=?)
treenode node = personservice.getreenode(cid);
//查询cid下的所有子节点(select * from tb_tree t where t.pid=?)
list childtreenodes = personservice.querytreenode(cid); 
//遍历子节点
for(treenode child : childtreenodes){
treenode n = recursivetree(child.getcid()); //递归
node.getnodes().add(n);
}
return node;
}
}

输出的json格式如下:

{
  "cid": 1,
  "nodes": [
    {
      "cid": 2,
      "nodes": [
        {
          "cid": 11,
          "nodes": [
          ],
          "cname": "密云县",
          "pid": 2
        }
      ],
      "cname": "北京市",
      "pid": 1
    },
    {
      "cid": 3,
      "nodes": [
        {
          "cid": 5,
          "nodes": [
            {
              "cid": 7,
              "nodes": [
              ],
              "cname": "海珠区",
              "pid": 5
            },
            {
              "cid": 8,
              "nodes": [
              ],
              "cname": "天河区",
              "pid": 5
            }
          ],
          "cname": "广州市",
          "pid": 3
        },
        {
          "cid": 6,
          "nodes": [
            {
              "cid": 9,
              "nodes": [
              ],
              "cname": "福田区",
              "pid": 6
            },
            {
              "cid": 10,
              "nodes": [
              ],
              "cname": "南山区",
              "pid": 6
            }
          ],
          "cname": "深圳市",
          "pid": 3
        }
      ],
      "cname": "广东省",
      "pid": 1
    },
    {
      "cid": 4,
      "nodes": [
        {
          "cid": 12,
          "nodes": [
          ],
          "cname": "浦东",
          "pid": 4
        }
      ],
      "cname": "上海市",
      "pid": 1
    }
  ],
  "cname": "中国",
  "pid": 0
}

总结

以上所述是小编给大家介绍的使用递归算法结合数据库解析成java树形结构的代码解析,希望对大家有所帮助