标签:生成 logger 自增 添加用户 增加 注册 encrypt layer 执行
本小结讲解管理用户的增删改查实现,
首先是添加用户,如下图所示,可以看到添加用户的同时也要给用户分配角色,至少给用户分配一个或者是多个角色
页面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) @ResponseBody public Map<String,Object> regAdminUser(@Valid AdminUser adminUser, HttpServletRequest request, Errors errors,Model model){ Map<String,Object> resultMap = new HashMap<String ,Object>(); //如果有错误return当前注册页面 if(errors.hasErrors()) { resultMap.put("result","参数不合法"); } List<Role> 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 = new PasswordEncryption().encryption(adminUser.getPassword(), adminUser.getLoginName().toString()); adminUser.setPassword(encryptPassword); int count = adminUserService.addAdminUser(adminUser,roleIds); if (count > 0) { resultMap.put("result","SUCCESS"); } } } return resultMap; }
看一下adminUserService.addAdminUser(adminUser,roleIds);方法:
adminUser:用户信息
roleIds:角色编号数组
admiUserServiceImpl部分:
/** * 添加管理用户 * * @param adminUser 管理用户信息 * @param roleIds 管理用户角色组 * @return */ @Override public int addAdminUser(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); } return successCount; }
持久层部分:
AdminUserMapper.xml
<!--对应AdminUserDao接口文件中的addAdminUser方法,参数类型adminUser,使用主键自增,返回新增主键--> <insert id="addAdminUser" parameterType="adminUser" useGeneratedKeys="true" keyProperty="id" keyColumn="id" > <!--order="BEFORE"会在insert语句之前执行,使用selectKey元素自定义主键的生成 返回要插入的ID,如果没有记录,返回ID为1,否则返回最后的ID+1(每次插入递增1),这样可以自定义ID编号规则--> <selectKey keyProperty="id" resultType="int" order="BEFORE" statementType="PREPARED" > SELECT if(MAX(id)=NULL,1,MAX(id)+1) FROM t_admin </selectKey> <!--注意这里使用了动态插入sql--> INSERT INTO t_admin <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="LoginName != null">LoginName,</if> <if test="password != null">password, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT},</if> <if test="LoginName != null">#{LoginName,jdbcType=VARCHAR},</if> <if test="password != null">#{password,jdbcType=VARCHAR}, </if> </trim> </insert>
RoleMapper.xml
<!--对应RoleDao接口中的addUserRoleRelations方法--> <insert id="addUserRoleRelations"> <!--order="BEFORE"会在insert语句之前执行,使用selectKey元素自定义主键的生成 返回要插入的ID,如果没有记录,返回ID为1,否则返回最后的ID+1(每次插入递增1),这样可以自定义ID编号规则--> <selectKey keyProperty="adminRoleId" resultType="int" order="BEFORE" statementType="PREPARED" > SELECT if(MAX(adminRoleId)=NULL,1,MAX(adminRoleId)+1) FROM t_admin_role </selectKey> INSERT INTO t_admin_role(adminRoleId,adminId,roleId) VALUES (#{adminRoleId},#{userId},#{roleId}) </insert>
删除用户
adminUserController
/** * 删除管理用户 * @param request * @return */ @AccessPermissionsInfo("admin:delete") @RequestMapping(value = "/admin-user-delete.action",method = RequestMethod.POST) @ResponseBody public Map<String,Object> deleteAdminUser(HttpServletRequest request) { String str_ids = request.getParameter("ids"); str_ids = str_ids.replace("/","");//替换掉/ String[] ids = str_ids.split(","); // 用,分割 //logger.info(Arrays.toString(ids)); Map<String,Object> resultMap = new HashMap<String,Object>(); 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","登录超时,请重新登录"); } } return resultMap; }
AdminUserServiceImpl
/** * 批量删除管理用户 * * @param ids 管理用户的编号组 * @return */ @Override public int deleteAdminUserByIds(String[] ids) { //先根据用户的编号删除该用户关联的角色信息 int successCount = roleDao.deleteAdminUserRoleRelation(ids); //然后再删除用户 successCount+=adminUserDao.deleteAdminUserByIds(ids); return successCount; }
持久层
<!--批量删除管理用户的角色关联信息,对应RoleDao接口文件中的deleteAdminUserByIds方法,参数:数组,返回值为执行条数--> <delete id="deleteAdminUserRoleRelation" parameterType="java.util.Arrays"> DELETE FROM t_admin_role WHERE adminId in <foreach item="item" index="index" collection="array" open="(" separator="," close=")" > #{item} </foreach> </delete>
<!--批量删除管理用户,对应AdminUserDao接口文件中的deleteAdminUserByIds方法,参数:数组,返回值为执行条数--> <delete id="deleteAdminUserByIds" parameterType="java.util.Arrays"> DELETE FROM t_admin WHERE id in <foreach item="item" index="index" collection="array" open="(" separator="," close=")" > #{item} </foreach> </delete>
修改用户:
AdminUserController
/** * 编辑管理员 * @param request * @param model * @return */ @AccessPermissionsInfo("admin:edit") @RequestMapping(value = "/admin-user-edit.action",method = RequestMethod.POST) @ResponseBody public Map<String,Object> editAdminUser(HttpServletRequest request,Model model){ Map<String,Object> resultMap = new HashMap<String ,Object>(); List<Role> 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","没有这个用户"); } } return resultMap; }
AdminUserServiceImpl
/** * 编辑管理员用户信息 * * @param adminUser 管理用户信息 * @param roleIds 角色组 * @return */ @Override public int editAdminUser(AdminUser adminUser, String[] roleIds) { //先删除用户关联的所有角色 int successCount = roleDao.deleteAdminUserRoleRelationById(adminUser.getId().toString()); //然后循环遍历角色数组增加用户角色关系 for(String roleId :roleIds) { successCount += roleDao.addUserRoleRelations(adminUser.getId(),roleId); } return successCount; }
持久层:
RoleMapper.xml
<!--对应RoleDao接口文件中的deleteAdminUserRoles方法--> <delete id="deleteAdminUserRoleRelationById" parameterType="string" > DELETE FROM t_admin_role WHERE adminId = #{adminId} </delete>
<!--对应RoleDao接口中的addUserRoleRelations方法--> <insert id="addUserRoleRelations"> <!--order="BEFORE"会在insert语句之前执行,使用selectKey元素自定义主键的生成 返回要插入的ID,如果没有记录,返回ID为1,否则返回最后的ID+1(每次插入递增1),这样可以自定义ID编号规则--> <selectKey keyProperty="adminRoleId" resultType="int" order="BEFORE" statementType="PREPARED" > SELECT if(MAX(adminRoleId)=NULL,1,MAX(adminRoleId)+1) FROM t_admin_role </selectKey> INSERT INTO t_admin_role(adminRoleId,adminId,roleId) VALUES (#{adminRoleId},#{userId},#{roleId}) </insert>
JavaEE权限管理系统的搭建(七)--------管理用户的增删改
标签:生成 logger 自增 添加用户 增加 注册 encrypt layer 执行
原文地址:https://www.cnblogs.com/shaojiang/p/10349227.html