java从数据库读取菜单,递归生成菜单树

5 篇文章 0 订阅
2 篇文章 0 订阅

实体类

public class ItimDoucumentManager {
private String id;
private String delFlag;
private String remark;
private String createBy;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
private String updateBy;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
private String lable;
private String parentId;
private String sort;
private List<ItimDoucumentManager> children;

@Override
public String getId() {
        return id;
}

@Override
public void setId(String id) {
        this.id = id;
}

@Override
public String getDelFlag() {
        return delFlag;
}

@Override
public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
}

@Override
public String getRemark() {
        return remark;
}

@Override
public void setRemark(String remark) {
        this.remark = remark;
}

@Override
public String getCreateBy() {
        return createBy;
}

@Override
public void setCreateBy(String createBy) {
        this.createBy = createBy;
}

@Override
public Date getCreateTime() {
        return createTime;
}

@Override
public void setCreateTime(Date createTime) {
        this.createTime = createTime;
}

@Override
public String getUpdateBy() {
        return updateBy;
}

@Override
public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
}

@Override
public Date getUpdateTime() {
        return updateTime;
}

@Override
public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
}

public String getLable() {
        return lable;
}

public void setLable(String lable) {
        this.lable = lable;
}

public String getParentId() {
        return parentId;
}

public void setParentId(String parentId) {
        this.parentId = parentId;
}

public String getSort() {
        return sort;
}

public void setSort(String sort) {
        this.sort = sort;
}

public List<ItimDoucumentManager> getChildren() {
        return children;
}

public void setChildren(List<ItimDoucumentManager> children) {
        this.children = children;
}
}
@Service
public List<ItimDoucumentManager> selectAllList(List<ItimDoucumentManager> itimDoucumentManagerList) {
    List<ItimDoucumentManager> itimDoucumentManagers = new ArrayList<>();
    for (int i = 0; i < itimDoucumentManagerList.size(); i++) {
        ItimDoucumentManager itimDoucumentManager = itimDoucumentManagerList.get(i);
        //如果父级为0,则是顶级菜单
        if (itimDoucumentManager.getParentId().equals("0")) {
            itimDoucumentManagers.add(itimDoucumentManager);
        }
    }
    for (ItimDoucumentManager manager : itimDoucumentManagerList) {
         manager.setChildren(getChild(manager.getId(), itimDoucumentManagerList));
    }
    return itimDoucumentManagers;
}


public List<ItimDoucumentManager> getChild(String id, List<ItimDoucumentManager> list) {
    // 子菜单
    List<ItimDoucumentManager> childList = new ArrayList<>();
    for (ItimDoucumentManager manager : list) {
        // 遍历所有节点,将父菜单id与传过来的id比较
        if (StringUtils.isNotBlank(manager.getParentId())) {
            if (manager.getParentId().equals(id)) {
                childList.add(manager);
            }
        }
    }
    // 把子菜单的子菜单再循环一遍
    for (ItimDoucumentManager rootMenu : childList) {//
        if (StringUtils.isBlank(rootMenu.getParentId())) {
            // 递归
            rootMenu.setChildren(getChild(rootMenu.getId(), list));
        }
    } // 递归退出条件
    if (childList.size() == 0) {
        return null;
    }
    return childList;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhh1996075

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值