根据父级id查询父级下的所有子级(递归算法)
一、问题:
这里的递归主要用来做,根据父级id查询父级下的所有子级,返回list集合,并放到分页中,分页使用的是,mybatis-plus的插件。
①递归代码
public void getRoleIdChildren(List<AuthRole> roleList,List<AuthRole> resultList,AuthRole role){
for(AuthRole roleItem : roleList) {
if(roleItem.getPid().equals(role.getId())) {
resultList.add(roleItem);
getRoleIdChildren(roleList,resultList,roleItem);
}
}
}
② 使用
resultList要new在外面,到时候给我们的分页赋值
List<AuthRole> resultList=new ArrayList<>();
if (StringUtils.hasText(data.getId())) {
AuthRole role = this.getById(data.getId());
resultList.add(role);
getRoleIdChildren(this.list(), resultList,role);
}
③放入到分页中
Page<AuthRole> pageDate = this.baseMapper.selectPage(page, queryWrapper);
if (data.getId()!=null && data.getId()!=""){
pageDate.setRecords(roleList);
}