hibernate分页技术

来源:互联网 发布:mac air 默认win7 编辑:程序博客网 时间:2024/06/05 18:23

1.新建项目并配置好包。

2.dao

//分页查询hql查询条件。

public List queryforpage(final String hql,final int offset,final int length);

//查询所有记录数hql查询的条件,总记录数

public int getAllRowCount(String hql);

daoImpl

public int getAllRowCount(String hql){

   return getHibernateTemplate().find(hql).size();

}

 public List queryforpage(final String hql,final int offset,final int length){

List list=getHibernateTemplate().executeFind(new HibernateCallback()){

   public Object doInHibernate(Session session) throws HibernateException,SQLException{

   Query query=session.createQuery(hql);

  query.setFirstResult(offset);

  query.setMaxResults(length);

  List list=query.list();

return list;

}

});

return list;

}

}

PageBean

public class PageBean implements Serializable{

private LIst list;//要返回的某一页的记录列表

private int allRow;  //总记录数

private int totalPage; //总页数

private int currentPage;  //当前页

private int pageSize;  //每页记录数

private boolean isFirstPage; //是否为第一页

private boolean isLastPage; //是否为最后一页

private boolean hasPreviousPage; //是否有前一页

private boolean hasNextPage; //是否有下一页

getset

public void init(){//初始化分页信息

this.isFirstPage=isFirstPage();

this.isLastPage=isLastPage();

this.hasPreviousPage=isHasPreviousPage();

this.hasNextPage=isHasNextPage();

}

public boolean isFirstPage(){//如是当前是第一页,,判断页的信息,只需get方法

return currentPage==1;

}

public boolean isLastPage(){//如果当前页是最后一页

return currentPage==totalPage;

}

public boolean isHasPreviousPage(){//只要当前页不是第一页

return currentPage!=1;

}

public boolean isHasNextPage(){

return currentPage !=totalPage;//只要当前页不是最后一页

}

//计算总页数,静态方法,供外部直接通过类名调用

public static int countTotalPage(final int pageSize,final int allRow){

int totalPage=allRow%pageSize==0?allRow/pageSize:allRow/pageSize+1;

return totalPage;

}

//计算当前页开始记录

public static int countOffset(final int pageSize,final int currentPage){

final int offset=pageSize*(currentPage-1);

return offset;

}

//计算当前页,假如为0或者没有值使用1代替

public static int countCurrentPage(int page){

final int curPage=(page==0?1:page);

return curPage;

}

}

service//分页查询currentPage当前第几页,pageSize每页大小

public PageBean queryforPage(int pageSize,int currentPage);

serviceImpl//分页查询currentpage当前第几页,pagesize每页大小,封闭了分页信息的bean

public PageBean queryforPage(int pageSize,int page){

final String hql="from com**model.u";

int allRow=dao.getAllRowCount(hql);//总记录数

int totalPage=PageBean.countTotalPage(pageSize,allRow);//总页数

final int offset=PageBean.countOffset(pageSize,page);//当前页开始记录

final int length=pageSize;//每页记录数

final int currentPage=PageBean.countCurrentPage(page);

List<modell> list=dao.queryforpage(hql,offset,length);

//把每页信息保存到bean中

PageBean pageBean=new PageBean();

pageBean.setpageSize(pageSize);

pageBean.setCurrentPage(currentPage);

pageBean.setAllRow(allRow);

pageBean.setTotalPage(totalPage);

pageBean.setList(list);

pageBean.init();

return pageBean;


}

jsp

s:iterator value="pageBean.list">
           
<s:propertyvalue="title"/>
           
<a href="getArticle.action?id=<s:property value="id"/>">modify</a>
           
<a href="deleteArticle.action?id=<s:property value="id"/>"onclick="return askDel()"/>delete</a><br/>
       
</s:iterator>
       
<s:propertyvalue="pageBean.allRow"/>条记录
       
<s:propertyvalue="pageBean.totalPage"/>
       
当前第<s:propertyvalue="pageBean.currentPage"/><br/>
        
       
<s:if test="%{pageBean.currentPage== 1}">
           
第一页上一页
       
</s:if>
       
<s:else>
           
<a href="listMyArticle.action?page=1">第一页</a>
           
<a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage-1}"/>">上一页</a>
       
</s:else>
       
<s:if test="%{pageBean.currentPage!= pageBean.totalPage}">
           
<a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage+1}"/>">下一页</a>

   <ahref="listMyArticle.action?page=<s:propertyvalue="pageBean.totalPage"/>">最后一页</a>
       
</s:if>
       
<s:else>
           
下一页 最后一页
       
</s:else>