Introduction of Mybatis Pagination Methods

本文介绍了在日常开发中常用的分页设计,讲解了Mybatis框架内的两种分页方法:RowBounds和PageHelper。RowBounds类能自动进行分页,但获取总页数等信息较为不便。PageHelper插件则更方便,不仅自动处理分页,还能提供详细的页面信息。

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

Pagination is commonly used in daily development design. Today's tutorial will introduce two methods to help us achieve pagination in Mybatis.

One: RowBounds method inside Mybatis.

There is a default Class in Mybatis package called RowBounds that can help us paginate our pages automatically.

public class RowBounds {
    public static final int NO_ROW_OFFSET = 0;
    public static final int NO_ROW_LIMIT = 2147483647;
    public static final RowBounds DEFAULT = new RowBounds();
    private final int offset;
    private final int limit;

    public RowBounds() {
        this.offset = 0;
        this.limit = 2147483647;
    }

    public RowBounds(int offset, int limit) {
        this.offset = offset;
        this.limit = limit;
    }

    public int getOffset() {
        return this.offset;
    }

    public int getLimit() {
        return this.limit;
    }
}

Here is the RowBounds class. Two parameters are necessary for the constructor. Offset means the start point of our list, and the limit parameter indicates the page size. For instance, {offset = 5, limit = 4} means start from List[4] to List[7].

@Select("SELECT * FROM t_student")
    @ResultMap("studentMap")
    public List<StudentBean> queryPagesByRowBounds(RowBounds rowBounds);

Above is our Mybatis map class. By using RowBounds class, we don't need to add "limit" in our SQL sentence. Mybatis will configure it itself;

 @Override
    public List<StudentBean> queryByRowBounds(int pageNum, int limit) {
//convert page number to start point.
     int offset = (pageNum-1)*limit;
        RowBounds rowBounds = new RowBounds(offset,limit);
        return studentDao.queryPagesByRowBounds(rowBounds);
    }

Therefore, in the service layer, we can directly obtain page numbers and limits. Convert it to a RowBounds object and inject it into the repository layer in parameter. However, it is still inconvenient as we need to get the page count and other information using different code. In Github, some teams developed a plugin called "PageHelper" can help us solve this issue.

Second method: PageHelper

The first step is setting up dependency in pom.

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.0</version>
        </dependency>
@Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties pageHelperProperties = new Properties();
        pageHelperProperties.put("helperDialect", "mysql");
        pageHelper.setProperties(pageHelperProperties);
        return pageHelper;
    }

After that, we need to add our pageHelper bean in Java configuration Class. You can also choose to add this in XML file. There are many different properties that can be set up including automatic limit last and start page. For more information, you can refer to Mybatis-PageHelper/HowToUse.md at master · pagehelper/Mybatis-PageHelper · GitHub

sqlSessionFactoryBean.setPlugins(new PageInterceptor());

One crucial point is don't forget setPlugins in sqlSessioFactoryBean configuration.

public List<StudentBean> queryByPageHelper(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<StudentBean> page = PageInfo.of(studentDao.queryPagesByPageHelper());
        System.out.println(page);
        return page.getList();
    }

In our service layer, we can initialize the PageHelper directly using static method "startPage". Same as in RowBounds tutorial, we don't need to append "limit" in our SQL query. The pageHelper would process the page number and the page size automatically. The amazing characteristic is that the PageHelper will created a "pageInfo" object, which contains all useful information like page count, page size etc. 

PageInfo{pageNum=2, pageSize=3, size=3, startRow=4, endRow=6, total=10, pages=4, list=Page{count=true, pageNum=2, pageSize=3, startRow=3, endRow=6, total=10, pages=4, reasonable=false, pageSizeZero=false}[StudentBean{studentId=4, studentName='成华小次郎, StudentBean{studentId=5, studentName='陈浩楠, StudentBean{studentId=6, studentName='特朗普], prePage=1, nextPage=3, isFirstPage=false, isLastPage=false, hasPreviousPage=true, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=4, navigatepageNums=[1, 2, 3, 4]}
Page{count=true, pageNum=2, pageSize=3, startRow=3, endRow=6, total=10, pages=4, reasonable=false, pageSizeZero=false}[StudentBean{studentId=4, studentName='成华小次郎, StudentBean{studentId=5, studentName='陈浩楠, StudentBean{studentId=6, studentName='特朗普]

Above is inside the PageInfo object.

Conclusion

There are two ways for us to do pagination in Java using Mybatis framework. One is the RowBounds class, which is coded inside the Mybatis framework and can process offset point and page size automatically. The other is a developed plugin called pageHelper, which is useful since it can return many detailed information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值