树形结构数据的存储与遍历

1.关系图
在这里插入图片描述
2.数据库表t_company及对应实体类Company.class
在这里插入图片描述

package com.zhl.model;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@Data
@Table(name="t_company")
public class Company {
	
	@Id
	@GeneratedValue(generator="JDBC")
    private Long id;

    private String name;

    private Long parentId;

}

3.树状图封装实体类TreeBo.class

package com.zhl.common.bo;

import java.util.List;

import lombok.Data;

@Data
public class TreeBo {

	private Long id;
	private String name;
	private Long pid;
	private List<TreeBo> children;
	
	public TreeBo() {
		super();
	}
	
	public TreeBo(Long id, String name, Long pid, List<TreeBo> children) {
		super();
		this.id = id;
		this.name = name;
		this.pid = pid;
		this.children = children;
	}
	
	public TreeBo(Long id, String name, Long pid) {
		super();
		this.id = id;
		this.name = name;
		this.pid = pid;
	}

}

4.树状图工具类(核心)TreeUtil.class

package com.zhl.utils;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhl.common.bo.TreeBo;

public class TreeUtil {

	private List<TreeBo> rootList; // 根节点对象存放到这里
	private List<TreeBo> bodyList; // 其他节点存放到这里,可以包含根节点

	public TreeUtil(List<TreeBo> rootList, List<TreeBo> bodyList) {
		this.rootList = rootList;
		this.bodyList = bodyList;
	}

	public List<TreeBo> getTree() { // 调用的方法入口
		if (bodyList != null && !bodyList.isEmpty()) {
			// 声明一个map,用来过滤已操作过的数据
			Map<Long, Long> map = Maps.newHashMapWithExpectedSize(bodyList.size());
			rootList.forEach(beanTree -> getChild(beanTree, map));
			return rootList;
		}
		return null;
	}

	private void getChild(TreeBo beanTree, Map<Long, Long> map) {
		List<TreeBo> childList = Lists.newArrayList();
		bodyList.stream().filter(c -> !map.containsKey(c.getId())).filter(c -> c.getPid().equals(beanTree.getId()))
				.forEach(c -> {
					map.put(c.getId(), c.getPid());
					getChild(c, map);
					childList.add(c);
				});
		beanTree.setChildren(childList);
	}

}

5.业务层将表数据封装并调用树状图工具类

public List<TreeBo> listTree() {
    /** 获取所有表数据*/
    List<Company> companyList = companyMapper.selectAll();
    List<TreeBo> rootList = new ArrayList<>();
    List<TreeBo> bodyList = new ArrayList<>();
    for (Company company : companyList) {
        if (company.getParentId()==null) {
            /** 根结点列表*/
            rootList.add(new TreeBo(company.getId(), company.getName(), company.getParentId()));
        }else {
            /** 其他结点列表(可以包含根结点)*/
            bodyList.add(new TreeBo(company.getId(), company.getName(), company.getParentId()));
        }
    }
    TreeUtil treeUtil = new TreeUtil(rootList, bodyList);
    return treeUtil.getTree();
}

6.返回数据结果

[{
	"id": 1,
	"name": "总集团",
	"pid": null,
	"children": [{
		"id": 2,
		"name": "子集团1",
		"pid": 1,
		"children": [{
			"id": 4,
			"name": "公司1",
			"pid": 2,
			"children": []
		}, {
			"id": 6,
			"name": "公司3",
			"pid": 2,
			"children": []
		}, {
			"id": 7,
			"name": "公司4",
			"pid": 2,
			"children": []
		}, {
			"id": 9,
			"name": "公司2",
			"pid": 2,
			"children": []
		}]
	}, {
		"id": 3,
		"name": "子集团2",
		"pid": 1,
		"children": [{
			"id": 5,
			"name": "公司2",
			"pid": 3,
			"children": []
		}, {
			"id": 8,
			"name": "公司5",
			"pid": 3,
			"children": []
		}]
	}]
}]
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值