StringBuffer pagingSelect = new StringBuffer( sql.length()+100 );
if (hasOffset) {
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
}
else {
pagingSelect.append("select * from ( ");
}
pagingSelect.append(sql);
if (hasOffset) {
pagingSelect.append(" ) row_ where rownum <= ?) where rownum_ > ?");
}
else {
pagingSelect.append(" ) where rownum <= ?");
}
if ( isForUpdate ) {
pagingSelect.append( " for update" );
}
return pagingSelect.toString();
}
Oracle采用嵌套3层的查询语句l合rownum来实现分,q在Oracle上是最好的方式Q因为如果只是一层或者两层的查询语句的rownum不能支持order by? 此外InterbaseQPostgreSQLQHSQL{也在语法别上支持分页Q具体实现可以查看相应的Dialect实现。如果数据库不支持分늚SQL语句Q那么如果数据库支持可滚动游标,那么Hibernate׃采用ResultSet的absoluteҎ直接Ud查询LQ否则用@环语句,通过rs.next一步步Ud到要查询的数据处Q?
final int firstRow = getFirstRow( selection );
if ( firstRow != 0 )
{
if ( getFactory().getSettings().isScrollableResultSetsEnabled() )
{
// we can go straight to the first required row
rs.absolute( firstRow );
}
else
{
// we need to step through the rows one row at a time (slow)
for ( int m = 0; m < firstRow; m++ ) rs.next();
}
}