当前位置:主页 > 软件编程 > JAVA代码 >

Spring Boot + Mybatis Plus实现树状菜单的方法

时间:2022-11-14 09:31:25 | 栏目:JAVA代码 | 点击:

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);

效果如下

在这里插入图片描述

您可能感兴趣的文章:

相关文章