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.菜单树工具类
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。