数据库表结构
实体类基类
@Getter
@Setter
@ToString
public class RecursionBean {
/**
* 编号
*/
private Long id;
/**
* 父权限ID,根节点的父权限为空
*/
@JsonIgnore
private Long pid;
private List<? extends RecursionBean> children;
/**
* 递归查询子节点
*
* @param root 根节点
* @param list 所有节点
* @return 根节点信息
*/
private static <T extends RecursionBean> List<T> getChildren(T root, List<T> list) {
final List<T> res = new ArrayList<>();
for (T item : list) {
if (Objects.equals(item.getPid(), root.getId())) {
item.setChildren(getChildren(item, list));
res.add(item);
}
}
return res;
}
public static <T extends RecursionBean> List<T> getRecursionList(List<T> list) {
final List<T> res = new ArrayList<>();
for (T item : list) {
if (item.getPid() == null) {
item.setChildren(getChildren(item, list));
res.add(item);
}
}
return res;
}
}
VO类
@Getter
@Setter
@ToString(callSuper = true)
public class PermissionVO extends RecursionBean {
/**
* 权限名称
*/
private String title;
/**
* 权限编码
*/
private String code;
/**
* 权限所对应的图标
*/
private String icon;
/**
* 菜单类型: 1菜单 2按钮
*/
private Integer type;
/**
* 权限所能访问的资源的路径
*/
private String path;
/**
* 是否为外部链接
*/
private Boolean external;
/**
* 删除标识:0删除 1未删除
*/
private Boolean deleted;
}
调用代码
@GetMapping("/v1/list")
@LogAnno(value = "权限分页", module = "Permission模块", opt = OptEnum.SELECT)
public ResultBean<List<PermissionVO>> list() {
// 将Query对象转换成QueryWrapper对象
QueryWrapper<Permission> queryWrapper = QueryWrapperUtil.query2wrapper(permissionQuery);
List<Permission> permissionList = permissionService.list(queryWrapper);
final List<PermissionVO> permissionVOList = permissionConverter.permissionList2permissionVOList(permissionList);
final List<PermissionVO> list = RecursionBean.getRecursionList(permissionVOList);
System.out.println(JsonUtil.obj2string(list));
return ResultBeanUtil.success(list);
}