前言
开发中经常会遇到树状结果的数据,例如开发权限管理模块的时候,菜单表就是树状的数据结构。遇到这种数据的时候,我们怎么正确快速的返回相对应的数据树。
Controller层
@GetMapping("initialize/selectAllManMenuList") @LogAnnotation(title = "菜单管理", action = "查询菜单列表", operation = "查询") @ApiOperation(value = "查询菜单列表", notes = "树形菜单结构,包含菜单所有层级", produces = "application/json") public Response<List<SelectAllManMenuVo>> selectAllManMenuList() { return Response.data(manMenuService.selectAllManMenuList()); }
Service层
public List<SelectAllManMenuVo> selectAllManMenuList() { //查询菜单所有列表 List<SelectAllManMenuVo> manUsers = classCopyList(manMenuDao.selectList(Wrappers.<ManMenu>lambdaQuery()), SelectAllManMenuVo.class); return getParent(manUsers); } /** * 获取父类节点(递归算法) * * @param selectAllManMenuVos 菜单集合 * @return 树形节点数据 */ public List<SelectAllManMenuVo> getParent(List<SelectAllManMenuVo> selectAllManMenuVos) { if (CollectionUtils.isEmpty(selectAllManMenuVos)) { return null; } List<SelectAllManMenuVo> trees = new ArrayList<>(); for (SelectAllManMenuVo selectAllManMenuVo : selectAllManMenuVos) { if (0 == selectAllManMenuVo.getParent()) { trees.add(findChildren(selectAllManMenuVo, selectAllManMenuVos)); } } return trees; } /** * 获取子类节点(递归算法) * * @param father * @param selectAllManMenuVos 菜单集合 * @return 子树节点数据 */ public SelectAllManMenuVo findChildren(SelectAllManMenuVo father, List<SelectAllManMenuVo> selectAllManMenuVos) { for (SelectAllManMenuVo children : selectAllManMenuVos) { if (father.getId().equals(children.getParent())) { father.getChild().add(findChildren(children, selectAllManMenuVos)); } } return father; }
实体类
package cn.logicalthinking.web.service.manage.entity.vo; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.experimental.Accessors; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 查询菜单列表 * * @author summer.chou * @version 1.0 * @date 2020/11/30 10:14 */ @Data @ApiModel(value = "查询菜单分层列表") @Accessors(chain = true) public class SelectAllManMenuVo { private static final long serialVersionUID = 1L; @TableId @ApiModelProperty(value = "主键ID") private Integer id; @ApiModelProperty(value = "标题") private String title; @ApiModelProperty(value = "图标") private String icon; @ApiModelProperty(value = "展开(true/false)") private Boolean spread; @ApiModelProperty(value = "状态(true/false)") private Boolean status; @ApiModelProperty(value = "路径") private String href; @ApiModelProperty(value = "目标") private String target; @ApiModelProperty(value = "父级ID") private Integer parent; @ApiModelProperty(value = "级别(0页面 1按钮)") private Integer type; @ApiModelProperty(value = "排序") private Integer rank; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+0") @ApiModelProperty(value = "更新时间") private Date turnoverTime; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+0") @ApiModelProperty(value = "创建时间") private Date creationTime; @TableField(exist = false) @ApiModelProperty(value = "子级") private List<SelectAllManMenuVo> child =new ArrayList<>(); }
测试结果