您好,登錄后才能下訂單哦!
本文實例為大家分享了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.菜單樹工具類
菜單樹工具類
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。