亲宝软件园·资讯

展开

java递归菜单树 java实现递归菜单树

男人要霸气 人气:0
想了解java实现递归菜单树的相关内容吗,男人要霸气在本文为您仔细讲解java递归菜单树的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java递归菜单树,java递归菜单,java菜单树,下面大家一起来学习吧。

1.表结构

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for menu
-- ----------------------------
DROP TABLE IF EXISTS `menu`;
CREATE TABLE `menu` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `menu_name` varchar(64) NOT NULL COMMENT '菜单名称',
 `order_num` int(11) DEFAULT NULL COMMENT '菜单顺序',
 `url` varchar(64) DEFAULT NULL COMMENT '菜单路径',
 `pid` varchar(11) DEFAULT NULL COMMENT '上级Id',
 `icon` varchar(255) DEFAULT NULL COMMENT '菜单图标',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of menu
-- ----------------------------
INSERT INTO `menu` VALUES ('1', '系统管理', '1', '', '0', null);
INSERT INTO `menu` VALUES ('2', '轨迹查询', '2', '', '0', null);
INSERT INTO `menu` VALUES ('3', '设备信息', '3', '', '0', null);
INSERT INTO `menu` VALUES ('4', '用户信息', '1', '', '1', null);
INSERT INTO `menu` VALUES ('5', '客户信息', '2', null, '1', null);
INSERT INTO `menu` VALUES ('6', '用户资料', '1', null, '4', null);
INSERT INTO `menu` VALUES ('7', '客户资料', '1', null, '5', null);
INSERT INTO `menu` VALUES ('8', '切割机', '1', null, '3', null);
INSERT INTO `menu` VALUES ('9', '铲车', '2', null, '3', null);
INSERT INTO `menu` VALUES ('10', '割片', '1', null, '8', null);

2.菜单实体类

import lombok.Data;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;


/**
 *
 * @author m
 * @email 123456789@qq.com
 * @date 2019-12-13 16:54:59
 */
@Data
@Table(name = "menu")
public class Menu implements Serializable {
 private static final long serialVersionUID = 1L;
 
 //
 @Id
 private Integer id;
 
 //菜单名称
 @Column(name = "menu_name")
 private String menuName;
 
 //菜单顺序
 @Column(name = "order_num")
 private Integer orderNum;
 
 //菜单路径
 @Column(name = "url")
 private String url;
 
 //上级Id
 @Column(name = "pid")
 private String pid;
 
 //菜单图标
 @Column(name = "icon")
 private String icon;

 //子菜单
 @Transient
 private List<Menu> children;


}

3.菜单树

 /**
  * 获取菜单树
  * @return
  */
 public List<Menu> menuTree() {
  //查询所有菜单
  List<Menu> menus = mapper.selectAll();
  //返回的菜单树
  List<Menu> rootMenus = new ArrayList<>();
  for (Menu menu : menus) {
   //pid(上级Id)为0的是根菜单
   if ("0".equals(menu.getPid())) {
    rootMenus.add(menu);
   }
  }
  //遍历,找到二级菜单(根菜单的id和所有菜单中的pid比较)
  for (Menu rootMenu : rootMenus) {
   List<Menu> child = getChild(String.valueOf(rootMenu.getId()), menus);
   rootMenu.setChildren(child);
  }
  return rootMenus;
 }


 /**
  * 递归获取下级菜单
  * @param pid 上级Id
  * @param menus 所有菜单
  * @return
  */
 public List<Menu> getChild(String pid, List<Menu> menus) {
  //子菜单列表
  List<Menu> childList = new ArrayList<>();
  for (Menu menu : menus) {
   if (pid.equals(menu.getPid())) {
    childList.add(menu);
   }
  }
  //遍历 获取子菜单的子菜单
  for (Menu menu : childList) {
   List<Menu> child = getChild(String.valueOf(menu.getId()), menus);
   menu.setChildren(child);
  }
  //递归出口 childList长度为0
  if (childList.size() == 0) {
   return new ArrayList<>();
  }
  return childList;
 }

4.测试

{
 "status": 200,
 "message": "操作成功",
 "data": [
  {
   "id": 1,
   "menuName": "系统管理",
   "orderNum": 1,
   "url": "",
   "pid": "0",
   "icon": null,
   "children": [
    {
     "id": 4,
     "menuName": "用户信息",
     "orderNum": 1,
     "url": "",
     "pid": "1",
     "icon": null,
     "children": [
      {
       "id": 6,
       "menuName": "用户资料",
       "orderNum": 1,
       "url": null,
       "pid": "4",
       "icon": null,
       "children": []
      }
     ]
    },
    {
     "id": 5,
     "menuName": "客户信息",
     "orderNum": 2,
     "url": null,
     "pid": "1",
     "icon": null,
     "children": [
      {
       "id": 7,
       "menuName": "客户资料",
       "orderNum": 1,
       "url": null,
       "pid": "5",
       "icon": null,
       "children": []
      }
     ]
    }
   ]
  },
  {
   "id": 2,
   "menuName": "轨迹查询",
   "orderNum": 2,
   "url": "",
   "pid": "0",
   "icon": null,
   "children": []
  },
  {
   "id": 3,
   "menuName": "设备信息",
   "orderNum": 3,
   "url": "",
   "pid": "0",
   "icon": null,
   "children": [
    {
     "id": 8,
     "menuName": "切割机",
     "orderNum": 1,
     "url": null,
     "pid": "3",
     "icon": null,
     "children": [
      {
       "id": 10,
       "menuName": "割片",
       "orderNum": 1,
       "url": null,
       "pid": "8",
       "icon": null,
       "children": []
      }
     ]
    },
    {
     "id": 9,
     "menuName": "铲车",
     "orderNum": 2,
     "url": null,
     "pid": "3",
     "icon": null,
     "children": []
    }
   ]
  }
 ]
}

5.菜单树工具类

菜单树工具类

加载全部内容

相关教程
猜你喜欢
用户评论