最近遇到了一个经常用的需求,递归获取部门及其子部门,代码记录一下,方便以后直接拿来用
//DeptDto是一个部门的封装类,用于承载部门及其子部门的数据
private DeptDto getDeptChildren(DeptDto deptDto){
if (deptDto == null) return null;
//获取子部门,对应数据库中查询语句
List<TDept> tDepts = getdeptList();
if (tDepts != null){
List<DeptDto> collect = tDepts.stream()
.map(i -> new DeptDto(i.getId(), i.getDeptName(), i.getDeptNo(), null))
.collect(Collectors.toList());
deptDto.setChildren(collect);
for (DeptDto ddd : collect) {
getDeptChildren(ddd);
}
}
return deptDto;
}