用户关联角色操作-代码实现
生活随笔
收集整理的這篇文章主要介紹了
用户关联角色操作-代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用戶角色關聯
用戶與角色之間是多對多關系,我們要建立它們之間的關系,只需要在中間表user_role插入數據即可。
用戶角色關聯相關頁面
在user-list.jsp頁面上添加鏈接
<a href="${pageContext.request.contextPath}/user/findUserByIdAndAllRole.do?id=${user.id}" class="btn bg-olive btn-xs">添加角色</a>UserController
findUserByIdAndAllRole(Long id)方法
此方法用于查找要操作的用戶及可以添加的角色,參數是要操作的用戶id
@RequestMapping("/findUserByIdAndAllRole.do") public ModelAndView findUserByIdAndAllRole(Long id) throws Exception {UserInfo user = userService.findById(id);List<Role> roleList = roleService.findOtherRole(id);ModelAndView mv = new ModelAndView();mv.addObject("user", user);mv.addObject("roleList", roleList);mv.setViewName("user-role-add");return mv; }調用IUserService的findById方法獲取要操作的User
調用IRoleService的findOtherRole方法用于獲取可以添加的角色信息
addRoleToUser(Long userId,Long[] ids)方法
些方法用于在用戶與角色之間建立關系,參數userId代表要操作的用戶id,參數ids代表的是角色id數組
@RequestMapping("/addRoleToUser.do") public String addRoleToUser(Long userId, Long[] ids) throws Exception {userService.addRoleToUser(userId,ids);return "redirect:findAll.do"; }Dao
IRoleDao
@Select("select * from role where id not in( select roleId from user_role where userId=# {id})") public List<Role> findOtherRole(Long id);用于查找可以添加的角色
IUserDao
@Insert("insert into user_role(userId,roleId) value(#{userId},#{roleId})") public void addRoleToUser(@Param("userId") Long userId, @Param("roleId") Long roleId);用于添加用戶與角色關系
總結
以上是生活随笔為你收集整理的用户关联角色操作-代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用户关联角色操作-流程分析
- 下一篇: 角色关联权限操作-代码实现