一、课程介绍
【课程名称】SpringBoot 2.x 实战仿B站高性能后端项目。
【章节】第三章3.33、3.34 操作权限&菜单权限
【讲师】HELLOSTAR
二、课程内容
1.开发准备
实际权限控制的接口开发,关联类型表的Java类中定义冗余字段,方便一次存储两张表的查询结果。
比如下面的角色权限表的Java类中保存了角色表中的角色名称和角色编码,就是为了将关联查询到的角色表数据一并保存。
如果为了实体类的准确性,可以不在实体类中加冗余字段,而是重新建一个实体类带上冗余字段。专门做这种用途。这种是不是也可行?
public class UserRole { private Long id; private Long userId; private Long roleId; //冗余字段(角色名称)存储角色名称 private String roleName; //冗余字段(角色编码) private String roleCode;
2.接口编写
实现接口:
根据用户id查询该用户所拥有的所有权限。
实现过程:
新建用户权限类,存储不同的权限。如下
List<AuthRoleElementOperation> roleElementOperationList; List<AuthRoleMenu> roleMenuList;
根据用户id查询用户所有的角色role集合;
根据auth_role_element_operation(角色操作权限表)的关联性查询当前用户角色的所有元素操作权限;
根据auth_role_menu(角色菜单权限表)的关联性查询当前用户角色的所有菜单操作权限。
查询元素操作权限的示例xml文件代码如下:
<resultMap id="AuthElementOperationResultMap" type="com.imooc.bilibili.domain.auth.AuthRoleElementOperation"> <id column="id" property="id"/> <id column="roleId" property="roleId"/> <id column="elementOperationId" property="elementOperationId"/> <association property="authElementOperation" javaType="com.imooc.bilibili.domain.auth.AuthElementOperation"> <id column="elementName" property="elementName"/> <id column="elementCode" property="elementCode"/> <id column="operationType" property="operationType"/> </association> </resultMap> <select id="getRoleElementOperationsByRoleIds" parameterType="java.util.Set" resultMap="AuthElementOperationResultMap"> select areo.*, aeo.elementName, aeo.elementCode, aeo.operationType from t_auth_role_element_operation areo left join t_auth_element_operation aeo on areo.elementOperationId = aeo.id where areo.roleId in <foreach collection="roleIdSet" item="roleId" index="index" open="(" close=")" separator=","> #{roleId} </foreach> </select>
三、课程收获
mybatis接口方法中的传入参数如果是集合或者set集合的话,需要使用注解@Params("")命名参数,xml文件中才能获取到参数。
mybatis查询结果如果是多个类的字段,返回类型使用ResultMap 映射关联表中的字段。
<resultMap id="AuthElementOperationResultMap" type="com.imooc.bilibili.domain.auth.AuthRoleElementOperation"> <id column="id" property="id"/> <id column="roleId" property="roleId"/> <id column="elementOperationId" property="elementOperationId"/> <association property="authElementOperation" javaType="com.imooc.bilibili.domain.auth.AuthElementOperation"> <id column="elementName" property="elementName"/> <id column="elementCode" property="elementCode"/> <id column="operationType" property="operationType"/> </association> </resultMap>
四、学习过程