机构树,数据表格里面有parentId这种,通过递归查询机构树
结果类似于
实体:通过list存储子菜单
@Data
public class OrganizationVo{
private String fatherName;
private Integer id;
private String name;
private Integer fatherId;
private Date createTime;
private Date updateTime;
/**
* 是否叶子节点:0否、1是
*/
private Integer isLeaf;
private static final long serialVersionUID = 1L;
private List<CustomerOrganizationVo> child;
}
通过递归查询子机构
@Override
public OrganizationVo departmentTree(Integer id) {
OrganizationVo OrganizationVoTree = new OrganizationVo();
//找到最顶级菜单
Organization departmentFather = organizationMapper.selectByPrimaryKey(1);
OrganizationVoTree.setId(departmentFather.getId());
OrganizationVoTree.setName(departmentFather.getName());
OrganizationVoTree.setFatherId(departmentFather.getFatherId());
OrganizationVoTree.setChild(getDepartmentChild(departmentFather));
return OrganizationVoTree;
}
private List<OrganizationVo> getDepartmentChild(Organization father) {
List<OrganizationVo> tree = new LinkedList<>();
if (father.getIsLeaf() == 0) {
//通过当前机构的id,找到他的所有的字机构,这里使用Example
OrganizationExample example = new OrganizationExample();
example.createCriteria().andFatherIdEqualTo(father.getId());
List<Organization> OrganizationList = organizationMapper.selectByExample(example);
for (Organization organization : OrganizationList) {
OrganizationVo organizationVo = new OrganizationVo();
BeanUtils.copyProperties(organization, organizationVo);
organizationVo.setFatherId(father.getId());
organizationVo.setFatherName(father.getName());
//递归查询子结构
organizationVo.setChild(getDepartmentChild(organization));
tree.add(organizationVo);
}
}
return tree;
}