本小结讲解管理用户的增删改查实现,
首先是添加用户,如下图所示,可以看到添加用户的同时也要给用户分配角色,至少给用户分配一个或者是多个角色
页面js部分:
$.ajax({//几个参数需要注意一下
type: "POST",//方法类型
dataType: "json",//预期服务器返回的数据类型
url: "/admin/admin-user-add.action" ,//url
data: $('#form1').serialize(),
success: function (data) {
//console.log(data.result);//打印服务端返回的数据(调试用)
if (data.result == 'SUCCESS'||data.result == 200) {
$("loginName").val('');
layer.msg("添加成功",{icon:1,time:2000});
}else{
layer.msg("添加失败:"+data.result.toString(),{icon:5,time:2000});
}
},
error : function() {
layer.msg(data.result.toString());
}
});
adminController
/**
* 添加管理员
* @param adminUser pojo对象
* @param errors 验证错误对象
* @return*/@AccessPermissionsInfo("admin:add")
@RequestMapping(value= "/admin-user-add.action",method =RequestMethod.POST)
@ResponseBodypublic MapregAdminUser(@Valid AdminUser adminUser,
HttpServletRequest request,
Errors errors,Model model){
Map resultMap = new HashMap();//如果有错误return当前注册页面
if(errors.hasErrors())
{
resultMap.put("result","参数不合法");
}
List roleList =roleService.findAllRoleList();
model.addAttribute("roleList",roleList);
logger.info("roleIds:"+request.getParameterValues("roleIds"));if(request.getParameterValues("roleIds")==null
||request.getParameterValues("roleIds").length<=0){
resultMap.put("result","请给用户分配至少一个角色");
}else{
String[] roleIds= request.getParameterValues("roleIds");//logger.info("regAdminUser方法被调用:"+adminUser.toString());//先查询数据库是否存在该用户,如果不存在就执行注册用户
AdminUser adminUser1 =adminUserService.findAdminUserByLoginName(adminUser.getLoginName().toString());if(adminUser1!=null){
resultMap.put("result","用户名已存在");
}else{//密码加密
String encryptPassword =
newPasswordEncryption().encryption(adminUser.getPassword(),
adminUser.getLoginName().toString());
adminUser.setPassword(encryptPassword);int count =adminUserService.addAdminUser(adminUser,roleIds);if (count > 0) {
resultMap.put("result","SUCCESS");
}
}
}returnresultMap;
}
看一下adminUserService.addAdminUser(adminUser,roleIds);方法:
adminUser:用户信息
roleIds:角色编号数组
admiUserServiceImpl部分:
/**
* 添加管理用户
*
* @param adminUser 管理用户信息
* @param roleIds 管理用户角色组
* @return*/@Overridepublic intaddAdminUser(AdminUser adminUser, String[] roleIds) {//先增加用户
int successCount =adminUserDao.addAdminUser(adminUser);//获取到新增用户的ID
int userId =Integer.parseInt(adminUser.getId().toString());//然后循环遍历角色数组增加用户角色关系
for(String roleId :roleIds)
{
successCount+=roleDao.addUserRoleRelations(userId,roleId);
}returnsuccessCount;
}
持久层部分:
AdminUserMapper.xml
SELECTif(MAX(id)=NULL,1,MAX(id)+1) FROM t_admin
INSERT INTO t_admin
id,
LoginName,
password,
#{id,jdbcType=BIGINT},
#{LoginName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
RoleMapper.xml
SELECTif(MAX(adminRoleId)=NULL,1,MAX(adminRoleId)+1) FROM t_admin_roleINSERT INTO t_admin_role(adminRoleId,adminId,roleId)
VALUES (#{adminRoleId},#{userId},#{roleId})
删除用户
adminUserController
/**
* 删除管理用户
* @param request
* @return*/@AccessPermissionsInfo("admin:delete")
@RequestMapping(value= "/admin-user-delete.action",method =RequestMethod.POST)
@ResponseBodypublic MapdeleteAdminUser(HttpServletRequest request)
{
String str_ids= request.getParameter("ids");
str_ids= str_ids.replace("/","");//替换掉/
String[] ids = str_ids.split(","); //用,分割//logger.info(Arrays.toString(ids));
Map resultMap = new HashMap();if(ids==null||ids.length<=0)
{
resultMap.put("result","参数异常");
}else{
AdminUser adminUser= (AdminUser)request.getSession().getAttribute("adminUser");if(adminUser!=null){int count =adminUserService.deleteAdminUserByIds(ids);if(count>0){
resultMap.put("result","SUCCESS");
}else{
resultMap.put("result","操作失败");
}
}else{
resultMap.put("result","登录超时,请重新登录");
}
}returnresultMap;
}
AdminUserServiceImpl
/**
* 批量删除管理用户
*
* @param ids 管理用户的编号组
* @return*/@Overridepublic intdeleteAdminUserByIds(String[] ids) {//先根据用户的编号删除该用户关联的角色信息
int successCount =roleDao.deleteAdminUserRoleRelation(ids);//然后再删除用户
successCount+=adminUserDao.deleteAdminUserByIds(ids);returnsuccessCount;
}
持久层
DELETE FROM t_admin_role
WHERE adminIdin
#{item}
DELETE FROM t_admin
WHERE idin
#{item}
修改用户:
AdminUserController
/**
* 编辑管理员
* @param request
* @param model
* @return*/@AccessPermissionsInfo("admin:edit")
@RequestMapping(value= "/admin-user-edit.action",method =RequestMethod.POST)
@ResponseBodypublic MapeditAdminUser(HttpServletRequest request,Model model){
Map resultMap = new HashMap();
List roleList =roleService.findAllRoleList();
model.addAttribute("roleList",roleList);
logger.info("roleIds:"+request.getParameterValues("roleIds"));if(request.getParameter("id")==null||request.getParameter("loginName")==null){
resultMap.put("result","用户参数不合法");
}else if(request.getParameterValues("roleIds")==null
||request.getParameterValues("roleIds").length<=0){
resultMap.put("result","请给用户分配至少一个角色");
}else{
String[] roleIds= request.getParameterValues("roleIds");
String id= request.getParameter("id").toString();
String loginName= request.getParameter("loginName").toString();//先查询数据库是否存在该用户
AdminUser adminUser1 =adminUserService.findAdminUserByLoginName(loginName);//如果存在该用户并且编号相等,就执行更新操作
if(adminUser1!=null&&adminUser1.getId().toString().equals(id)){int count =adminUserService.editAdminUser(adminUser1,roleIds);if (count > 0) {
resultMap.put("result","SUCCESS");
}
}else{
resultMap.put("result","没有这个用户");
}
}returnresultMap;
}
AdminUserServiceImpl
/**
* 编辑管理员用户信息
*
* @param adminUser 管理用户信息
* @param roleIds 角色组
* @return*/@Overridepublic inteditAdminUser(AdminUser adminUser, String[] roleIds) {//先删除用户关联的所有角色
int successCount =roleDao.deleteAdminUserRoleRelationById(adminUser.getId().toString());//然后循环遍历角色数组增加用户角色关系
for(String roleId :roleIds)
{
successCount+=roleDao.addUserRoleRelations(adminUser.getId(),roleId);
}returnsuccessCount;
}
持久层:
RoleMapper.xml
DELETE FROM t_admin_role
WHERE adminId=#{adminId}
SELECTif(MAX(adminRoleId)=NULL,1,MAX(adminRoleId)+1) FROM t_admin_roleINSERT INTO t_admin_role(adminRoleId,adminId,roleId)
VALUES (#{adminRoleId},#{userId},#{roleId})