Spring Boot + Mybatis Plus實現樹狀菜單的方法
1、實體類中添加子菜單列表或集合
@TableField(exist = false) private Set<SysMenu> childrenList = new HashSet<>();
2、定義一個方法遞歸的獲取子菜單
public SysMenu getChildrenList(SysMenu sysMenu, List<SysMenu> sysMenuList) { for (SysMenu menu : sysMenuList) { if (menu.getMenuParentId().equals(sysMenu.getId())) { sysMenu.getChildrenList().add(getChildrenList(menu, sysMenuList)); } } return sysMenu; }
3、構建菜單樹
public List<SysMenu> menuTree() { //獲取所有菜單 List<SysMenu> sysMenuList = list(new LambdaQueryWrapper<SysMenu>() //::在java8中的作用就是獲得方法 .eq(SysMenu::getMenuType, ResultConstant.MENU) ); //stream().filter()作用:過濾一個List對象,查找符合條件的對象集合 List<SysMenu> firstMenuList = sysMenuList.stream().filter //過濾出一級菜單 (s -> s.getMenuParentId().equals(ResultConstant.MENU_TOP)) // 返回list集合形式 .collect(Collectors.toList()); // 遍歷一級菜單,構造菜單樹 for (SysMenu menu : firstMenuList) { getChildrenList(menu, sysMenuList); } return firstMenuList; }
菜單表如下
CREATE TABLE `sys_menu` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `menu_name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '資源名稱', `menu_permission` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '權限', `menu_path` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '對應url', `menu_parent_id` bigint(20) DEFAULT NULL COMMENT '父節點id', `menu_type` int(11) DEFAULT NULL COMMENT '菜單類型0菜單,1欄目類型', `order_num` int(11) DEFAULT NULL COMMENT '排序字段:值越小 順序越靠前', `create_time` datetime DEFAULT NULL COMMENT '創建時間', `update_time` datetime DEFAULT NULL COMMENT '最後更新時間', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC COMMENT='菜單表'; -- ---------------------------- -- Records of sys_menu -- ---------------------------- INSERT INTO `sys_menu` VALUES ('1', '菜單樹', 'homePage:menuTree', '', '0', '0', '1', '2021-09-17 12:46:11', '2021-09-17 12:46:11'); INSERT INTO `sys_menu` VALUES ('2', '登錄模塊', null, null, '0', '0', '2', null, null); INSERT INTO `sys_menu` VALUES ('3', '登錄', null, '/login', '2', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('4', '修改密碼', null, '/login/updatePassword', '2', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('5', '修改個人信息', null, '/login/updateMessage', '2', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('6', '忘記密碼', 'login:forgetPassword', '/login/forgetPassword', '2', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('7', '消息通知管理', null, null, '0', '0', '3', null, null); INSERT INTO `sys_menu` VALUES ('8', '發佈通知', 'notice:insert', '/notice/insert', '7', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('9', '修改通知', 'notice:update', '/notice/update', '7', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('10', '刪除通知', 'notice:delete', '/notice/delete', '7', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('11', '查看所有通知', 'notice:listAll', '/notice/listAll', '7', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('12', '查看本人已發佈通知', 'notice:noticeList', '/notice/noticeList', '7', '1', '5', null, null); INSERT INTO `sys_menu` VALUES ('13', '查看所有已發佈通知', 'notice:releasedNotice', '/notice/releasedNotice', '7', '1', '6', null, null); INSERT INTO `sys_menu` VALUES ('14', '系統管理', null, null, '0', '0', '4', null, null); INSERT INTO `sys_menu` VALUES ('15', '用戶管理', null, null, '14', '0', '1', null, null); INSERT INTO `sys_menu` VALUES ('16', '添加用戶', 'user:insert', '/user/insert', '15', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('17', '修改用戶信息', 'user:update', '/user/update', '15', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('18', '刪除用戶信息', 'user:delete', '/user/delete', '15', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('19', '查看所有用戶信息', 'user:listAll', '/user/listAll', '15', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('20', '分配用戶角色', 'user:setRole', '/user/setRole', '15', '1', '5', null, null); INSERT INTO `sys_menu` VALUES ('21', '角色管理', null, null, '14', '0', '2', null, null); INSERT INTO `sys_menu` VALUES ('22', '添加角色', 'role:insert', '/role/insert', '21', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('23', '修改角色信息', 'role:update', '/role/update', '21', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('24', '刪除角色', 'role:delete', '/role/delete', '21', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('25', '查看所有角色', 'role:listAll', 'role:listAll', '21', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('26', '設置角色權限', 'role:setMenu', '/role/setMenu', '21', '1', '5', null, null); INSERT INTO `sys_menu` VALUES ('27', '查看自己是否各個擁有角色', 'role:myRole', '/role/myRole', '21', '1', '6', null, null); INSERT INTO `sys_menu` VALUES ('28', '權限管理', null, null, '14', '0', '3', null, null); INSERT INTO `sys_menu` VALUES ('29', '添加權限', 'menu:insert', '/menu/insert', '28', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('30', '修改權限', 'menu:update', '/menu/update', '28', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('31', '刪除權限', 'menu:delete', '/menu/delete', '28', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('32', '查看所有權限', 'menu:listAll', '/menu/listAll', '28', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('33', '登錄日志', '', '', '14', '0', '4', null, null); INSERT INTO `sys_menu` VALUES ('34', '操作日志', '', '', '14', '0', '5', null, null); INSERT INTO `sys_menu` VALUES ('35', '信息管理', null, null, '0', '0', '5', null, null); INSERT INTO `sys_menu` VALUES ('36', '查看所有學生信息', 'student:listAll', '/student/listAll', '35', '0', '1', null, null); INSERT INTO `sys_menu` VALUES ('37', '查看所有教師信息', 'teacher:listAll', '/teacher/listAll', '35', '0', '2', null, null); INSERT INTO `sys_menu` VALUES ('38', '班級信息管理', null, null, '35', '0', '3', null, null); INSERT INTO `sys_menu` VALUES ('39', '添加班級', 'class:insert', '/class/insert', '38', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('40', '修改班級信息', 'class:update', '/class/update', '38', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('41', '刪除班級', 'class:delete', '/class/delete', '38', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('42', '查看所有班級', 'class:listAll', '/class/listAll', '38', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('43', '專業管理', null, null, '35', '0', '4', null, null); INSERT INTO `sys_menu` VALUES ('44', '添加專業', 'major:insert', '/major/insert', '43', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('45', '修改專業信息', 'major:update', '/major/update', '43', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('46', '刪除專業', 'major:delete', '/major/delete', '43', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('47', '查看所有專業', 'major:listAll', '/major/listAll', '43', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('48', '學院管理', null, null, '35', '0', '5', null, null); INSERT INTO `sys_menu` VALUES ('49', '添加學院', 'institute:insert', '/institute/insert', '48', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('50', '修改學院信息', 'institute:update', '/institute/update', '48', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('51', '刪除學院', 'institute:delete', '/institute/delete', '48', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('52', '查看所有學院', 'institute:listAll', '/institute/listAll', '48', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('53', '上課管理', null, null, '0', '0', '6', null, null); INSERT INTO `sys_menu` VALUES ('54', '課程表', null, null, '53', '0', '1', null, null); INSERT INTO `sys_menu` VALUES ('55', '添加課程', 'lesson:insert', '/lesson/insert', '54', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('56', '修改課程', 'lesson:update', '/lesson/update', '54', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('57', '刪除課程', 'lesson:delete', '/lesson/delete', '54', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('58', '查看所有課程', 'lesson:listAll', '/lesson/listAll', '54', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('59', '考勤管理', null, null, '53', '0', '2', null, null); INSERT INTO `sys_menu` VALUES ('60', '教師發放簽到', 'lesson:startCourse', '/lesson/startCourse', '59', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('61', '學生簽到', 'attendance:studentSignIn', '/attendance/studentSignIn', '59', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('62', '查看考勤信息', 'attendance:listAll', '/attendance/listAll', '59', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('63', '請假管理', null, null, '0', '0', '7', null, null); INSERT INTO `sys_menu` VALUES ('64', '學生請假', 'leave:studentLeave', '/leave/studentLeave', '63', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('65', '修改請假信息', 'leave:studentUpdate', '/leave/studentUpdate', '63', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('66', '刪除請假', 'leave:studentDelete', '/leave/studentDelete', '63', '1', '3', null, null); INSERT INTO `sys_menu` VALUES ('67', '查看所有請假信息', 'leave:listAll', '/leave/listAll', '63', '1', '4', null, null); INSERT INTO `sys_menu` VALUES ('68', '請假審批', 'leave:approval', '/leave/approval', '63', '1', '5', null, null); INSERT INTO `sys_menu` VALUES ('69', '查看登錄日志', 'loginLog:listAll', '/loginLog/listAll', '33', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('70', '刪除登錄日志', 'loginLog:delete', '/loginLog/delete', '33', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('71', '查看操作日志', 'operateLog:listAll', '/operateLog/listAll', '34', '1', '1', null, null); INSERT INTO `sys_menu` VALUES ('72', '刪除操作日志', 'operateLog:delete', '/operateLog/delete', '34', '1', '2', null, null); INSERT INTO `sys_menu` VALUES ('73', '分配教師班級', 'teacher:assignTeacherClasses', '/teacher/assignTeacherClasses', '38', '1', '5', null, null); INSERT INTO `sys_menu` VALUES ('74', '查看教師是否任教各個班級', 'teacher:myClass', '/teacher/myClass', '38', '1', '6', null, null);
效果如下
到此這篇關於Spring Boot + Mybatis Plus實現樹狀菜單的文章就介紹到這瞭,更多相關Spring Boot Mybatis Plus樹狀菜單內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- pgsql之create user與create role的區別介紹
- JavaWeb倉庫管理系統詳解
- Spring Security權限想要細化到按鈕實現示例
- Postgres 創建Role並賦予權限的操作
- Mybatis如何實現InsertOrUpdate功能