java实现树形菜单对象

网友投稿 237 2023-01-14

java实现树形菜单对象

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

1、SysMenu

package com.zy.shiro.domain;

import com.baomidou.mybatisplus.annotation.IdThttp://ype;

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 list;

// @JsonInclude(value = JsonInclude.Include.NON_EMPTY)

// private List 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 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 要生成有层级关系的源数据

http:// * @param topId 最顶层的pid 根节点的pid

* @return

*/

public static List build(List treeNodes,Integer topId){

List 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 roleIds = sysUserRoleMapper.selectObjs(new LambdaQueryWrapper().select(

SysUserRole::getRoleId

).

eq(SysUserRole::getUserId, userId)

);

if (roleIds == null || roleIds.isEmpty()) {

return menuAuthResult;

}

// 2 通过角色的id 查询菜单的id

List menuIds = sysRoleMenuMapper.selectObjs(new LambdaQueryWrapper().select(

SysRoleMenu::getMenuId

).in(SysRoleMenu::getRoleId, roleIds)

);

if (menuIds == null || menuIds.isEmpty()) {

return menuAuthResult;

}

// 3 通过菜单的Id 查询菜单的数据

List sysMenus = sysMenuMapper.selectList(new LambdaQueryWrapper().in(SysMenu::getMenuId, menuIds));

if (sysMenus == null || sysMenus.isEmpty()) {

return menuAuthResult;

}

查询出来的menu集合:

4、测试

@Test

public void test3() {

SysUser sysUser = sysUserMapper.selectById(1);

List menuList = null;

if (null != sysUser) {

menuList = this.sysMenuService.queryeMenuByUserIdForList(sysUser.getUserId());

List 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 build = com.zy.common.tree.TreeNode.TreeNodeBuilder.build(treeNodes, 0);

System.out.println(build);

treeNodes:

buid后的树形结构:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 反射类型Type的用法说明
下一篇:springboot集成ftp实现文件上传
相关文章

 发表评论

暂时没有评论,来抢沙发吧~