本文最后更新于 2026年5月20日 晚上
删除用户接口的实现
删除用户接口的需求
先检查用户是否存在
要禁止删除系统管理员用户
要注意的是
不应该将用户和角色的关联关系删除,避免后续恢复用户时丢失角色配置
代码实现
在 AdminUserServiceImpl 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| @Override @Transactional public ResponseResult deleteUser(Long[] ids) { if (ids == null || ids.length == 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "请选择要删除的用户"); }
List<Long> userIdList = Arrays.asList(ids); List<SysUser> usersToDelete = adminUserService.listByIds(userIdList); List<Long> adminUserIds = usersToDelete.stream() .map(SysUser::getId) .filter(id -> Objects.equals(id, 1L)) .toList(); if (!adminUserIds.isEmpty()) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "系统管理员用户不能删除!"); }
if (usersToDelete.size() != ids.length) { Set<Long> existIds = usersToDelete.stream() .map(SysUser::getId) .collect(Collectors.toSet());
List<Long> notExistIds = userIdList.stream() .filter(id -> !existIds.contains(id)) .toList();
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "删除失败!!原因:以下用户 id 不存在:" + notExistIds); }
boolean removed = adminUserService.removeByIds(userIdList); if (!removed) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "删除用户失败"); }
adminUserService.removeByIds(userIdList);
return ResponseResult.okResult(); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/