pageHelper的基本使用

本文介绍如何在Spring Boot项目中使用PageHelper分页插件实现数据分页功能。通过配置依赖、定义Service接口及其实现类,最终在控制器中展示分页数据。文章详细展示了实现过程及关键代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

maven项目中,首先添加依赖,然后在service层中,调用
public interface IRealAuthService {
    PageInfo queryPage(RealAuthQueryObject qo);

}

在实现类中编写方法

public class RealAuthServiceImpl implements IRealAuthService{
@Override
    public PageInfo queryPage(RealAuthQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List result = realAuthMapper.queryForList(qo);
        return new PageInfo(result);

    }
}
qo为:
public class QueryObject {
    private int currentPage=1;
    private int pageSize=5;
}

public class RealAuthQueryObject extends QueryObject {


}

controller控制器:

public class RealAuthController {

    @Autowired
    private IRealAuthService realAuthService;

    @RequestMapping("/realAuth")
    public String realAuthPage(@ModelAttribute("qo") RealAuthQueryObject qo, Model model){
        PageInfo pageResult=realAuthService.queryPage(qo);
        model.addAttribute("pageResult",pageResult);
        return "/realAuth/list";
    }
}


PageHelper中默认PageInfo的成员变量

//当前页  
    private int pageNum;
    //每页的数量  
    private int pageSize;
    //当前页的数量  
    private int size;
    //由于startRow和endRow不常用,这里说个具体的用法  
    //可以在页面中"显示startRow到endRow 共size条数据"  

    //当前页面第一个元素在数据库中的行号  
    private int startRow;
    //当前页面最后一个元素在数据库中的行号  
    private int endRow;
    //总记录数  
    private long total;
    //总页数  
    private int pages;
    //结果集  
    private List<T> list;

    //第一页  
    private int firstPage;
    //前一页  
    private int prePage;

    //是否为第一页  
    private boolean isFirstPage = false;
    //是否为最后一页  
    private boolean isLastPage = false;
    //是否有前一页  
    private boolean hasPreviousPage = false;
    //是否有下一页  
    private boolean hasNextPage = false;
    //导航页码数  
    private int navigatePages;
    //所有导航页号  
    private int[] navigatepageNums;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值