运用到了递归得思想
/**
* @author xc
*/
@Data
public class PermissionResNodeTreeVo {
@ApiModelProperty(value = "权限id")
private Long id;
@ApiModelProperty(value = "菜单权限名称")
private String title;
@ApiModelProperty(value = "图标")
private String icon;
@ApiModelProperty(value = "地址")
private String path;
@ApiModelProperty(value = "名称")
private String name;
/**
* ! 重点:List 集合就是实体类本身
*/
@ApiModelProperty(value = "子集")
private List<PermissionResNodeTreeVo> children;
}
树状递归方法
/**
* 递归设置级别,用于 添加/编辑 所属菜单树结构数据
*
* @param categoryEntities
* @param pid
* @param index
* @return
*/
private static List<CategoryResVo> categoryDirectory(
List<CategoryEntity> categoryEntities,
Integer pid,
Integer index
){
List<CategoryResVo> list = new ArrayList<>();
for (int i = index;i < categoryEntities.size();i++){
CategoryEntity categoryEntity = categoryEntities.get(i);
if (categoryEntity.getId().equals(pid)){
CategoryResVo vo = new CategoryResVo(
categoryEntity.getId(),categoryEntity.getName(),new ArrayList<>()
);
vo.setChildren(categoryDirectory(categoryEntities,categoryEntity.getId(),i + 1));
list.add(vo);
}
}
return list;
}