『博客开发日记-后台』之获取文件列表接口的实现

本文最后更新于 2026年6月3日 晚上

获取文件列表接口的实现


获取文件列表接口的需求

根据关键字模糊查询文件

按创建时间升序排序

支持分页查询


代码实现

新建 FileListDto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 文件查询请求DTO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "文件查询请求对象")
public class FileListDto {

@ApiModelProperty(value = "类型", example = "图片")
private String type;

@ApiModelProperty(value = "关键字(用于模糊搜索文件名称)", example = "Fluid")
private String keywords;
}


再创建 FileListVo

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
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "文件列表响应对象")
public class FileListVo
{
@ApiModelProperty(value = "文件ID", example = "1")
private Long id;

@ApiModelProperty(value = "文件名称", example = "bb278d771b9c47b9ab48843f554447ff_loading_1.gif")
private String name;

@ApiModelProperty(value = "文件来源", example = "用户“里斯啊”的头像图片")
private String fileSource;

@ApiModelProperty(value = "文件类型(图片,视频,文档等....)", example = "图片")
private String type;

@ApiModelProperty(value = "文件存储路径", example = "文件oss中的存储路径")
private String filePath;

@ApiModelProperty(value = "文件URL,图片预览URL", example = "域名 + 文件oss中的存储路径")
private String url;

@ApiModelProperty(value = "文件大小(字节)", example = "363856")
private Long size;

@ApiModelProperty(value = "MIME类型", example = "image/gif")
private String mimeType;

@TableField(exist = false)
@ApiModelProperty(value = "创建者名字", example = "云梦泽")
private String creatorName;

@ApiModelProperty(value = "创建时间", example = "2026-05-08 14:40:22")
private Date createTime;

@ApiModelProperty(value = "更新时间", example = "2026-05-08 14:40:50")
private Date updateTime;
}

在 SysFileServiceImpl 中实现方法

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//获取文件列表
@Override
public ResponseResult<PageVo> getFileList(Integer pageNum, Integer pageSize, FileListDto fileListDto)
{
//构建查询条件
LambdaQueryWrapper<SysFile> queryWrapper = new LambdaQueryWrapper<>();

//根据关键字(文件名字,文件来源)模糊查询文件
if (StringUtils.hasText(fileListDto.getKeywords())) {
queryWrapper.and(wrapper -> wrapper
.like(SysFile::getName, fileListDto.getKeywords())
.or()
.like(SysFile::getFileSource, fileListDto.getKeywords())
);
}

//按创建时间倒序排序
queryWrapper.orderByDesc(SysFile::getCreateTime);

//分页查询
Page<SysFile> page = new Page<>(pageNum, pageSize);
sysFileService.page(page, queryWrapper);

//封装成vo返回
List<SysFile> fileRecords = page.getRecords();
List<FileListVo> fileListVos = BeanCopyUtils.copyBeanList(fileRecords, FileListVo.class);
fillCreatorName(fileRecords, fileListVos);
PageVo pageVo = new PageVo(fileListVos, page.getTotal());

return ResponseResult.okResult(pageVo);
}

//根据创建者id转换创建者名字
private void fillCreatorName(List<SysFile> fileRecords, List<FileListVo> fileListVos)
{
//文件列表为空时直接返回
if (fileRecords == null || fileRecords.isEmpty() || fileListVos == null || fileListVos.isEmpty()) {
return;
}

//收集文件记录中的创建者id并过滤空id
Set<Long> creatorIds = fileRecords.stream()
.map(SysFile::getCreateBy)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (creatorIds.isEmpty()) {
return;
}

//根据创建者id批量查询用户信息
Map<Long, String> creatorNameMap = sysUserService.list(
Wrappers.<SysUser>lambdaQuery()
.select(SysUser::getId, SysUser::getNickname, SysUser::getUsername)
.in(SysUser::getId, creatorIds)
).stream()
.collect(Collectors.toMap(
SysUser::getId,
//优先使用用户昵称作为创建者名字;如果昵称为空则使用用户名
user -> StringUtils.hasText(user.getNickname()) ? user.getNickname() : user.getUsername(),
(oldName, newName) -> oldName
));

//将创建者id转换后的创建者名字设置到Vo中
for (int i = 0; i < fileRecords.size(); i++) {
Long creatorId = fileRecords.get(i).getCreateBy();
fileListVos.get(i).setCreatorName(creatorNameMap.get(creatorId));
}
}




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

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


预告

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

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

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


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


『博客开发日记-后台』之获取文件列表接口的实现
http://example.com/2026/05/06/『博客开发日记-后台』之获取文件列表接口的实现/
作者
云梦泽
发布于
2026年5月6日
更新于
2026年6月3日
许可协议