本文實(shí)例為大家分享了java實(shí)現(xiàn)樹形菜單對(duì)象的具體代碼,供大家參考,具體內(nèi)容如下
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對(duì)象", 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;
/**
* 授權(quán)(多個(gè)用逗號(hào)分隔,如:user:list,user:create)
*/
private String permisions;
/**
* 類型 0:目錄 1:菜單 2:按鈕
*/
private Integer menuType;
/**
* 菜單圖標(biāo)
*/
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<>();
/**
* 登陸成功之后系統(tǒng)主而需要的菜單樹的json對(duì)象構(gòu)造器
* @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 要生成有層級(jí)關(guān)系的源數(shù)據(jù)
* @param topId 最頂層的pid 根節(jié)點(diǎn)的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節(jié)點(diǎn)是
nodes.add(n1);
}
for (TreeNode n2 : treeNodes) {
if(n2.getPid().equals(n1.getId())){
//說明n2是n1的子節(jié)點(diǎn) 應(yīng)該放到n1的clild里面
n1.getChild().add(n2);
}
}
}
return nodes;
}
}
}
3、SysMenuServiceImpl
根據(jù)用戶id查詢?cè)撚脩羲鶕碛械牟藛?/p>
// 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 查詢菜單的數(shù)據(jù)
List<SysMenu> sysMenus = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().in(SysMenu::getMenuId, menuIds));
if (sysMenus == null || sysMenus.isEmpty()) {
return menuAuthResult;
}
查詢出來的menu集合:
4、測(cè)試
@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后的樹形結(jié)構(gòu):
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持W3Cschool。