In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use Java to achieve tree menu objects", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Java to implement tree menu objects.
1 、 SysMenupackage 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 object", description= "menu Management") public class SysMenu implements Serializable {@ TableId (value= "menu_id", type = IdType.AUTO) private Long menuId; / * parent menu id, parent menu is 0 * / private Long parentId / * menu name * / private String menuName; / * menu url * / private String url; / * Authorization (multiple separated by commas, such as: user:list,user:create) * / private String permisions; / * * Type 0: directory 1: menu 2: button * / private Integer menuType / * * menu icon * / private String icon; / * sort * / 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, TreeNodepackage 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@NoArgsConstructorpublic 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 () / @ param id * @ param pid * @ param title * @ param icon * @ param href * @ 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 to generate hierarchical source data * @ param topId pid * @ return * / public static List build (List treeNodes,Integer topId) {List nodes=new ArrayList () of the topmost pid root node For (TreeNode N1: treeNodes) {if (n1.getPid () .equals (topId)) {/ / indicates that the N1 node is nodes.add (N1) } for (TreeNode N2: treeNodes) {if (n2.getPid (). Equals (n1.getId () {/ / indicates that N2 is a child node of N1 should be placed in the clild of N1 n1.getChild () .add (N2) } return nodes;} 3 、 SysMenuServiceImpl
Query the menu owned by the user according to the user's id
/ / 1 the user's id query role List roleIds = sysUserRoleMapper.selectObjs (new LambdaQueryWrapper () .select (SysUserRole::getRoleId). Eq (SysUserRole::getUserId, userId); if (roleIds = = null | | roleIds.isEmpty ()) {return menuAuthResult;} / / 2 through the role's id query menu id List menuIds = sysRoleMenuMapper.selectObjs (new LambdaQueryWrapper (). Select (SysRoleMenu::getMenuId) .in (SysRoleMenu::getRoleId, roleIds)); if (menuIds = = null | menuIds.isEmpty ()) {return menuAuthResult } / / 3 query menu data List sysMenus = sysMenuMapper.selectList (new LambdaQueryWrapper (). In (SysMenu::getMenuId, menuIds)); if (sysMenus = = null | | sysMenus.isEmpty ()) {return menuAuthResult;} 4, test @ 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) At this point, I believe you have a deeper understanding of "how to use Java to achieve tree menu objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.