天天看點

Hibernate頁面資料分頁顯示

package com.huike.hidp.util;

import org.hibernate.query;

import org.hibernate.session;

import org.springframework.orm.hibernate3.hibernatecallback;

import org.springframework.orm.hibernate3.support.hibernatedaosupport;

/**

 * 頁面資料分頁顯示

 * @author

 * @date 2009-5

 */

public class paginationhibernatedaosupport extends hibernatedaosupport {

 * 使用hql語句進行分頁查詢

 * @param hql 需要查詢的hql語句

 * @param offset 第一條記錄索引

 * @param pagesize 每頁需要顯示的記錄條數

 * @return 目前頁的所有記錄

public list findbypage(final string hql,final int offset,final int pagesize){

//通過一個hibernatecallback 對象來執行查詢

list list = gethibernatetemplate().executefind(new hibernatecallback()

{

//實作hibernatecallback接口必須實作的方法

public object doinhibernate(session session) throws hibernateexception{

//執行hibernate 分頁查詢

list result = session.createquery(hql)

                     .setfirstresult(offset)

                     .setmaxresults(pagesize)

                     .list();

return result;

}

);

return list;

 * @param value 如果hql 有一個參數需要傳入,value就是傳入hql語句的參數

public list findbypage(final string hql,final object value,

               final int offset,final int pagesize){

                     .setparameter(0, value)

 * @param values 如果hql有多個參數需要傳入,values 就是傳入hql的參數組 

public list findbypage(final string hql,final object[] values,

query query = session.createquery(hql);

//為hql語句傳入參數

for(int i=0; i < values.length; i++){

query.setparameter(i, values[i]);

list result =   query.setfirstresult(offset)