在進行大量的數據展示時,必須要使用分頁查詢,第一次使用在SSH框架整合中使用分頁查詢,遇到了一些問題,下面以我練習的項目為例詳細介紹,如何在Spring+hibernate(+action)的環境下完成數據的分頁查詢.
在utils包下新建一個pageBean(這個實體類用於封裝當前頁面的數據集合,以及和page相關的參數):
public class PageBean<T> { private int page;//當前頁數
private int totalCount;//總記錄數
private int totalPage;//總頁數(總記錄數/每頁記錄數)
private int limit;//每頁記錄數
private List<T> list;//包含商品的集合 //set/get方法省略
}
在我的案例中,在頁面中,當點擊"全部商品"時,會跳入到(商品的action類)GoodsAction,並且傳入一個參數page,默認為1.
GoodsAction:
public class GoodsAction extends ActionSupport implements ModelDriven<Goods>,ServletRequestAware{ private GoodsService goodsService; private Goods goods=new Goods(); HttpServletRequest request; private int page; /** * 展示所有商品 * @return
*/
public String showAll(){ //List<Goods> gList=goodsService.findAll();
PageBean<Goods> pagebean=goodsService.findByPage(page); ActionContext.getContext().getValueStack().set("pageBean", pagebean); return "findAll_succ"; } public void setGoodsService(GoodsService goodsService) { this.goodsService = goodsService; } @Override public Goods getModel() { return goods; } @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } public void setPage(int page) { this.page = page; } public int getPage() { return page; } }
在action,需要返回得到pageBean的所有私有成員的信息,並且設置到值棧(ValueStack)棧頂,以供頁面回顯調用.
下面是GoodsService:
package com.wang.shop.goods.service; import java.util.List; import com.wang.shop.goods.dao.GoodsDao; import com.wang.shop.goods.entity.Goods; import com.wang.shop.util.PageBean; public class GoodsService { private GoodsDao goodsDao; public void setGoodsDao(GoodsDao goodsDao) { this.goodsDao = goodsDao; } public PageBean<Goods> findByPage(int page) { PageBean<Goods> pageBean =new PageBean<Goods>(); pageBean.setPage(page); int limit=4; pageBean.setLimit(limit); int totalCount=goodsDao.findTotalCount(); pageBean.setTotalCount(totalCount); int totalpage=(int)Math.ceil(totalCount/limit); pageBean.setTotalPage(totalpage); //每頁顯示的數據集合
int begin=(page-1)*limit; List<Goods> list=goodsDao.findByPageId(begin,limit); pageBean.setList(list); return pageBean; } }
在service中,設置pageBean的每個屬性,可以得到的直接設置,得不到的去Dao層中進行數據庫查詢.
GoodsDao:
package com.wang.shop.goods.dao; import java.util.List; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.orm.hibernate4.support.HibernateDaoSupport; import com.wang.shop.goods.entity.Goods; import com.wang.shop.util.PageHibernateCallback; public class GoodsDao extends HibernateDaoSupport{ public List<Goods> findAll() { List<Goods> list=(List<Goods>) this.getHibernateTemplate().find("from Goods"); return list; } public Goods findById(int goodsId) { Goods goods=this.getHibernateTemplate().get(Goods.class, goodsId); return goods; } //查詢goods表中總記錄數
public int findTotalCount() { String hql="select count(*) from Goods"; List<Long> list=(List<Long>) this.getHibernateTemplate().find(hql); if(list!=null&&list.size()>0){ return list.get(0).intValue(); } return 0; } //查詢當前頁面的商品集合
public List<Goods> findByPageId(int begin, int limit) { String hql="from Goods"; List<Goods> list=(List<Goods>) this.getHibernateTemplate().execute((HibernateCallback<Goods>) new PageHibernateCallback(hql, new Object[]{}, begin, limit)); if(list!=null&&list.size()>0){ return list; } return null; } }
注意這里我用到了一個PageHibernateCallback類,通常情況下,我們會寫一個HibernateCallBack的匿名內部類,然后在里邊寫相關代碼,為了代碼復用,這里重新寫了一個類來實現HibernateCallback接口,再通過泛型依賴注入,就可以得到一個工具類了.下面是代碼.
PageHibernateCallback:
package com.wang.shop.util; import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate4.HibernateCallback; public class PageHibernateCallback<T> implements HibernateCallback<List<T>>{ private String hql; private Object[] params; private int startIndex; private int pageSize; public PageHibernateCallback(String hql, Object[] params, int startIndex, int pageSize) { super(); this.hql = hql; this.params = params; this.startIndex = startIndex; this.pageSize = pageSize; } public List<T> doInHibernate(Session session) throws HibernateException { Query query = session.createQuery(hql); if(params != null){ for(int i = 0 ; i < params.length ; i ++){ query.setParameter(i, params[i]); } } query.setFirstResult(startIndex); query.setMaxResults(pageSize); return query.list(); } }
上面代碼的構造方法中的第二個參數是一個object類型的數組,用於設置hql語句中的"?",如果你沒有這個參數,可以在調用的時候,寫為 new Object[]{}.
以上就可以實現,數據的分頁查詢了.
