springboot项目中使用MyBatis分页插件

本文介绍了如何在SpringBoot项目中集成PageHelper分页插件,并通过测试类展示了如何设置分页参数,查询数据并获取总记录数和当前页数据。
摘要由CSDN通过智能技术生成

pom.xml 

<!-- 分页插件 -->
 <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.4.6</version>
 </dependency>

测试类

package com.cc.bigevent;

import com.cc.bigevent.mapper.ArticleMapper;
import com.cc.bigevent.pojo.Article;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
@SpringBootTest
public class PageHelperTest {
    @Resource
    ArticleMapper articleMapper;
    @Test
    public void test(){
        //当前页数
        int pageNum = 1;
        //每页数据条数
        int pageSize = 5;
        //开启mybatis分页查询
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Article> articles = articleMapper.list(10, 2, "草稿");
        //强转获得当前页数据
        Page<Article> articlePage = (Page<Article>) articles;
        //当前页数据条数
        System.out.println(articlePage.getTotal());
        //当前页数据集合
        System.out.println(articlePage.getResult());
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值