首页 > 编程知识 正文

oracle使用rownum分页,pagehelper原理

时间:2023-05-04 03:06:23 阅读:16584 作者:2800

物理寻呼Mybatis插件原理分析(3)寻呼插件

Mybatis提供了一个简单的逻辑分页使用类RowBounds。 物理分页是指在sql语句中指定limit和offset的值。 在DefaultSqlSession提供的一些查询接口中,可以看到行边界作为参数用于分页。 请参阅以下接口。

publicelisteselectlist (字符串语句,对象参数,原始边界原始边界) )。

RowBounds的源代码如下:

public class RowBounds { /*默认offset为0 * */publicstaticfinalintno _ row _ offset=0; /*逻辑分页* */publicstaticfinalintno _ row _ Limit=integer.max _ value,因为缺省limit是int的最大值; publicstaticfinalrowboundsdefault=newrow bounds (; 私密int offset; 隐私限制; publicrowbounds ((this.offset=no _ row _ offset; this.limit=NO_ROW_LIMIT; }publicrowbounds(intoffset,int limit ) { this.offset=offset; this.limit=limit; } public int getOffset () { return offset; } public int getLimit () { return limit; }逻辑分页的实现原理:

在DefaultResultSetHandler中,逻辑分页查询所有结果,并根据RowBounds提供的offset和limit值检索最终结果。 DefaultResultSetHandler实现如下:

privatevoidhandlerowvaluesforsimpleresultmap (resultsetwrapperrsw,ResultMap resultMap,ResultHandler? 结果处理程序,原始边界原始边界,结果映射父映射) throws sqlexception { defaultresultcontextobjectresultcontext 判断//数据是否小于limit,如果小于limit,则重复取值while (shouldprocessmorerows (result context ), row bounds (rsw.get resultset (.next ) ) resultmapdiscriminatedresultmap=resolvediscriminatedresultmap ) rsw.getresultmap 存储对象(result handler,resultContext,rowValue,parentMapping,rsw.getResultSet () ); } privatebooleanshouldprocessmorerows (结果上下文? 上下文,rowboundsrowbounds (throws sqlexception (/确定数据是否小于limit,是否小于返回true return! context.is stopped (context.get result count ) (rowBounds.getLimit ); //跳过不需要的行是rowbounds设置的limit和offsetprivatevoidskiprows (resultset RS,RowBounds rowBounds ) throwssqlexception (原始边界) if=row bounds.no _ row _ offset ((RS.absolute ) rowbounds.getoffset ) ); }跳过}else{//rowbounds中设定的offset栏的数据for (inti=0; i rowBounds.getOffset (; I () { rs.next ); } }

总结: Mybatis的逻辑分页比较简单,简单来说就是取出所有符合条件的数据,舍弃前面的offset个数据,取剩下的limit个数据

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。