Mybatis逻辑分页原理解析RowBounds
来源:互联网 发布:vb和vc的区别 编辑:程序博客网 时间:2024/06/05 01:16
物理分页Mybatis插件原理分析(三)分页插件
Mybatis提供了一个简单的逻辑分页使用类RowBounds(物理分页当然就是我们在sql语句中指定limit和offset值),在DefaultSqlSession提供的某些查询接口中我们可以看到RowBounds是作为参数用来进行分页的,如下接口:
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds)
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; }}逻辑分页的实现原理:
在DefaultResultSetHandler中,逻辑分页会将所有的结果都查询到,然后根据RowBounds中提供的offset和limit值来获取最后的结果,DefaultResultSetHandler实现如下:
private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException { DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();//跳过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(); } } }总结:Mybatis的逻辑分页比较简单,简单来说就是取出所有满足条件的数据,然后舍弃掉前面offset条数据,然后再取剩下的数据的limit条
3 0
- Mybatis逻辑分页原理解析RowBounds
- mybatis 分页 RowBounds
- mybatis 分页参数 RowBounds
- 博为峰JavaEE技术文章 ——MyBatis RowBounds分页
- MyBatis中的RowBounds
- MyBatis精通之路之分页功能的实现(数组分页、sql分页、拦截器,RowBounds分页)
- Mybatis 逻辑与物理分页
- mybatis逻辑分页,含分页导航
- mybatis RowBounds 限制查询条数
- mybatis分页原理实现
- PHP 分页原理解析
- MyBatis原理--配置解析
- mybatis原理解析
- mybatis原理解析
- Mybatis使用及原理解析
- Mybatis使用及原理解析
- Mybatis实现原理深入解析
- Mybatis实现原理深入解析
- 检测到在集成的托管管道模式下不适用的 ASP.NET 设置。
- 矩阵的行列式、秩的意义
- C++——函数模板和函数指针
- 企业发展中的商业智能具有更多能量
- easyui combobox 选中某个默认值
- Mybatis逻辑分页原理解析RowBounds
- 同步和异步,阻塞和非阻塞
- php中的位运算???
- JS-定时器
- 编译Vim从源码中
- Sublime中集成Nodejs环境
- Linux常用命令详解(一)
- [Usaco2008 Feb]Eating Together麻烦的聚餐(DP)
- 各种排序算法总结和比较