教你使用Java实现树形菜单对象 实例代码解析

退役熬夜选手 2021-08-18 11:39:16 浏览数 (2261)
反馈

本文实例为大家分享了java实现树形菜单对象的具体代码,供大家参考,具体内容如下

1、SysMenu

package com.zy.shiro.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * sys_menu
 * @author 
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="SysMenu对象", description="菜单管理")
public class SysMenu implements Serializable {
    @TableId(value = "menu_id", type = IdType.AUTO)
    private Long menuId;

    /**
     * 父菜单id,父菜单为0
     */
    private Long parentId;

    /**
     * 菜单名称
     */
    private String menuName;

    /**
     * 菜单url
     */
    private String url;

    /**
     * 授权(多个用逗号分隔,如:user:list,user:create)
     */
    private String permisions;

    /**
     * 类型   0:目录   1:菜单   2:按钮
     */
    private Integer menuType;

    /**
     * 菜单图标
     */
    private String icon;

    /**
     * 排序
     */
    private Integer orderNum;

    @TableField(exist = false)
    private List<SysMenu> list;

//    @JsonInclude(value = JsonInclude.Include.NON_EMPTY)
//    private List<SysMenu> child=new ArrayList<>();

    private static final long serialVersionUID = 1L;


}

2、TreeNode

package com.zy.common.tree;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import sun.reflect.generics.tree.Tree;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class TreeNode implements Serializable {

    private Integer id;
    private Integer pid;
    private String title;
    private String icon;
    private String href;
    private String target;
    private Boolean spread;
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY)
    private List<TreeNode> child=new ArrayList<>();

    /**
     * 登陆成功之后系统主而需要的菜单树的json对象构造器
     * @param id
     * @param pid
     * @param title
     * @param icon
     * @param href
     * @param target
     * @param spread
     */
    public TreeNode(Integer id, Integer pid, String title, String icon, String href, String target, Boolean spread) {
        this.id = id;
        this.pid = pid;
        this.title = title;
        this.icon = icon;
        this.href = href;
        this.target = target;
        this.spread = spread;
    }




    public  static class TreeNodeBuilder{
        /**
         *
         * @param treeNodes 要生成有层级关系的源数据
         * @param topId  最顶层的pid 根节点的pid
         * @return
         */
        public static List<TreeNode> build(List<TreeNode> treeNodes,Integer topId){

            List<TreeNode> nodes=new ArrayList<>();

            for (TreeNode n1 : treeNodes) {
                if(n1.getPid().equals(topId)){
                    //说明n1节点是
                    nodes.add(n1);
                }
                for (TreeNode n2 : treeNodes) {
                    if(n2.getPid().equals(n1.getId())){
                        //说明n2是n1的子节点 应该放到n1的clild里面
                        n1.getChild().add(n2);
                    }
                }
            }
            return nodes;
        }
    }
}

3、SysMenuServiceImpl

根据用户id查询该用户所拥有的菜单

 // 1 用户的id 查询角色
    List<Object> roleIds = sysUserRoleMapper.selectObjs(new LambdaQueryWrapper<SysUserRole>().select(
      SysUserRole::getRoleId
      ).
        eq(SysUserRole::getUserId, userId)
    );
    if (roleIds == null || roleIds.isEmpty()) {
      return menuAuthResult;
    }
    // 2 通过角色的id 查询菜单的id
    List<Object> menuIds = sysRoleMenuMapper.selectObjs(new LambdaQueryWrapper<SysRoleMenu>().select(
      SysRoleMenu::getMenuId
      ).in(SysRoleMenu::getRoleId, roleIds)
    );
    if (menuIds == null || menuIds.isEmpty()) {
      return menuAuthResult;
    }
    // 3 通过菜单的Id 查询菜单的数据
    List<SysMenu> sysMenus = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().in(SysMenu::getMenuId, menuIds));
    if (sysMenus == null || sysMenus.isEmpty()) {
      return menuAuthResult;
    }

查询出来的menu集合:

4、测试

@Test
  public  void test3() {
    SysUser sysUser = sysUserMapper.selectById(1);
    List<SysMenu> menuList = null;
    if (null != sysUser) {
      menuList = this.sysMenuService.queryeMenuByUserIdForList(sysUser.getUserId());
      List<com.zy.common.tree.TreeNode> treeNodes=new ArrayList<>();
      for (SysMenu m : menuList) {
        treeNodes.add(new com.zy.common.tree.TreeNode(m.getMenuId().intValue(),m.getParentId().intValue(),m.getMenuName(),m.getIcon(),m.getUrl(),"_self",Boolean.TRUE));
      }
      List<com.zy.common.tree.TreeNode> build = com.zy.common.tree.TreeNode.TreeNodeBuilder.build(treeNodes, 0);
      System.out.println(build);

treeNodes:

buid后的树形结构:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持W3Cschool


0 人点赞