分析 --首先修改功能跟新增功能一样,都需要对用户名(userName),电话(telephone),邮箱(email)进行判断,但不同的是修改的时候还需要多一个判断,如果按照新增功能的逻辑去测试的时候就会发现问题....
UserController
1 /** 2 * 修改功能实现 3 * @param sysUser 4 * @param roleId 5 * @return 6 */ 7 @PostMapping("edit") 8 @ResponseBody 9 public Results edit(SysUser sysUser,Integer roleId){ 10 List<SysUser> userList = userService.getUser(sysUser.getUserName(),sysUser.getTelephone(),sysUser.getEmail()); 11 if (userList != null && userList.size()>0){ 12 for (SysUser user : userList){ 13 //如果相等,跳出当前判断 14 if (user.getId() == sysUser.getId()){ 15 continue; 16 } 17 //判断用户名是否已存在 18 if (user.getUserName().equals(sysUser.getUserName())){ 19 return Results.failure(ResponseCode.USERNAME_REPEAT.getCode(),ResponseCode.USERNAME_REPEAT.getMessage()); 20 } 21 //判断手机号是否已存在 22 if (user.getTelephone().equals(sysUser.getTelephone())){ 23 return Results.failure(ResponseCode.PHONE_REPEAT.getCode(),ResponseCode.PHONE_REPEAT.getMessage()); 24 } 25 //判断邮箱是否已存在 26 if (user.getEmail().equals(sysUser.getEmail())){ 27 return Results.failure(ResponseCode.EMAIL_REPEAT.getCode(),ResponseCode.EMAIL_REPEAT.getMessage()); 28 } 29 } 30 } 31 return userService.updateUser(sysUser,roleId); 32 } 33
UserService
UserServiceImpl
UserDao
SQL映射--UserMapper.xml
(截图有限,直接复制)
1 <!--修改用户信息--> 2 <update id="updateUser" parameterType="com.beilin.entity.SysUser"> 3 update sys_user 4 <set> 5 <if test="userName != null"> 6 user_name = #{userName}, 7 </if> 8 <if test="nickName != null"> 9 nick_name = #{nickName}, 10 </if> 11 <if test="sex != null"> 12 sex = #{sex}, 13 </if> 14 <if test="telephone != null"> 15 telephone = #{telephone}, 16 </if> 17 <if test="email != null"> 18 email = #{email}, 19 </if> 20 <if test="birthday != null"> 21 birthday = #{birthday}, 22 </if> 23 24 <if test="status != null"> 25 status = #{status}, 26 </if> 27 updateTime = #{updateTime} 28 </set> 29 where id = #{id} 30 </update>
更新完sys_user表后再更新sys_user_role表
UserRoleDao