role
package com. zhongruan. bean;
public class Role {
private int id;
private String rolename;
private String roledesc;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getRolename ( ) {
return rolename;
}
public void setRolename ( String rolename) {
this . rolename = rolename;
}
public String getRoledesc ( ) {
return roledesc;
}
public void setRoledesc ( String roledesc) {
this . roledesc = roledesc;
}
@Override
public String toString ( ) {
return "Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", roledesc='" + roledesc + '\'' +
'}' ;
}
}
controller的部分代码改动
@Autowired
private IRoleService roleService;
@RequestMapping ( "/login.do" )
public ModelAndView login ( User user, HttpSession session) {
int id = userService. login ( user. getUsername ( ) , user. getPassword ( ) ) ;
ModelAndView modelAndView = new ModelAndView ( ) ;
if ( id != - 1 ) {
List< Integer> roleIds = roleService. findRoleId ( id) ;
session. setAttribute ( "roleIds" , roleIds) ;
session. setAttribute ( "user" , user) ;
modelAndView. setViewName ( "main" ) ;
} else {
modelAndView. setViewName ( "../failer" ) ;
}
return modelAndView;
}
@RequestMapping ( "toAddRole.do" )
public ModelAndView toAddRole ( int id) {
List< Role> roleList = roleService. findRoleByUserId ( id) ;
ModelAndView mv = new ModelAndView ( ) ;
mv. addObject ( "roles" , roleList) ;
mv. setViewName ( "user-role-add" ) ;
return mv;
}
IRoleService
package com. zhongruan. service;
import com. zhongruan. bean. Role;
import com. zhongruan. bean. UserRole;
import java. util. List;
public interface IRoleService {
List< Integer> findRoleId ( int userId) ;
List< Role> findRoleByUserId ( int id) ;
}
RoleService
package com. zhongruan. service. impl;
import com. zhongruan. bean. Role;
import com. zhongruan. bean. UserRole;
import com. zhongruan. dao. RoleDao;
import com. zhongruan. service. IRoleService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import java. util. List;
@Service
public class RoleService implements IRoleService {
@Autowired
private RoleDao roleDao;
@Override
public List< Integer> findRoleId ( int userId) {
return roleDao. findRoleIdByUserId ( userId) ;
}
@Override
public List< Role> findRoleByUserId ( int id) {
return roleDao. findRoleByUserId ( id) ;
}
}
RoleDao
package com. zhongruan. dao;
import com. zhongruan. bean. Role;
import com. zhongruan. bean. UserRole;
import java. util. List;
public interface RoleDao {
List< Integer> findRoleIdByUserId ( int userId) ;
List< Role> findRoleByUserId ( int id) ;
}
rolemapper
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace= "com.zhongruan.dao.RoleDao" >
< select id= "findRoleIdByUserId" parameterType= "int" resultType= "int" >
select roleId from user_role where userId= #{ userId}
< / select>
< select id= "findRoleByUserId" parameterType= "int" resultType= "role" >
select * from tb_role where id not in ( select roleId from user_role where userId= #{ id} )
< / select>
< / mapper>
user-list
< td class = "text-center" >
< a href= "${pageContext.request.contextPath}/user/toUpdate.do?id=${user.id}" class = "btn bg-olive btn-xs" > 更新< / a>
< a href= "${pageContext.request.contextPath}/user/deleteById.do?id=${user.id}" class = "btn bg-olive btn-xs" > 删除< / a>
< % List< Integer> roleIds = ( List< Integer> ) session. getAttribute ( "roleIds" ) ; % >
< % if ( roleIds. contains ( 1 ) ) { % >
< a href= "${pageContext.request.contextPath}/user/toAddRole.do?id=${user.id}" class = "btn bg-olive btn-xs" > 添加角色< / a>
< % } % >
< / td>
user-role-add
function addRoles ( ) {
var checkedNum = $ ( "input[name='roleId']:checked" ) . length;
if ( checkedNum == 0 ) {
alert ( "请至少选择一个角色进行添加!" ) ;
return ;
}
if ( confirm ( "确认要添加这些角色吗?" ) ) {
var roleList = new Array ( ) ;
$ ( "input[name='roleId']:checked" ) . each (
function ( ) {
roleList. push ( $ ( this ) . val ( ) )
}
) ;
var userId= $ ( "input[name=userId]" ) . val ( ) ;
$. ajax ( {
type: "post" ,
url: "${pageContext.request.contextPath}/user/deleteAll.do" ,
data: { roleList: roleList. toString ( ) ,
userList: userList} ,
success: function ( ) {
alert ( "添加角色成功" ) ;
location. href= "${pageContext.request.contextPath}/user/findAll.do"
} ,
error: function ( ) {
alert ( "添加角色失败" ) ;
}
} ) ;
}
}