Hibernate implements paging query [turn]

xiaoxiao2021-03-06  72

Hibernate can implement paging query,

For example, from the 20,000th item, take 100 record code: query q = session.createQuery ("from cat as c"); Q.SetFirstResult (20000); Q.SetMaxResults (100); list l = q.list So how do Hibernate implements paging? In fact, Hibernate's query is defined in Net.sf.hibernate.Loader.Loader, read the code carefully, you can completely figure out the problem. Hibernate2.0.3 Loader Source Code Chapter 480: Code: IF (UseElimit) SQL = Dialect.getlimitstring (SQL); PreparedStatement St = Session.getBatcher (). PrepareQueryStatement (SQL, Scrollable); if the corresponding database defines the definition Query the recorded SQL statement, then use the SQL statement of a specific database directly. Then look net.sf.hibernate.dialect.MySQLDialect: Code: public boolean supportsLimit () {return true;} public String getLimitString (String sql) {StringBuffer pagingSelect = new StringBuffer (100); pagingSelect.append (sql); pagingSelect .append ("limit?,?"); return Pagingselect.toString ();} This is a dedicated page statement of MySQL, then looks back to Net.sf.Hibernate.DiaLect.OrCle9DiaLect: Public Boolean Supportslimit () {Return True; } 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 ();} ORACLE uses the nested 3-layer query statement combines ROWNUM to implement paging, which is the fastest way in Oracle, if only ROWNUM of a layer or two query statements cannot support Order By. In addition, Interbase, PostgreSQL, HSQL also supports paging SQL statements, in the corresponding DIALECT, everyone's own reference.

转载请注明原文地址:https://www.9cbs.com/read-121151.html

New Post(0)