接着上次用户管理模块的分享,角色管理模块呢,他中间的内容其实和用户是一样的,但是有一点不同就是,要给用户分配角色呀,所以就是个问题了,接下来,,,,害,码不太清楚,就直接上主要的代码吧!!!!上分享:
1.首先在实现接口类(RoleUserServiceimpl)中,就要开始判断:
public Result addUserRole(UserRoleEntity userRoleEntity) { int res = 0; if (userRoleEntity.getRoleId() != null && userRoleEntity.getUserId() != null) { String[] userId=userRoleEntity.getUserId().split(","); UserRoleEntity userRoleEntity1=new UserRoleEntity(); userRoleEntity1.setRoleId(userRoleEntity.getRoleId()); for (String i:userId){ userRoleEntity1.setUserId(i); userRoleMapper.addUserRole(userRoleEntity1); } } return Result.success("新增用户角色成功");}}
2.在xml文件中,就要写三条语句:
<!--查询角色已经分配的用户--> <select id="selectUserIdByRoleId" parameterType="com.itheima.entity.UserRoleEntity" resultType="com.itheima.entity.UserEntity"> select sys_user.id, sys_user.`name`, sys_user.depId, sys_user.account from sys_user inner JOIN user_role on sys_user.id = user_role.userId where sys_user.isDeleted = 0 and user_role.roleId= #{roleId}; </select> <!--查询角色未分配的用户--> <select id="selectNoUserIdByRoleId" resultType="com.itheima.entity.UserEntity" parameterType="com.itheima.entity.UserRoleEntity"> select sys_user.id, sys_user.`name`, sys_user.depId, sys_user.account from sys_user where sys_user.isDeleted = 0 and !find_in_set(id,#{userId}); </select> <!--查询角色的用户id--> <select id="selectUserId" parameterType="com.itheima.entity.UserRoleEntity" resultType="com.itheima.entity.UserRoleEntity"> select userId from user_role where roleId = #{roleId}; </select>