Springboot 2.3.5使用 pagehelper 实现分页

参考

  1. SpringBoot - 获取Get请求参数详解(附样例:非空、默认值、数组、对象)
  2. 如何使用 PageHelper 一篇博客就够了
  3. pageHelper详解
  4. Mybatis中的resultType和resultMap
  5. 如何使用分页插件
  6. springboot实现MyBatis分页
  7. Spring Boot:实现MyBatis分页
  8. mybatis 中文文档 配置

正文

  1. pom.xml 节点 dependencies 下添加 pagehelper 依赖
<!--        分页工具-->
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
  1. mapper.xml 添加 id 为 findByPaging 的查(直接查询所有即可,因为在 Service中的 PageHelper.startPage(pageNum, pageSize); 会对这个语句下面的第一个查询进行分页)
    <select id="findByPaging" resultType="com.xxx.blog.entity.ArticleEntity" parameterType="map">
        select
        *
        from `articles`
    </select>
  1. ArticleMapper.java
@Mapper
public interface ArticleMapper {
    public ArticleEntity findById(Integer id);
    public List<ArticleEntity> findByPaging(Map param);
}
  1. ArticleService.java 新增 findByPaging 方法,将查询出的数据转换为 分页类型,对控制器隐藏处理过程
@Service
public class ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    public ArticleEntity findById(Integer id){
        return articleMapper.findById(id);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param map
     * @return
     */
    public PageInfo findByPaging(Integer pageNum, Integer pageSize,Map map){
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleEntity> articleEntityPage = articleMapper.findByPaging(map);
        PageInfo pageInfo = new PageInfo(articleEntityPage);
        return pageInfo;
    }
}
  1. ArticleController.java 下新增 list 方法
@RestController
@RequestMapping("v1/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @GetMapping(value = "", produces = "application/json;charset=UTF-8")
    public ResponseEntity<CustomResponseStructureDto<PageInfo>> list(@RequestParam(name = "page_num", defaultValue = "1") Integer pageNum, @RequestParam(name = "page_size",defaultValue = "10") Integer pageSize){
        Map<String, String> map = new HashMap<>();
        PageInfo pageInfo = articleService.findByPaging(pageNum, pageSize, map);
        return ResponseEntity.ok(new CustomResponseStructureDto(0, pageInfo, "成功"));
    }

    @GetMapping(value = "{id}", produces = "application/json;charset=UTF-8")
    public ResponseEntity<CustomResponseStructureDto<ArticleEntity>> list(@PathVariable("id") Integer id){
        ArticleEntity article = articleService.findById(id);
        return ResponseEntity.ok(new CustomResponseStructureDto<ArticleEntity>(0, article, "成功"));
    }
}
  1. 测试 localhost:8080/v1/article?page_num=2&page_size=1
{
    "code": 0,
    "msg": "成功",
    "data": {
        "total": 2,
        "list": [
            {
                "id": 4,
                "title": "测试文章002",
                "introduction": "描述内容",
                "created_at": null,
                "updated_at": null,
                "content": "内容12131内容12131内容12131内容12131内容12131内容12131",
                "shows": 0,
                "likes": 0
            }
        ],
        "pageNum": 2,
        "pageSize": 1,
        "size": 1,
        "startRow": 2,
        "endRow": 2,
        "pages": 2,
        "prePage": 1,
        "nextPage": 0,
        "isFirstPage": false,
        "isLastPage": true,
        "hasPreviousPage": true,
        "hasNextPage": false,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 2
    }
}
posted @   夏秋初  阅读(289)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示