本篇内容主要讲解“如何使用Java实现树形菜单对象”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Java实现树形菜单对象”吧!
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;
}
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;
}
}
}
根据用户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;
}
@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);
到此,相信大家对“如何使用Java实现树形菜单对象”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。