本文实例为大家分享了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;
@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;
private Long parentId;
private String menuName;
private String url;
private String permisions;
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<>();
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{
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后的树形结构:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。