使用递归算法结合数据库解析成Java树形结构的代码解析
作者:Ruthless 时间:2024-01-18 00:11:33
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树形结构的代码解析网站的支持!
来源:http://www.cnblogs.com/linjiqin/archive/2013/06/21/3148066.html
标签:java,树形结构,递归算法
0
投稿
猜你喜欢
抓包工具Fiddler的使用方法详解(Fiddler中文教程)
2022-09-21 03:34:55
JS自定义混合Mixin函数示例
2024-04-16 10:29:41
python 密码验证(滑块验证)
2021-01-24 02:32:18
Go语言基础知识点介绍
2024-04-26 17:22:50
numpy给array增加维度np.newaxis的实例
2023-06-30 06:41:34
Python之如何调整图片的文件大小
2022-09-27 04:57:50
python如何从键盘获取输入实例
2021-02-21 07:21:47
javascript 常用功能总结
2023-07-02 03:50:17
JavaScript实现省市联动过程中bug的解决方法
2024-04-19 10:46:05
数据结构简明备忘录 线性表
2024-01-25 01:59:28
SQL SERVER的数据类型
2024-01-20 03:40:41
python抓取百度首页的方法
2023-05-25 13:31:58
Python动态配置管理Dynaconf的实现示例详解
2021-01-24 19:35:06
MySQL 如何修改root用户的密码
2024-01-23 19:34:31
python 遍历字符串(含汉字)实例详解
2022-10-06 05:08:58
python交互式图形编程实例(一)
2022-11-12 14:44:53
记录微信小程序 height: calc(xx - xx);无效问题
2024-05-02 16:27:16
微信小程序访问mysql数据库流程详解
2024-01-23 10:34:43
python time()的实例用法
2022-01-09 02:33:02
PHP登录环节防止sql注入的方法浅析
2023-07-21 06:41:01