6. 权限管理
6.1 环境搭建
6.1.1 数据库
- 权限管理的5张表的关系
添加4张表
# 权限表(菜单表) CREATE TABLE `sys_permission` ( `id` int(10) NOT NULL AUTO_INCREMENT, `permName` varchar(50) , `parent_id` int(11) , `path` varchar(255) , PRIMARY KEY (`id`) USING BTREE ); INSERT INTO `sys_permission` VALUES (1, '班级管理', 0, '/classes'); INSERT INTO `sys_permission` VALUES (2, '添加班级', 1, '/classes/classesAdd'); INSERT INTO `sys_permission` VALUES (3, '班级列表', 1, '/classes/classesList'); INSERT INTO `sys_permission` VALUES (4, '学生管理', 0, '/student'); INSERT INTO `sys_permission` VALUES (5, '学生列表', 4, '/student/studentList'); INSERT INTO `sys_permission` VALUES (6, '权限管理', 0, '/perm'); INSERT INTO `sys_permission` VALUES (7, '权限列表', 6, '/perm/permissionList'); INSERT INTO `sys_permission` VALUES (8, '角色列表', 6, '/perm/roleList'); INSERT INTO `sys_permission` VALUES (9, '用户列表', 6, '/perm/userList'); # 角色表 CREATE TABLE `sys_role` ( `id` int(10) NOT NULL AUTO_INCREMENT, `roleName` varchar(50), `roleDesc` varchar(50), PRIMARY KEY (`id`) USING BTREE ) ; INSERT INTO `sys_role` VALUES (1, 'admin', '超级管理员'); INSERT INTO `sys_role` VALUES (2, 'sms_admin', 'sms管理员'); INSERT INTO `sys_role` VALUES (3, 'user', '普通用户'); #中间表:角色权限表 CREATE TABLE `sys_role_permission` ( `role_id` int(10), `perm_id` int(10), INDEX `FK_Reference_3`(`role_id`) USING BTREE, INDEX `FK_Reference_4`(`perm_id`) USING BTREE, CONSTRAINT `sys_role_permission_ibfk_1` FOREIGN KEY (`perm_id`) REFERENCES `sys_permission` (`id`) , CONSTRAINT `sys_role_permission_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `sys_role` (`id`) ); INSERT INTO `sys_role_permission` VALUES (1, 1); INSERT INTO `sys_role_permission` VALUES (1, 2); INSERT INTO `sys_role_permission` VALUES (1, 3); INSERT INTO `sys_role_permission` VALUES (1, 4); INSERT INTO `sys_role_permission` VALUES (1, 5); INSERT INTO `sys_role_permission` VALUES (1, 6); INSERT INTO `sys_role_permission` VALUES (1, 7); INSERT INTO `sys_role_permission` VALUES (1, 8); INSERT INTO `sys_role_permission` VALUES (1, 9); INSERT INTO `sys_role_permission` VALUES (2, 1); INSERT INTO `sys_role_permission` VALUES (2, 2); INSERT INTO `sys_role_permission` VALUES (2, 3); INSERT INTO `sys_role_permission` VALUES (2, 4); INSERT INTO `sys_role_permission` VALUES (2, 5); # 中间表:用户角色表 CREATE TABLE `sys_user_role` ( `user_id` VARCHAR(32), `role_id` INT(10) , INDEX `FK_Reference_1`(`user_id`) USING BTREE, INDEX `FK_Reference_2`(`role_id`) USING BTREE, CONSTRAINT `sys_user_role_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `sys_role` (`id`), CONSTRAINT `sys_user_role_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`u_id`) ); INSERT INTO `sys_user_role` VALUES ('u001', 1); INSERT INTO `sys_user_role` VALUES ('u001', 2); INSERT INTO `sys_user_role` VALUES ('u002', 2);
6.1.2 后端环境
- 基本内容:JavaBean、Mapper、Service、Controller
- JavaBean
基本结构
6.2 用户管理
6.2.1 查询所有用户(关联角色)
1)后端
- 修改javaBean:
List<SysRole> roleList
- 编写Mapper:使用注解的方式查询关联数据
- 编写Service
- 编写Controller
- 修改javaBean:
List<SysRole> roleList
编写Mapper:使用注解的方式查询关联数据
- 修改UserMapper:查询所有,含角色
package com.czxy.classes.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.czxy.domain.TbUser; import org.apache.ibatis.annotations.*; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn * @description */ @Mapper public interface TbUserMapper extends BaseMapper<TbUser> { @Select("SELECT * FROM tb_user") @Results({ @Result(property = "uid", column = "u_id"), @Result(property = "userName", column = "user_name"), @Result(property = "password", column = "password"), @Result(property = "gender", column = "gender"), @Result(property = "image", column = "image"), @Result(property = "roleList", many = @Many(select = "com.czxy.classes.mapper.SysRoleMapper.findAllByUid") , column = "u_id") }) public List<TbUser> findAll(); }
修改RoleMapper:查询指定用户的角色
package com.czxy.classes.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.czxy.sys.SysRole; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn * @description */ @Mapper public interface SysRoleMapper extends BaseMapper<SysRole> { /** * 查询指定用户的所有角色 * @author 桐叔 * @email liangtong@itcast.cn * @return */ @Select("SELECT r.* FROM sys_role r, sys_user_role ur WHERE r.id = ur.role_id AND ur.user_id = #{uid}") public List<SysRole> findAllByUid(@Param("uid") String uid); }
编写Service
package com.czxy.classes.service; import com.baomidou.mybatisplus.extension.service.IService; import com.czxy.domain.TbUser; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn * @description */ public interface TbUserService extends IService<TbUser> { public List<TbUser> findAll(); } package com.czxy.classes.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.czxy.domain.TbUser; import com.czxy.classes.mapper.TbUserMapper; import com.czxy.classes.service.TbUserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn * @description