Spring+Hibernate 分頁處理

 
Spring+Hibernate分頁處理要使用到一個接口org.springframework.orm.hibernate3.HibernateCallback
該接口中有一個函數
Object doInHibernate (Session session) 我從文檔上理解所得的意思爲HibernateTemplate.execute執行中調用的session,現在可以根據這個session進行操作,該資料可以在Spring官方網站上查詢www.springframework.org
 
另外還有一個Query接口,它有兩個函數,setFirstResult(int firstResult)setMaxResults(int maxResults),這兩個函數看名取意,就不多說了,下面來個例子
 
 
接口
package com.david.dao;
 
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
 
public interface IPageBeanSH extends HibernateCallback {
    public Type[] getArrTypes();
 
    public void setArrTypes(Type[] arrTypes);
 
    public Object[] getArrValues();
 
    public void setArrValues(Object[] arrValues);
 
    public String getHql();
 
    public void setHql(String hql);
 
    public int getNum();
 
    public void setNum(int num);
 
    public int getStart();
 
    public void setStart(int start);
}
 
實現類
package com.david.dao;
 
import java.sql.SQLException;
import java.util.List;
 
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
 
/**
 * 負責分頁的bean
 *
 * @author Administrator
 *
 */
public class PageBeanSH implements IPageBeanSH {
    //起始位置
    private int start=0;
    //數量
    private int num;
    //查詢語句
    private String hql; 
   
    //對象數組,查詢
    private Object[] arrValues;
    //對象
    private Type[] arrTypes;
   
    public PageBeanSH() {
       super();
       // TODO Auto-generated constructor stub
    }
   
    public PageBeanSH(String hql,Object[] arrValues) {
       this.hql = hql;
       this.arrValues=arrValues;
       // TODO Auto-generated constructor stub
    }
 
    /**
     * 構造函數
     * @param start
     * @param num
     * @param hql
     */
    public PageBeanSH(int start, int num, String hql,Object[] arrValues,Type[] arrTypes) {
       super();
       this.start = start;
       this.num = num;
       this.hql = hql;
       this.arrValues=arrValues;
       this.arrTypes=arrTypes;
    }
 
 
    /**
     * 得出查詢結果
     */
    public Object doInHibernate(Session session) throws HibernateException,
           SQLException {
       // TODO Auto-generated method stub
       Query query=session.createQuery(hql);
       query.setFirstResult(start*num);
       query.setMaxResults(num);
       //循環套入參數
       if(arrValues.length==0){
           ;
       }else{
           for(int i=0;i<arrValues.length;i++){
              query.setParameter(i, arrValues[i]);
           }
       }
       return query.list();
    }
 
    public Type[] getArrTypes() {
       return arrTypes;
    }
 
    public void setArrTypes(Type[] arrTypes) {
       this.arrTypes = arrTypes;
    }
 
    public Object[] getArrValues() {
       return arrValues;
    }
 
    public void setArrValues(Object[] arrValues) {
       this.arrValues = arrValues;
    }
 
    public String getHql() {
       return hql;
    }
 
    public void setHql(String hql) {
       this.hql = hql;
    }
 
    public int getNum() {
       return num;
    }
 
    public void setNum(int num) {
       this.num = num;
    }
 
    public int getStart() {
       return start;
    }
 
    public void setStart(int start) {
       this.start = start;
    }
 
}
這樣就可以通過設置hql語句和參數進行分頁操作
例如
 
package com.david.dao;
 
import java.sql.SQLException;
import java.util.List;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
import com.david.bo.Category;
import com.david.bo.Userinfo;
 
public class CategoryDao extends HibernateDaoSupport implements ICategoryDao {
 
    private IPageBeanSH pageBeanSH;
   
    public boolean addCategory(Category category) {
       // TODO Auto-generated method stub
       try{
           this.getHibernateTemplate().save(category);
           return true;
       }catch(DataAccessException e){
           e.printStackTrace();
           return false;
       }
    }
 
    public boolean deleteCategory(Category category) {
       // TODO Auto-generated method stub
       return false;
    }
 
    public List<Category> getCategories(int start, Userinfo userinfo) {
       // TODO Auto-generated method stub
      
       String hql="from Category as category where category.userinfo=?";// as c where c.userinfo=?";
       pageBeanSH.setHql(hql);
       pageBeanSH.setStart(start);
       Userinfo[] arrValues={userinfo};
       pageBeanSH.setArrValues(arrValues);
       try {
           return (List<Category>) pageBeanSH.doInHibernate(this.getSession());
       } catch (DataAccessResourceFailureException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
           return null;
       } catch (HibernateException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
           return null;
       } catch (IllegalStateException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
           return null;
       } catch (SQLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
           return null;
       }
    }
   
    public boolean updateCategory(Category category) {
       // TODO Auto-generated method stub
       return false;
    }
 
    public void setPageBeanSH(IPageBeanSH pageBeanSH) {
       // TODO Auto-generated method stub
       this.pageBeanSH=pageBeanSH;
    }
 
    public int getCategoriesNum(Userinfo userinfo) {
       // TODO Auto-generated method stub
       return this.getHibernateTemplate().find("from Category as category where category.userinfo=?", userinfo).size();
    }
 
}
 
在函數public List<Category> getCategories(int start, Userinfo userinfo)通過設置起始位置,hsl語句,參數列表數組就可以進行分頁的查詢操作了
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章