pagebounds mysql_Mybatis之RowBounds分页原理详解

Mybatis可以通过传递RowBounds对象,来进行数据库数据的分页操作,然而遗憾的是,该分页操作是对ResultSet结果集进行分页,也就是人们常说的逻辑分页,而非物理分页。

RowBounds对象的源码如下:

public class RowBounds {

public static final int NO_ROW_OFFSET = 0;

public static final int NO_ROW_LIMIT = Integer.MAX_VALUE;

public static final RowBounds DEFAULT = new RowBounds();

private int offset;

private int limit;

public RowBounds() {

this.offset = NO_ROW_OFFSET;

this.limit = NO_ROW_LIMIT;

public RowBounds(int offset, int limit) {

this.offset = offset;

this.limit = limit;

public int getOffset() {

return offset;

public int getLimit() {

return limit;

对数据库数据进行分页,依靠offset和limit两个参数,表示从第几条开始,取多少条。也就是人们常说的start,limit。

下面看看Mybatis的如何进行分页的。

org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap()方法源码。

private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping)

throws SQLException {

DefaultResultContext Object resultContext = new DefaultResultContext Object

// 跳到offset位置,准备读取

skipRows(rsw.getResultSet(), rowBounds);

// 读取limit条数据

while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {

ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);

Object rowValue = getRowValue(rsw, discriminatedResultMap);

storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());

private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {

if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {

if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET) {

// 直接定位

rs.absolute(rowBounds.getOffset());

} else {

// 只能逐条滚动到指定位置

for (int i = 0; i rowBounds.getOffset(); i++) {

rs.next();

说明,Mybatis的分页是对结果集进行的分页。

假设查询结果总[!--empirenews.page--]此页面是否是列表页或首页?未找到合适正文内容。

转载:非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“黎青松SEO博客”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值