Mybatis-PageHelper 学习

本文详细介绍Mybatis-PageHelper分页插件的使用方法,包括传统项目和SpringBoot项目的依赖引入,配置参数说明如offsetAsPageNum、rowBoundsWithCount等,以及如何通过PageHelper和PageInfo实现分页查询。

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

传统项目:

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.0</version>
        </dependency> 

 

springboot 项目引入:


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

git地址: 

github项目地址:https://github.com/pagehelper/Mybatis-PageHelper 
码云 项目地址:http://git.oschina.net/free/Mybatis_PageHelper

 

配置文件配置:

 offsetAsPageNum  //offset作为PageNum使用
 rowBoundsWithCount  //RowBounds方式是否做count查询
 pageSizeZero   //当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页
 reasonable   //分页合理化,true开启,如果分页参数不合理会自动修正。默认false不启用

reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。

supportMethodsArguments //是否支持接口参数来传递分页参数,默认false
pagehelper.helper-dialect= Mysql
pagehelper.page-size-zero= true
pagehelper.row-bounds-with-count= false
pagehelper.reasonable= true
 

使用:

        PageHelper.startPage(currentPage, pageSize);        
        List<DataRole> roles = dataRoleMapper.selectAll(name);
        PageInfo<DataRole> newsPageInfo = new PageInfo<>(roles);

通过类配置:

@Configuration
public class MyBatisConfiguration {
    @Bean
    public PageHelper pageHelper(){
        System.out.println("MyBatisConfiguration.pageHelper");
        PageHelper pageHelper=new PageHelper();
        Properties p=new Properties();
        p.setProperty("offsetAsPageNum","true");
        p.setProperty("rowBounfsWithCount","false");
        p.setProperty("reasonable","true");
        pageHelper.setProperties(p);
        return  pageHelper;
    }
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值