只有一个根节点数据结构:
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
}
有多个根节点的树形结构:
package com.forchain.platform.model;
import javax.persistence.*;
@Table(name = "tb_tree")
public class TbTree {
@Id
private Long cid;
@Column(name = "CNAME")
private String cname;
@Column(name = "PID")
private Long pid;
/**
* @return CID
*/
public Long getCid() {
return cid;
}
/**
* @param cid
*/
public void setCid(Long cid) {
this.cid = cid;
}
/**
* @return CNAME
*/
public String getCname() {
return cname;
}
/**
* @param cname
*/
public void setCname(String cname) {
this.cname = cname;
}
/**
* @return PID
*/
public Long getPid() {
return pid;
}
/**
* @param pid
*/
public void setPid(Long pid) {
this.pid = pid;
}
}
/**
* author chong
* 创建日期: 2019/2/26
* 创建时间: 12:35
*/
@Getter
@Setter
public class TreeNode extends TbTree implements Serializable {
List> nodeList = new ArrayList<>();
}
package com.forchain.platform.mapper;
import com.forchain.platform.config.mapper.BaseMapper;
import com.forchain.platform.model.TbTree;
import com.forchain.platform.model.TreeNode;
import java.util.List;
public interface TbTreeMapper extends BaseMapper {
List queryTreeNode(Long pId);
}
create table tb_tree
(
CID BIGINT not null,
CNAME VARCHAR(50),
PID BIGINT
)
insert into tb_tree (CID, CNAME, PID) values (1, '中国', NULL);
insert into tb_tree (CID, CNAME, PID) values (2, '北京市', NULL);
insert into tb_tree (CID, CNAME, PID) values (3, '广东省', NULL);
insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);
insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 1);
insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 1);
insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 1);
insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 2);
insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 2);
insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 3);
insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 3);
insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 3);
insert into tb_tree (CID, CNAME, PID) values (13, '浦东22', 3);
insert into tb_tree (CID, CNAME, PID) values (14, '浦东23', 4);
insert into tb_tree (CID, CNAME, PID) values (15, '浦东24', 4);
insert into tb_tree (CID, CNAME, PID) values (16, '浦东25', 6);
insert into tb_tree (CID, CNAME, PID) values (17, '浦东25', 6);
insert into tb_tree (CID, CNAME, PID) values (18, '浦东25', 6);
@RunWith(SpringRunner.class)
@SpringBootTest
public class BcHrPlatformTest {
@Resource
TbTreeMapper tbTreeMapper;
@Test
public void loadTree() {
System.err.println(JSONObject.toJSONString(recursiveTree(Long.parseLong(String.valueOf(0)))));
}
/**
* 递归算法解析成树形结构
*/
public List recursiveTree(Long pId) {
List treeNodes = tbTreeMapper.queryTreeNode(pId);
for (TreeNode node:treeNodes) {
List treeNodes1 = recursiveTree(node.getCid());
if (!treeNodes1.isEmpty()) {
node.getNodeList().add(treeNodes1);
}
}
return treeNodes;
}
}
[{
"cid": 1,
"cname": "中国",
"nodeList": [
[{
"cid": 4,
"cname": "上海市",
"nodeList": [
[{
"cid": 14,
"cname": "浦东23",
"nodeList": [],
"pid": 4
}, {
"cid": 15,
"cname": "浦东24",
"nodeList": [],
"pid": 4
}]
],
"pid": 1
}, {
"cid": 5,
"cname": "广州市",
"nodeList": [],
"pid": 1
}, {
"cid": 6,
"cname": "深圳市",
"nodeList": [
[{
"cid": 16,
"cname": "浦东25",
"nodeList": [],
"pid": 6
}, {
"cid": 17,
"cname": "浦东25",
"nodeList": [],
"pid": 6
}, {
"cid": 18,
"cname": "浦东25",
"nodeList": [],
"pid": 6
}]
],
"pid": 1
}, {
"cid": 7,
"cname": "海珠区",
"nodeList": [],
"pid": 1
}]
],
"pid": 0
}, {
"cid": 2,
"cname": "北京市",
"nodeList": [
[{
"cid": 8,
"cname": "天河区",
"nodeList": [],
"pid": 2
}, {
"cid": 9,
"cname": "福田区",
"nodeList": [],
"pid": 2
}]
],
"pid": 0
}, {
"cid": 3,
"cname": "广东省",
"nodeList": [
[{
"cid": 10,
"cname": "南山区",
"nodeList": [],
"pid": 3
}, {
"cid": 11,
"cname": "密云县",
"nodeList": [],
"pid": 3
}, {
"cid": 12,
"cname": "浦东",
"nodeList": [],
"pid": 3
}, {
"cid": 13,
"cname": "浦东22",
"nodeList": [],
"pid": 3
}]
],
"pid": 0
}]