feat(文章): 添加根据状态分页查询文章功能

实现文章列表的分页查询功能,包括:
- 在ArticleRepository添加分页查询方法
- 在IArticleService和ArticleService添加分页接口和实现
- 新增PageDto用于分页参数校验
- 在ArticleController添加分页接口
This commit is contained in:
qingfeng1121
2025-11-14 15:30:39 +08:00
parent 47c357695b
commit eb1f70d431
14 changed files with 5649 additions and 15226 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -3,7 +3,9 @@ package com.qf.myafterprojecy.controller;
import com.qf.myafterprojecy.config.ResponseMessage;
import com.qf.myafterprojecy.pojo.Article;
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
import com.qf.myafterprojecy.pojo.dto.PageDto;
import com.qf.myafterprojecy.service.imp.IArticleService;
import org.springframework.data.domain.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -52,6 +54,18 @@ public class ArticleController {
return articleService.getArticlesByStatus(status);
}
/**
* 根据状态分页获取文章列表
* @param status 文章状态0未发表 1已发表 2已删除
* @param page 页码从0开始可选默认为0
* @param size 每页大小可选默认为10最大为100
* @return 返回包含分页文章列表的ResponseMessage对象
*/
@GetMapping("/status/page/{status}")
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(@PathVariable PageDto pageDto) {
return articleService.getArticlesByStatusWithPagination(pageDto.getStatus(), pageDto.getPage(), pageDto.getSize());
}
/**
* 获取所有文章列表
* @return 返回包含文章列表的ResponseMessage对象
@@ -60,6 +74,7 @@ public class ArticleController {
public ResponseMessage<List<Article>> getAllArticles() {
return articleService.getAllArticles();
}
/**
* 根据标题查询文章列表
* @param title 文章标题

View File

@@ -0,0 +1,42 @@
package com.qf.myafterprojecy.pojo.dto;
import javax.ws.rs.DefaultValue;
public class PageDto {
private Integer status;
@DefaultValue("0")
private Integer page;
@DefaultValue("10")
private Integer size;
// 数据验证
public void validate() {
if ( status > 0 && status <= 2) {
throw new IllegalArgumentException("Status must be 0 or 2");
}
if (page < 0) {
throw new IllegalArgumentException("Page number must be non-negative");
}
if (size <= 0 || size > 100) {
throw new IllegalArgumentException("Page size must be between 1 and 100");
}
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}

View File

@@ -1,6 +1,8 @@
package com.qf.myafterprojecy.repository;
import com.qf.myafterprojecy.pojo.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
@@ -87,4 +89,13 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
*/
@Query("SELECT a FROM Article a WHERE a.status = :status")
List<Article> findByStatus(@Param("status") Integer status);
/**
* 根据状态分页查询文章列表
* @param status 文章状态0-草稿1-已发布2-已删除
* @param pageable 分页参数,包含页码、每页大小和排序信息
* @return 返回符合状态条件的文章分页结果
*/
@Query("SELECT a FROM Article a WHERE a.status = :status ORDER BY a.createdAt DESC")
Page<Article> findByStatusWithPagination(@Param("status") Integer status, Pageable pageable);
}

View File

@@ -12,6 +12,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -98,6 +100,32 @@ public class ArticleService implements IArticleService {
return ResponseMessage.error("获取已发布文章列表失败");
}
}
@Override
@Transactional(readOnly = true)
public ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(Integer status, Integer page, Integer size) {
try {
if (status == null) {
return ResponseMessage.badRequest("文章状态不能为空");
}
if (status < 0 || status > 2) {
return ResponseMessage.badRequest("文章状态值必须在0到2之间");
}
if (page == null || page < 0) {
page = 0; // 默认第一页
}
if (size == null || size <= 0 || size > 100) {
size = 10; // 默认每页10条最大100条
}
PageRequest pageRequest = PageRequest.of(page, size);
Page<Article> articlePage = articleRepository.findByStatusWithPagination(status, pageRequest);
return ResponseMessage.success(articlePage, "根据状态分页查询文章成功");
} catch (Exception e) {
log.error("根据状态分页查询文章列表失败: {}", e.getMessage());
return ResponseMessage.error("根据状态分页查询文章列表失败");
}
}
@Override
@Transactional(readOnly = true)
public ResponseMessage<List<Article>> getArticlesByTitle(String title) {

View File

@@ -3,6 +3,7 @@ package com.qf.myafterprojecy.service.imp;
import com.qf.myafterprojecy.config.ResponseMessage;
import com.qf.myafterprojecy.pojo.Article;
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
import org.springframework.data.domain.Page;
import java.util.List;
@@ -89,4 +90,13 @@ public interface IArticleService {
* @return 返回包含已发布文章列表的ResponseMessage对象
*/
ResponseMessage<List<Article>> getPublishedArticles();
/**
* 根据状态分页查询文章列表
* @param status 文章状态0未发表 1已发表 2已删除
* @param page 页码从0开始
* @param size 每页大小
* @return 返回包含分页文章列表的ResponseMessage对象
*/
ResponseMessage<Page<Article>> getArticlesByStatusWithPagination(Integer status, Integer page, Integer size);
}