目录
建表
1、建两张表,用于区分管理员和用户的权限,达到不同权限登录可以达到不同效果的办法。
配置:
bean包下Role类和UserRole类
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 + '\'' +
'}';
}
}
public class UserRole {
public UserRole() {
}
public UserRole(int id, int userId, int roleId) {
this.id = id;
this.userId = userId;
this.roleId = roleId;
}
private int id;
private int userId;
private int roleId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", userId=" + userId +
", roleId=" + roleId +
'}';
}
}
useController
@RequestMapping("toAddRole.do")
public ModelAndView toAddRole(int id){
List<Role> roleList=roleService.findRoleByUserId(id);
ModelAndView mv=new ModelAndView();
mv.addObject("roles",roleList);
mv.addObject("id",id);
mv.setViewName("user-role-add");
return mv;
}
// addRole.do
@RequestMapping("addRole.do")
@ResponseBody
public String add(String roleList,String userId){
String[] strs = roleList.split(",");
List<Integer> ids=new ArrayList<>();
for(String s:strs){
ids.add(Integer.parseInt(s));
}
roleService.add(ids,userId);
return "";
}
RoleService
@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);
}
@Override
public void add(List<Integer> ids, String userId) {
for (int roleId : ids) {
UserRole userRole = new UserRole();
userRole.setUserId(Integer.parseInt(userId));
userRole.setRoleId(roleId);
roleDao.addRole(userRole);
}
}
}
IRoleService接口
public interface IRoleService {
List<Integer> findRoleId(int userId);
List<Role> findRoleByUserId(int id);
void add(List<Integer> ids, String userId);
}
dao包下RoleDao类
public interface RoleDao {
List<Integer> findRoleIdByUserId(int userId);
List<Role> findRoleByUserId(int id);
void addRole(UserRole userRole);
}
RoleMapper.xml
<?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>
<insert id="addRole" parameterType="userRole">
insert into user_role (userId,roleId) values (#{userId},#{roleId})
</insert>
</mapper>
user-list.jsp
<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.jsp
function addRoles() {
var roleList=new Array();
$("input[name='roleId']:checked").each(
function () {
roleList.push($(this).val())
}
);
var userId=$("input[name=userId]").val();
alert(roleList);
alert(userId);
$.ajax({
type:"post",
url: "${pageContext.request.contextPath}/user/addRole.do",
data:{roleList:roleList.toString(),userId:userId},
success:function () {
alert("添加角色成功");
location.href="${pageContext.request.contextPath}/user/findAll.do";
},
error:function () {
alert("添加角色失败");
}
});
}