java把存在上下级关系数据转换为 json 树

数据库表结构如下:

public class MiniProgramAction {

    @TableId
    private Integer id;
    private Integer parentId = 0;
    private String actionName = ""; // 动作名称
    private String icon = ""; // 图标

    private String href = ""; // 链接地址
    private Integer sort = 0;

    private Byte type; // 菜单类型 0首页,1 应用

    public MiniProgramAction() {

    }

    public MiniProgramAction(Integer parentId, String actionValue, String actionName, Integer sort) {
        this.parentId = parentId;
        this.actionValue = actionValue;
        this.actionName = actionName;
        this.sort = sort;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParentId() {
        return parentId;
    }

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

    public String getActionName() {
        return actionName;
    }

    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public Integer getSort() {
        return sort;
    }

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

    public String getActionValue() {
        return actionValue;
    }

    public void setActionValue(String actionValue) {
        this.actionValue = actionValue;
    }

    public Byte getType() {
        return type;
    }

    public void setType(Byte type) {
        this.type = type;
    }
}

业务数据

public class GetMenusVo {

    private long id; // 菜单ID
    private String mname; // 菜单名称
    private String icon; // 菜单图标
    private String href;
    private long parentId; // 父菜单ID
    private byte type; // 菜单类型 0:首页 1 应用

    public List<GetMenusVo> subs; // 子菜单列表

    public GetMenusVo(long id, String mname, String icon, String href, long parentId, byte type) {
        this.id = id;
        this.mname = mname;
        this.icon = icon;
        this.href = href;
        this.parentId = parentId;
        this.type = type;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public long getParentId() {
        return parentId;
    }

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

    public byte getType() {
        return type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public List<GetMenusVo> getSubs() {
        return subs;
    }

    public void setSubs(List<GetMenusVo> subs) {
        this.subs = subs;
    }
}

Java 业务处理

    @Override
    public List<GetMenusVo> getMenus() {
        int roleId = PmasThreadLocalUtil.get().getRoleId(); // 获取当前用户的角色id

        // 获取角色授权菜单
        List<MiniProgramAction> menuList = miniProgramActionMapper.findByRoleId(roleId);
        List<GetMenusVo> menus = menuList.stream().map(e -> new GetMenusVo(e.getId(), e.getActionName(), e.getIcon(), e.getHref(),
                e.getParentId(), e.getType())).collect(Collectors.toList());

        //获取父节点
        return menus.stream().filter(m -> m.getParentId() == 0).peek(m -> m.setSubs(getSubs(m, menus))).collect(Collectors.toList());
    }

    /**
     * 查询子节点
     */
    private List<GetMenusVo> getSubs(GetMenusVo root, List<GetMenusVo> menus) {
        return menus.stream().filter(m -> {
            return Objects.equals(m.getParentId(), root.getId());
        }).peek(m -> m.setSubs(getSubs(m, menus))).collect(Collectors.toList());
    }

controller

    /**
     *  获取应用菜单
     */
    @PostMapping("/getMenus")
    public List<GetMenusVo> getMenus() {
        return pmasUserService.getMenus();
    }

调用响应:

[{"id":1000,"mname":"首页","href":null,"parentId":0,"subs":[]},{"id":2000,"mname":"概览","href":null,"parentId":0,"subs":[{"id":2001,"mname":"收入概览","href":null,"parentId":2000,"subs":[]},{"id":2002,"mname":"车流概览","href":null,"parentId":2000,"subs":[]},{"id":2003,"mname":"固定车辆概览","href":null,"parentId":2000,"subs":[]},{"id":2004,"mname":"监控","href":null,"parentId":2000,"subs":[]}]},{"id":3000,"mname":"应用","href":null,"parentId":0,"subs":[{"id":3001,"mname":"车辆","href":null,"parentId":3000,"subs":[]},{"id":3002,"mname":"车辆","href":null,"parentId":3000,"subs":[]},{"id":3003,"mname":"车主","href":null,"parentId":3000,"subs":[]}]}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

木小百99

听说打赏的人都发财了

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

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

打赏作者

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

抵扣说明:

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

余额充值