hibernate分页实现机制
hibernate分页实现机制
话说上次面试,面试官问我看过hibernate分页源码,我惭愧的说,没看过。。。这几天真好闲就查了一下hibernate的分页机制。也算差不多明白了吧。。呵呵。。
public String getLimitString(String sql) { StringBuffer pagingSelect = new StringBuffer(100); pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( "); pagingSelect.append(sql); pagingSelect.append(" ) row_ where rownum <= ?) where rownum_ > ?"); return pagingSelect.toString(); }
hibernate主要的实现函数。它是最后的sql语句。主要使用了3层嵌套。
为什么使用3层嵌套,就涉及到oracle的rownum的实现:
1 Oracle executes your query.
2Oraclefetchesthefirstrowandcallsitrownumber1.
3Havewegottenpastrownumbermeetsthecriteria?Ifno,thenOraclediscardstherow,Ifyes,thenOraclereturntherow.
4Oraclefetchesthenextrowandadvancestherownumber(to2,andthento3,andthento4,andsoforth).
5 Go to step 3.具体的讲解:
http://7880.com/info/Article-4545c3c0.html