2025年RowBounds分页原理、RowBounds的坑

RowBounds分页原理、RowBounds的坑目录 背景说明 一 RowBounds 分页原理 二 RowBounds 的使用 三 RowBounds 的坑 背景说明 项目中经常会使用分页查询 有次使用了 RowBounds 进行分页 因为很多场景或网上也看到很多这样的写法 所以我也在项目中使用了该类进行分页 但是有次线上却抛了异常 由此引发了对 RowBounds 原理的探究 RowBounds 是将所有符合条件的数据全都查询到内存中

目录

背景说明

一:RowBounds分页原理

二:RowBounds的使用

三:RowBounds的坑

背景说明

项目中经常会使用分页查询,有次使用了RowBounds进行分页,因为很多场景或网上也看到很多这样的写法,所以我也在项目中使用了该类进行分页。但是有次线上却抛了异常,由此引发了对RowBounds原理的探究。

RowBounds是将所有符合条件的数据全都查询到内存中,然后在内存中对数据进行分页,若数据量大,千万别使用RowBounds

如我们写的sql语句:select * from user where id>0 limit 0,10
RowBounds会将id>0的所有数据全都加载到内存中,然后截取前10行,若id>0有100万条,则100万条数据都会加载到内存中,从而造成内存OOM。

一:RowBounds分页原理

Mybatis可以通过传递RowBounds对象,来进行数据库数据的分页操作,然而遗憾的是,该分页操作是对ResultSet结果集进行分页,也就是人们常说的逻辑分页,而非物理分页(物理分页当然就是我们在sql语句中指定limit和offset值)。

RowBounds源码如下:

public class RowBounds { 

/* 默认offset是0**/
public static final int NO_ROW_OFFSET = 0;
/* 默认Limit是int的最大值,因此它使用的是逻辑分页**/
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()方法源码。

Mybatis中使用RowBounds实现分页的大体思路:
先取出所有数据,然后游标移动到offset位置,循环取limit条数据,然后把剩下的数据舍弃。

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

DefaultResultContext resultContext = new DefaultResultContext();
//跳过RowBounds设置的offset值
skipRows(rsw.getResultSet(), rowBounds);
//判断数据是否小于limit,如果小于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 boolean shouldProcessMoreRows(ResultContext context, RowBounds rowBounds) throws SQLException {

//判断数据是否小于limit,小于返回true
return !context.isStopped() && context.getResultCount() < rowBounds.getLimit();
}
//跳过不需要的行,应该就是rowbounds设置的limit和offset
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 {

//跳过RowBounds中设置的offset条数据,只能逐条滚动到指定位置
for (int i = 0; i < rowBounds.getOffset(); i++) {

rs.next();
}
}
}

二:RowBounds的使用

原理:拦截器。

使用方法:

RowBounds:在dao.java中的方法中传入RowBounds对象。

2.1:引入依赖


org.mybatis
mybatis
3.5.6

2.2:service层

import org.apache.ibatis.session.RowBounds;
public class UserServiceImpl implements UserService {

@Autowired
private UserDao userDao;
@Override
public Map queryUserList(String currentPage, String pageSize) {

//查询数据总条数
int total = userDao.queryCountUser();
//返回结果集
Map resultMap = new HashMap();
resultMap.put("total", total);
//总页数
int totalpage = (total + Integer.parseInt(pageSize) - 1) / Integer.parseInt(pageSize);
resultMap.put("totalpage", totalpage);
//数据的起始行
int offset = (Integer.parseInt(currentPage)-1)*Integer.parseInt(pageSize);
RowBounds rowbounds = new RowBounds(offset, Integer.parseInt(pageSize));
//用户数据集合
List> userList = userDao.queryUserList(rowbounds);
resultMap.put("userList", userList);
return resultMap;
}
}

2.3:dao层

import org.apache.ibatis.session.RowBounds;
public interface UserDao {

public int queryCountUser(); //查询用户总数
public List> queryUserList(RowBounds rowbounds); //查询用户列表
}

2.4:mapper.xml
mappep.xml里面正常配置,不用对rowBounds任何操作。mybatis的拦截器自动操作rowBounds进行分页。


PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">





三:RowBounds的坑

2021-11-19 15:15:14.933 ERROR [task-10] [org.springframework.transaction.interceptor.TransactionInterceptor] Application exception overridden by rollback exception
org.springframework.dao.TransientDataAccessResourceException:
— Error querying database. Cause: java.sql.SQLException: Java heap space
— The error may exist in com/test/mapper/InfoRecordMapper.java (best guess)
– The error may involve com.test.mapper.InfoRecordMapper.selectByExampleAndRowBounds-Inline
— The error occurred while setting parameters

RowBounds是将所有符合条件的数据全都查询到内存中,然后在内存中对数据进行分页
如我们查询user表中id>0的数据,然后分页查询sql如下:
select * from user where id >0 limit 3,10

但使用RowBounds后,会将id>0的所有数据都加载到内存中,然后跳过offset=3条数据,截取10条数据出来,若id>0的数据有100万,则100w数据都会被加载到内存中,从而造成内存OOM。

所以当数据量非常大时,一定要慎用RowBounds类。切记!切记!

编程小号
上一篇 2025-04-03 09:40
下一篇 2025-09-15 21:51

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/116291.html