『博客开发日记-后台』之获取用户表单数据接口的实现

本文最后更新于 2026年5月17日 晚上

获取用户表单数据接口的实现


获取用户表单数据接口的需求

根据用户id查询表单数据

注意还要查该用户所属的角色id列表

这涉及到用户角色关联表


代码实现

创建 UserFormDetailVo

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
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "后台用户表单详情响应对象")
public class UserFormDetailVo
{
@ApiModelProperty(value = "用户id", example = "1")
private Long id;

@ApiModelProperty(value = "头像URL", example = "https://avatars.githubusercontent.com/u/155459505?v=4")
private String avatar;

@ApiModelProperty(value = "部门id", example = "测试不")
private String deptId;

@ApiModelProperty(value = "用户邮箱", example = "2962933152@qq.com")
private String email;

@ApiModelProperty(value = "用户性别,(0 男,1 女,2 隐藏)", example = "0")
private String sex;

@ApiModelProperty(value = "手机号", example = "18329384753")
@JSONField(name = "mobile")//转换成 mobile 再响应给前端
private String phone;

@ApiModelProperty(value = "昵称", example = "云梦泽")
private String nickname;

@ApiModelProperty(value = "角色id列表", example = "[1,2,3]")
private List<Long> roleIds;

@ApiModelProperty(value = "账号状态(0 正常,1 停用)", example = "0")
private String status;

@ApiModelProperty(value = "用户名", example = "云梦泽")
private String username;
}

在 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
  @Autowired
private SysUserRoleMapper sysUserRoleMapper;

@Autowired
private SysUserService sysUserService;

//获取用户表单数据
@Override
public ResponseResult<UserFormDetailVo> getUserFormDetail(Long userId)
{
//检查用户是否存在
SysUser sysUser = sysUserService.getById(userId);
if (sysUser == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该用户不存在!");
}

//转换vo
UserFormDetailVo userFormDetailVo = BeanCopyUtils.copyBean(sysUser, UserFormDetailVo.class);

//填充角色id列表
List<Long> roleIds = getRoleIdsByUserId(userId);
userFormDetailVo.setRoleIds(roleIds);

return ResponseResult.okResult(userFormDetailVo);
}


/**
* 根据用户ID查询角色id列表
* @param userId 用户ID
* @return 角色ID列表
*/
private List<Long> getRoleIdsByUserId(Long userId)
{
//查询用户角色关联表
LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUserRole::getUserId, userId);
List<SysUserRole> UserRoles = sysUserRoleMapper.selectList(queryWrapper);

//返回角色ID列表
return UserRoles.stream()
.map(SysUserRole::getRoleId)
.collect(Collectors.toList());
}



PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记-后台』之获取用户表单数据接口的实现
http://example.com/2026/05/17/『博客开发日记-后台』之获取用户表单数据接口的实现/
作者
云梦泽
发布于
2026年5月17日
更新于
2026年5月17日
许可协议