本文最后更新于 2026年4月17日 下午
获取分页查询分类列表接口的实现
获取分页查询分类列表接口的需求
支持根据分类名称模糊查询
只查询未删除的分类
要根据状态筛选
以创建时间降序
支持分页查询
查询未删除且已发布的文章并统计文章数
代码实现
先创建 CategoryListDto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@Data @ApiModel(description = "分类列表请求对象") public class CategoryListDto { @ApiModelProperty(value = "分类ID(更新时必填)", example = "1") private Long id; @ApiModelProperty(value = "分类名称", required = true, example = "Java") private String name;
@ApiModelProperty(value = "状态", example = "0") private String status;
@ApiModelProperty(value = "分类ID列表", required = true, example = "[1,2,3]") private List<Long> ids;
@ApiModelProperty(value = "关键字(用于模糊搜索分类名称)", example = "Vue") private String keywords; }
|
再创建 AdminCategoryListVo
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
| @Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "分类响应对象") public class AdminCategoryListVo { @ApiModelProperty(value = "分类ID") private Long id;
@ApiModelProperty(value = "分类名") private String name;
@ApiModelProperty(value = "分类描述") private String description;
@ApiModelProperty(value = "状态(0-正常,1-禁用)") private String status;
@ApiModelProperty(value = "排序") private Integer sort;
@ApiModelProperty(value = "文章数量") private Integer articleCount;
@ApiModelProperty(value = "创建日期") private Date createTime; }
|
在 CategoryController 中添加 categoryList 方法
1 2 3 4 5 6 7 8 9 10 11 12
| @GetMapping @SystemLog(businessName = "分页查询分类列表") @ApiOperation(value = "分类列表", notes = "分页查询分类列表", response = CategoryVo.class, responseContainer = "List") @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "页号", dataType = "int", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", paramType = "query"), @ApiImplicitParam(name = "keywords", value = "搜索关键字", dataType = "string", paramType = "query") }) public ResponseResult categoryList(Integer pageNum, Integer pageSize, CategoryListDto categoryListDto) { return adminCategoryService.categoryList(pageNum, pageSize, categoryListDto); }
|
在 AdminCategoryService 中添加方法 这里就不写了
最后在 AdminCategoryServiceImpl 中实现方法
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
| @Autowired private ArticleService articleService;
@Override public ResponseResult categoryList(Integer pageNum, Integer pageSize, CategoryListDto categoryListDto) { LambdaQueryWrapper<Category> categoryQueryWrapper = new LambdaQueryWrapper<>();
if (StringUtils.hasText(categoryListDto.getKeywords())){ categoryQueryWrapper.like(Category::getName, categoryListDto.getKeywords()); }
categoryQueryWrapper.eq(Category::getDelFlag, SystemConstants.NOT_DELETED);
if (StringUtils.hasText(categoryListDto.getStatus())) { categoryQueryWrapper.eq(Category::getStatus, categoryListDto.getStatus()); }
categoryQueryWrapper.orderByDesc(Category::getCreateTime);
Page<Category> page = new Page<>(pageNum, pageSize); categoryService.page(page, categoryQueryWrapper);
List<Category> categoryList = page.getRecords();
final List<AdminCategoryListVo> categoryListVos = BeanCopyUtils.copyBeanList(categoryList, AdminCategoryListVo.class);
LambdaQueryWrapper<Article> articleQueryWrapper = new LambdaQueryWrapper<>(); articleQueryWrapper.eq(Article::getDelFlag, SystemConstants.NOT_DELETED) .eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL); List<Article> articleList = articleService.list(articleQueryWrapper);
for (AdminCategoryListVo categoryListVo : categoryListVos) { long count = articleList.stream() .filter(article -> article.getCategoryId().equals(categoryListVo.getId())) .count(); categoryListVo.setArticleCount((int) count); }
PageVo pageVo = new PageVo(categoryListVos, page.getTotal());
return ResponseResult.okResult(pageVo); }
|
最后测试通过
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/