『博客开发日记-后台』之查询文章列表接口的实现

本文最后更新于 2026年4月12日 晚上

后台查询文章列表接口的实现


后台查询文章列表接口的需求

根据分类进行查询

根据文章标题进行查询

根据文章状态进行查询

列表中要有 标题、所属分类、所属标签、状态、浏览量、等数据

按创建时间降序排序

代码实现

首先要说明的是获取文章列表在前台也有

但是他们的功能和分工不一样

所以要将前台和后台的实现代码分开

先创建后台的PostsController类

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
/**
* 后台文章管理接口
*/
@RestController
@RequestMapping("/posts")
@Api(tags = "后台文章管理", description = "后台文章管理相关接口")
public class PostsController
{
@Autowired
private AdminPostsService adminPostsService;

@GetMapping
@SystemLog(businessName = "后台分页查询文章列表")
@ApiOperation(value = "后台文章列表", notes = "分页查询文章列表,支持多条件筛选", response = ArticleListVo.class, responseContainer = "List")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "页号", dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "categoryId", value = "分类id", dataType = "long", paramType = "query"),
@ApiImplicitParam(name = "status", value = "文章状态", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "keywords", value = "搜索关键字", dataType = "string", paramType = "query")
})
public ResponseResult<ArticleListVo> postsList(Integer pageNum, Integer pageSize, PostsListDto postsListDto)
{
return adminPostsService.postsList(pageNum, pageSize, postsListDto);
}

}

创建PostsListDto

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
/**
* 文章列表操作请求DTO
*/
@Data
@ApiModel(description = "文章列表操作请求对象")
public class PostsListDto
{
@ApiModelProperty(value = "文章ID(更新时必填)", example = "1")
private Long id;

@ApiModelProperty(value = "文章标题", required = true, example = "测试标题")
private String title;

@ApiModelProperty(value = "分类id", example = "1")
private Long categoryId;

@ApiModelProperty(value = "文章状态(已发布,草稿)", example = "草稿")
private String status;

@ApiModelProperty(value = "文章ID列表", required = true, example = "[1,2,3]")
private List<Long> ids;

@ApiModelProperty(value = "关键字(用于模糊搜索文章标题)", example = "标题")
private String keywords;
}

创建AdminPostsListVo

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
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "后台文章列表响应对象")
public class AdminPostsListVo
{
@ApiModelProperty(value = "文章ID")
private Long id;

@ApiModelProperty(value = "文章标题")
private String title;

@ApiModelProperty(value = "文章状态(0-已发布,1-草稿)")
private String status;

@ApiModelProperty(value = "所属分类名")
private String categoryName;

@ApiModelProperty(value = "访问量")
private Long viewCount;

@ApiModelProperty(value = "创建时间")
private Date createTime;

@ApiModelProperty(value = "标签列表")
private List<TagVo> tags;
}

再创建AdminPostsService和AdminPostsServiceImpl

在AdminPostsServiceImpl里实现功能

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* 后台文章管理服务实现类
*
* @author mengze
*/
@Service
public class AdminPostsServiceImpl implements AdminPostsService
{

@Autowired
private ArticleService articleService;

@Autowired
private CategoryService categoryService;

@Autowired
private RedisCache redisCache;

@Autowired
private ArticleTagMapper articleTagMapper;

@Autowired
private TagService tagService;

//获取文章列表
@Override
public ResponseResult postsList(Integer pageNum, Integer pageSize, PostsListDto postsListDto)
{
//构建查询条件
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();

//根据分类id筛选
if (Objects.nonNull(postsListDto.getCategoryId()) && postsListDto.getCategoryId() > 0) {
queryWrapper.eq(Article::getCategoryId, postsListDto.getCategoryId());
}

//根据状态筛选
if (StringUtils.hasText(postsListDto.getStatus())) {
queryWrapper.eq(Article::getStatus, postsListDto.getStatus());
}

//根据关键词搜索(标题或摘要)
if (StringUtils.hasText(postsListDto.getKeywords())) {
queryWrapper.and(wrapper -> wrapper
.like(Article::getTitle, postsListDto.getKeywords())
.or()
.like(Article::getSummary, postsListDto.getKeywords())
);
}

//置顶的文章要放在前面
queryWrapper.orderByDesc(Article::getIsTop);
//按创建时间降序排序
queryWrapper.orderByDesc(Article::getCreateTime);

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

//填充分类名称和浏览量
List<Article> articles = page.getRecords();
articles.forEach(article -> {
article.setCategoryName(categoryService.getById(article.getCategoryId()).getName());
//从redis中获取viewCount
Integer viewCount = redisCache.getCacheMapValue(SystemConstants.ARTICLE_VIEW_COUNT, article.getId().toString());
article.setViewCount(viewCount.longValue());
});

//封装查询结果为Vo
List<AdminPostsListVo> postsListVos = BeanCopyUtils.copyBeanList(page.getRecords(), AdminPostsListVo.class);

//填充标签信息
postsListVos.forEach(postsListVo -> {
List<TagVo> tags = getTagsByArticleId(postsListVo.getId());
postsListVo.setTags(tags);
});

PageVo pageVo = new PageVo(postsListVos, page.getTotal());
return ResponseResult.okResult(pageVo);
}

/**
* 根据文章ID查询标签列表
* @param articleId 文章ID
* @return 标签列表
*/
private List<TagVo> getTagsByArticleId(Long articleId)
{
//查询文章标签关联表
LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ArticleTag::getArticleId, articleId);
List<ArticleTag> articleTags = articleTagMapper.selectList(queryWrapper);

//获取标签ID列表
List<Long> tagIds = articleTags.stream()
.map(ArticleTag::getTagId)
.collect(Collectors.toList());

//如果没有标签,返回空列表
if (tagIds.isEmpty()) {
return List.of();
}

//查询标签信息
List<Tag> tags = tagService.listByIds(tagIds);

//转换为TagVo
return tags.stream()
.map(tag -> {
TagVo tagVo = new TagVo();
tagVo.setId(tag.getId());
tagVo.setName(tag.getName());
tagVo.setCreateTime(tag.getCreateTime());
return tagVo;
})
.collect(Collectors.toList());
}
}



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

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


预告

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

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

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


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


『博客开发日记-后台』之查询文章列表接口的实现
http://example.com/2026/04/11/『博客开发日记-后台』之查询文章列表接口的实现/
作者
云梦泽
发布于
2026年4月11日
更新于
2026年4月12日
许可协议