分页查询分类

This commit is contained in:
lizhengwei 2026-03-09 18:18:36 +08:00
parent 35c829150d
commit 5b7a447fd6
3 changed files with 61 additions and 12 deletions

View File

@ -1,7 +1,9 @@
package com.luban.api.controller;
import com.luban.api.dto.H5ComponentCategoryQueryDTO;
import com.luban.api.entity.H5ComponentCategory;
import com.luban.api.service.H5ComponentCategoryService;
import com.luban.api.vo.H5ComponentCategoryPageVO;
import com.luban.api.vo.H5ComponentCategoryTreeVO;
import com.luban.api.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
@ -27,19 +29,12 @@ public class H5ComponentCategoryController {
private final H5ComponentCategoryService h5ComponentCategoryService;
@Operation(summary = "查询所有一级分类")
@GetMapping("/top-level")
public Result<List<H5ComponentCategory>> getTopLevelCategories() {
List<H5ComponentCategory> result = h5ComponentCategoryService.getTopLevelCategories();
return new Result<>(result);
}
@Operation(summary = "根据父分类 ID 查询子分类")
@GetMapping("/children")
public Result<List<H5ComponentCategory>> getChildCategories(
@Parameter(description = "父分类 ID", required = true) @RequestParam Long parentId) {
@Operation(summary = "分页查询分类", description = "支持查询一级分类和子分类。不传 parentId 时查询一级分类,传入 parentId 时查询对应子分类")
@GetMapping("/page")
public Result<H5ComponentCategoryPageVO> pageCategories(
@Parameter(description = "查询参数") @ModelAttribute H5ComponentCategoryQueryDTO queryDTO) {
List<H5ComponentCategory> result = h5ComponentCategoryService.getChildCategories(parentId);
H5ComponentCategoryPageVO result = h5ComponentCategoryService.pageCategories(queryDTO);
return new Result<>(result);
}

View File

@ -1,6 +1,8 @@
package com.luban.api.service;
import com.luban.api.dto.H5ComponentCategoryQueryDTO;
import com.luban.api.entity.H5ComponentCategory;
import com.luban.api.vo.H5ComponentCategoryPageVO;
import com.luban.api.vo.H5ComponentCategoryTreeVO;
import java.util.List;
@ -64,4 +66,12 @@ public interface H5ComponentCategoryService {
* @return 树形分类列表
*/
List<H5ComponentCategoryTreeVO> getCategoryTree();
/**
* 分页查询分类支持一级分类和子分类
*
* @param queryDTO 查询参数
* @return 分页查询结果
*/
H5ComponentCategoryPageVO pageCategories(H5ComponentCategoryQueryDTO queryDTO);
}

View File

@ -1,10 +1,13 @@
package com.luban.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luban.api.dao.H5ComponentCategoryDao;
import com.luban.api.dto.H5ComponentCategoryQueryDTO;
import com.luban.api.entity.H5ComponentCategory;
import com.luban.api.service.H5ComponentCategoryService;
import com.luban.api.vo.H5ComponentCategoryPageVO;
import com.luban.api.vo.H5ComponentCategoryTreeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -152,4 +155,45 @@ public class H5ComponentCategoryServiceImpl extends ServiceImpl<H5ComponentCateg
BeanUtils.copyProperties(entity, vo);
return vo;
}
@Override
public H5ComponentCategoryPageVO pageCategories(H5ComponentCategoryQueryDTO queryDTO) {
// 构建查询条件
LambdaQueryWrapper<H5ComponentCategory> wrapper = new LambdaQueryWrapper<>();
// 如果 parentId null默认查询一级分类parentId = 0
if (queryDTO.getParentId() == null) {
wrapper.eq(H5ComponentCategory::getParentId, 0L);
} else {
// 否则查询指定 parentId 的子分类
wrapper.eq(H5ComponentCategory::getParentId, queryDTO.getParentId());
}
// 类型过滤
wrapper.eq(queryDTO.getType() != null && !queryDTO.getType().isBlank(),H5ComponentCategory::getType, queryDTO.getType())
.like(queryDTO.getName() != null && !queryDTO.getName().isBlank(),H5ComponentCategory::getName, queryDTO.getName());
// 排序
wrapper.orderByAsc(H5ComponentCategory::getSort);
// 分页参数
int pageNum = queryDTO.getPage() != null ? queryDTO.getPage() : 1;
int pageSize = queryDTO.getSize() != null ? queryDTO.getSize() : 10;
// 执行分页查询
Page<H5ComponentCategory> page = new Page<>(pageNum, pageSize);
Page<H5ComponentCategory> resultPage = baseMapper.selectPage(page, wrapper);
// 构建返回结果
H5ComponentCategoryPageVO pageVO = new H5ComponentCategoryPageVO();
pageVO.setList(resultPage.getRecords());
pageVO.setTotal(resultPage.getTotal());
pageVO.setPageNum((int) resultPage.getCurrent());
pageVO.setPageSize((int) resultPage.getSize());
pageVO.setHasPrevious(resultPage.getCurrent() > 1);
pageVO.setHasNext(resultPage.getCurrent() < resultPage.getPages());
return pageVO;
}
}