RowBounds代码实现排序功能版权声明
原创首先,让我们看看物理分页和逻辑分页。
物理分页:例如,直接从数据库中获取我们需要的数据。Mysql中使用limit
逻辑分页:从数据库中取出所有需要的数据,然后从这些数据中获取所需的分页数据。
优缺点
物理分页每次都访问数据库,逻辑分页只访问一次。
物理分页占用的内存较少,逻辑分页相对较大
物理分页数据每次都是最新的,逻辑分页可能会滞后
让我们再看一遍。RowBounds,我们可以通过RowBounds对象来实现逻辑分页。
public List queryPushMessageByPage(RowBounds rowBounds);
wechatPushMessageDao.queryPushMessageByPage(new RowBounds(offset,limit));
RowBounds对象有2个属性,offset和limit。
offset:页码,第一页为1,
limit:页面上有多少条数据,例如一页12条
因此,取出的数据是:offset+1线路开始,取limit行
Mybatis中使用RowBounds实现分页的总体思路:
先取出所有数据,然后移动光标offset位置,循环拍摄limit数据,然后丢弃剩余数据。
private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException { DefaultResultContext resultContext = new DefaultResultContext(); this.skipRows(rsw.getResultSet(), rowBounds); //游标跳到offset位置 //取出limit条数据 while(this.shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) { ResultMap discriminatedResultMap = this.resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, (String)null); Object rowValue = this.getRowValue(rsw, discriminatedResultMap); this.storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet()); }
}
private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException { if (rs.getType() != 1003) { if (rowBounds.getOffset() != 0) { rs.absolute(rowBounds.getOffset()); } } else { //从开始移动光标offset位置 for(int i = 0; i < rowBounds.getOffset(); ++i) { rs.next(); } }
}
以上就是RowBounds代码的全文实现了分页功能。,我希望这对你学习和使用数据库有帮助。.
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除