java实现递归菜单树
作者:男人要霸气 时间:2023-02-01 00:03:06
本文实例为大家分享了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.菜单树工具类
菜单树工具类
来源:https://blog.csdn.net/weixin_42740530/article/details/103564528
标签:java,递归,菜单树
0
投稿
猜你喜欢
C# 反射与dynamic最佳组合示例代码
2022-01-18 02:29:53
Java Synchronized的偏向锁详细分析
2021-07-09 00:35:23
深入学习C#多线程
2021-06-12 12:35:04
Java线程安全中的原子性浅析
2021-09-06 16:08:13
深入理解C♯ 7.0中的Tuple特性
2023-10-28 09:33:14
C#反射之基础应用实例总结
2023-08-29 04:50:56
Admin - SpringBoot + Maven 多启动环境配置实例详解
2022-03-08 09:48:16
WPF使用WrapPanel实现虚拟化效果
2021-07-04 14:53:53
详解feign调用session丢失解决方案
2021-08-29 20:38:20
Java实现查找算法的示例代码(二分查找、插值查找、斐波那契查找)
2022-05-28 04:17:38
Java内存溢出案例模拟和原理分析过程
2023-04-09 07:27:48
c#方法重写和隐藏的学习示例
2022-12-24 14:20:12
解决java.lang.Error: Unresolved compilation problems:问题
2023-02-10 05:58:08
Android绘制圆形百分比加载圈效果
2023-07-16 17:23:38
使用Spring Boot进行单元测试详情
2023-11-10 08:01:53
Android编程之绘图canvas基本用法示例
2022-08-21 15:02:23
C#用记事本编写简单WinForm窗体程序
2021-12-19 13:02:31
利用C#代码将html样式文件与Word文档互换的方法
2022-04-22 07:11:31
Flutter开发setState能否在build中直接调用详解
2022-05-17 14:55:27
Springboot 如何指定获取出 yml文件里面的配置值
2022-08-29 21:04:48