之前发布过一个前端树形结构,现在补充上java代码
首先我写了一个工具类
package com.athena.organization.Utils;
import com.athena.organization.clientVO.TreeOrganization;
import com.athena.organization.pojo.SysOrganization;
import com.google.common.collect.Lists;
import java.util.List;
/**
* @Author 王发扬
* @{Date} {Time}
*/
public class TreeUtils {
public static List<TreeOrganization> recursiveOrganizationTree(Long pid, List<SysOrganization> sysOrganizationList){
List<TreeOrganization> organizationList = Lists.newArrayList();
sysOrganizationList.forEach(p -> {
if (pid .equals(p.getParentId())){
//继续深入递归
List<TreeOrganization> children = recursiveOrganizationTree(p.getId(),sysOrganizationList);
TreeOrganization organization = new TreeOrganization();
organization.setId(p.getId());
organization.setLabel(p.getOrgName());
organization.setCreateTime(p.getCreateTime());
organization.setParentId(p.getParentId());
organization.setStatus(p.getStatus());
organization.setChildren(children);
organizationList.add(organization);
}
});
return organizationList;
}
}
然后直接查询出数据库的所有数据传入工具类即可,