java实现递归菜单树

网友投稿 415 2022-11-25


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 http://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

}

3.菜单树

/**

* 获取菜单树

* @returhttp://n

*/

public List

//查询所有菜单

List

//返回的菜单树

List

for (Menu menu : menus) {

//pid(上级Id)为0的是根菜单

if ("0".equals(menu.getPid())) {

rootMenus.add(menu);

}

}

//遍历,找到二级菜单(根菜单的id和所有菜单中的pid比较)

for (Menu rootMenu : rootMenus) {

List

rootMenu.setChildren(child);

}

return rootMenus;

}

/**

* 递归获取下级菜单

* @param pid 上级Id

* @param menus 所有菜单

* @return

*/

public List

//子菜单列表

List

for (Menu menu : menus) {

if (pid.equals(menu.getPid())) {

childList.add(menu);

}

}

//遍历 获取子菜单的子菜单

for (Menu menu : childList) {

List

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,

yrXCjoCXJ "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": "铲车"yrXCjoCXJ,

"orderNum": 2,

"url": null,

"pid": "3",

"icon": null,

"children": []

}

]

}

]

}

5.菜单树工具类

菜单树工具类


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot学习系列之MyBatis Plus整合封装的实例详解
下一篇:IDEA2020.1个性化设置的实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~