java实现递归菜单树

发布时间:2020-09-28 18:01:57 作者:男人要霸气
来源:脚本之家 阅读:199

本文实例为大家分享了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.菜单树工具类

菜单树工具类

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. java实现递归求子集
  2. JavaScript如何实现无限级递归树

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 递归 菜单树

上一篇:T-SQL的组成

下一篇:python如何爬取动态网站

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》