您好,登錄后才能下訂單哦!
本篇內容主要講解“如何使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。