关于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的使用

原理:拦截器。

使用方法:

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进行分页。

                                     

三: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类。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持0133技术站。

以上就是关于RowBounds分页原理、RowBounds的坑记录的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java