最近做项目,需要分页查询,自己在网上查了查,尝试了一下,下面给大家总结归纳一下,希望对大家有用
-1)需要一个PageBean<T>
public class PageBean<T> {
private Integer page;//当前页数
private Integer totalCount;//总记录数
private Integer totalPage;//总页数
private Integer limit;//每页显示的记录数
private List<T> list;//每页显示的数据集合
--get和set方法--
}
-2)写一个实现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;
}
@SuppressWarnings("unchecked")
@Override
public List<T> doInHibernate(Session session) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
//1.执行hql
Query query = session.createQuery(hql);
//2.实际参数
if(params != null){
for(int i=0;i<params.length;i++){
query.setParameter(i, params[i]);
}
}
//3.分页
query.setFirstResult(startIndex);
query.setMaxResults(pageSize);
return query.list();
}
-3)Dao层写查询
//后台查询所有商品的方法
public List<goods> findByPage(Integer begin,Integer limit){
String hql = "from goods";
List<goods> list = this.getHibernateTemplate().execute(new PageHibernateCallback<goods>(hql, null, begin, limit));
if(list != null && list.size() > 0){
return list;
}
return null;
}其中可以传递参数进去,比如new Object[]{id},根据类别查询,HQL语句为from goods where cateId = ?
-4)Service中去初始化PageBean
//后台查询所有商品带分页
public PageBean<goods> findByPage(Integer page){
PageBean<goods> pageBean = new PageBean<goods>();
//设置当前页数
pageBean.setPage(page);
//设置每页显示的记录数
int limit = 6;
pageBean.setLimit(limit);
//设置总记录数
int totalCount = 0;
totalCount = goodsDao.findCount();
pageBean.setTotalCount(totalCount);
//设置总页数
int totalPage = 0;
if(totalCount % limit == 0){
totalPage = totalCount / limit;
} else {
totalPage = totalCount / limit + 1;
}
pageBean.setTotalPage(totalPage);
//每页显示的数据集合
int begin = (page - 1) * limit;
List<goods> list = goodsDao.findByPage(begin, limit);
pageBean.setList(list);
return pageBean;
}
-5)去Action中写
//查询相关page上的goods
public String findAll(){
PageBean<goods> pageBean = goodsService.findByPage(page);
ActionContext.getContext().put("pageBean", pageBean);
return "findAll";
}
-6)界面中下面实现上一页/下一页的功能,使用struts2标签
<div id="templatemo_left_content">
<h1>商品信息</h1>
<p align="left">
<TABLE class=main border=0 cellSpacing=2 cellPadding=2>
<s:iterator var="g" value="#pageBean.list" status="st">
<s:if test="#st.index%3==0">
<tr></tr>
</s:if>
<td>
<TABLE οnmοuseοver="this.style.backgroundColor='#FF6600'" οnmοuseοut="this.style.backgroundColor=''" border=0 cellSpacing=1 cellPadding=2 width=98 bgColor=#e1e1e1 align=center height=100>
<TR>
<TD bgColor=#ffffff height=100 width=120 align=left>
<p align="center">
<A href="${ pageContext.request.contextPath }/goods_findGoodsById.action?id=<s:property value="#g.id"/>"><IMG border=0 align=absMiddle src="<%=path %>/<s:property value="#g.fujian"/>" width="135" height="150" style="display: inline-block;"></A>
<A href="${ pageContext.request.contextPath }/goods_findGoodsById.action?id=<s:property value="#g.id"/>"><FONT color="#ff0000"><s:property value="#g.mingcheng"/></FONT></A><BR> 价格:<s:property value="#g.shichangjia"/>
</p>
</TD>
</TR>
</TABLE>
</td>
</s:iterator>
</TABLE>
</p>
<div class="pagination">
<span>第 <s:property value="#pageBean.page"/>/<s:property value="#pageBean.totalPage"/>页</span>
<br/><br/>
<s:if test="#pageBean.page == 1">
<a href="javascript:0" class="firstPageDisabled"> </a>
<a href="javascript:0" class="previousPageDisabled"> </a>
</s:if>
<s:if test="#pageBean.page != 1">
<a href="${ pageContext.request.contextPath }/goods_findAll.action?page=1" class="firstPage"> </a>
<a href="${ pageContext.request.contextPath }/goods_findAll.action?page=<s:property value="#pageBean.page-1"/>" class="previousPage"> /a>
</s:if>
<s:iterator var="i" begin="1" end="#pageBean.totalPage">
<s:if test="#pageBean.page != #i">
<a href="${pageContext.request.contextPath }/goods_findAll.action?page=<s:property value="#i"/>"><s:property value="#i"/></a>
</s:if>
<s:else>
<span class="currentPage"><s:property value="#i"/></span>
</s:else>
</s:iterator>
<s:if test="#pageBean.page != #pageBean.totalPage">
<a class="nextPage" href="${ pageContext.request.contextPath }/goods_findAll.action?page=<s:property value="#pageBean.page+1"/>"> </a>
<a class="lastPage" href="${ pageContext.request.contextPath }/goods_findAll.action?page=<s:property value="#pageBean.totalPage"/>"> </a>
</s:if>
<s:if test="#pageBean.page == #pageBean.totalPage">
<a href="javascript:0" class="nextPageDisabled"> </a>
<a href="javascript:0" class="lastPageDisabled"> </a>
</s:if>
</div>
</div>
-OK,大功告成了