Java 利用遞歸算法獲取樹狀結構的菜單列表


 前言
開發中經常會遇到樹狀結果的數據,例如開發權限管理模塊的時候,菜單表就是樹狀的數據結構。遇到這種數據的時候,我們怎么正確快速的返回相對應的數據樹。

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<>();


}

測試結果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM