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

}

3.菜单树

/**

* 获取菜单树

* @return

*/

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,

"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.菜单树工具类

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

无限级Java递归) 2007-02-08 10:26 这几天,用java写了一个无限极的递归写的,可能代码不够简洁,性能不够好,不过也算是练习,这几天再不断改进。前面几个小图标的判断,搞死我了。 package com.nickol.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.nickol.utility.DB; public class category extends HttpServlet { /** * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println(""); out.println(""); out.println(" Category" + "" + "body{font-size:12px;}" + "" + "" + ""); out.println(" "); out.println(showCategory(0,0,new ArrayList(),"0")); out.println(" "); out.println(""); out.flush(); out.close(); } public String showCategory(int i,int n,ArrayList frontIcon,String countCurrent){ int countChild = 0; n++; String webContent = new String(); ArrayList temp = new ArrayList(); try{ Connection conn = DB.GetConn(); PreparedStatement ps = DB.GetPs("select * from category where pid = ?", conn); ps.setInt(1, i); ResultSet rs = DB.GetRs(ps); if(n==1){ if(rs.next()){ webContent += "";//插入结尾的减号 temp.add(new Integer(0)); } webContent += " ";//插入站点图标 webContent += rs.getString("cname"); webContent += "\n"; webContent += showCategory(Integer.parseInt(rs.getString("cid")),n,temp,"0"); } if(n==2){ webContent += "\n"; }else{ webContent += "\n"; } while(rs.next()){ for(int k=0;k<frontIcon.size();k++){ int iconStatic = ((Integer)frontIcon.get(k)).intValue(); if(iconStatic == 0){ webContent += "";//插入空白 }else if(iconStatic == 1){ webContent += "";//插入竖线 } } if(rs.isLast()){ if(checkChild(Integer.parseInt(rs.getString("cid")))){ webContent += "";//插入结尾的减号 temp = (ArrayList)frontIcon.clone(); temp.add(new Integer(0)); }else{ webContent += "";//插入结尾的直角 } }else{ if(checkChild(Integer.parseInt(rs.getString("cid")))){ webContent += "";//插入未结尾的减号 temp = (ArrayList)frontIcon.clone(); temp.add(new Integer(1)); }else{ webContent += "";//插入三叉线 } } if(checkChild(Integer.parseInt(rs.getString("cid")))){ webContent += " ";//插入文件夹图标 }else{ webContent += " ";//插入文件图标 } webContent += rs.getString("cname"); webContent += "\n"; webContent += showCategory(Integer.parseInt(rs.getString("cid")),n,temp,countCurrent+countChild); countChild++; } webContent += "\n"; DB.CloseRs(rs); DB.ClosePs(ps); DB.CloseConn(conn); }catch(Exception e){ e.printStackTrace(); } return webContent; } public boolean checkChild(int i){ boolean child = false; try{ Connection conn = DB.GetConn(); PreparedStatement ps = DB.GetPs("select * from category where pid = ?", conn); ps.setInt(1, i); ResultSet rs = DB.GetRs(ps); if(rs.next()){ child = true; } DB.CloseRs(rs); DB.ClosePs(ps); DB.CloseConn(conn); }catch(Exception e){ e.printStackTrace(); } return child; } } --------------------------------------------------------------------- tree.js文件 function changeState(countCurrent,countChild){ var object = document.getElementById("level" + countCurrent + countChild); if(object.style.display=='none'){ object.style.display='block'; }else{ object.style.display='none'; } var cursor = document.getElementById("cursor" + countCurrent + countChild); if(cursor.src.indexOf("images/tree_minus.gif")>=0) {cursor.src="images/tree_plus.gif";} else if(cursor.src.indexOf("images/tree_minusbottom.gif")>=0) {cursor.src="images/tree_plusbottom.gif";} else if(cursor.src.indexOf("images/tree_plus.gif")>=0) {cursor.src="images/tree_minus.gif";} else {cursor.src="images/tree_minusbottom.gif";} var folder = document.getElementById("folder" + countCurrent + countChild); if(folder.src.indexOf("images/icon_folder_channel_normal.gif")>=0){ folder.src = "images/icon_folder_channel_open.gif"; }else{ folder.src = "images/icon_folder_channel_normal.gif"; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值