【Hibernate】Hibernate真分页实现

来源:互联网 发布:steelcase淘宝 编辑:程序博客网 时间:2024/04/29 07:53

【前言】

          这几乎是一个网站类项目逃不掉的技术点——分页, 因为项目用的是ssh的框架,所以总结一下Hibernate的分页功能。

【正文】

先贴上两个引用的工具类PageBean和PageHibernateCallback:

package com.tgb.dmtOS.utils;import java.sql.SQLException;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.springframework.orm.hibernate3.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,SQLException {//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();}}

package com.tgb.dmtOS.utils;import java.util.List;import com.tgb.dmtOS.adminnews.vo.AdminNews;//��ҳ��ķ�װpublic class PageBean<T> {private int page;    //��ǰҳ�� private int totalCount;  //�ܼ�¼��private int totalPage;  //�ܵ�ҳ��private int limit;      //ÿҳ��ʾ��¼��private List<T> list;//ÿҳ��ʾ��ݵļ���public int getPage() {return page;}public void setPage(int page) {this.page = page;}public int getTotalCount() {return totalCount;}public void setTotalCount(int totalCount) {this.totalCount = totalCount;}public int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getLimit() {return limit;}public void setLimit(int limit) {this.limit = limit;}public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}}


DAO层

public List<Article> findByPage(int begin,int limit){String hql="from Article order by Time desc";List<Article> list=this.getHibernateTemplate().execute(new PageHibernateCallback<Article>(hql,null,begin,limit));if (list != null && !list.isEmpty()) {return list;}return null;}

ACTION

private Integer page;public void setPage(Integer page) {this.page = page;}
public String findAll(){List<Category> cList =categoryService.findAll();ActionContext.getContext().getValueStack().set("cList",cList);PageBean<Article> pageBean = adminArticleService.findByPage(page); ActionContext.getContext().getValueStack().set("pageBean", pageBean);return "findAll";}


页面:

<tr align="center"><td colspan="4">第<s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/>页<s:if test="pageBean.page != 1"><a href="${pageContext.request.contextPath }/adminArticle_findAll.action?page=1">首页</a>|<a href="${pageContext.request.contextPath }/adminArticle_findAll.action?page=<s:property value="pageBean.page-1"/>">上一页</a>|</s:if><s:if test="pageBean.page != pageBean.totalPage"><a href="${pageContext.request.contextPath }/adminArticle_findAll.action?page=<s:property value="pageBean.page+1"/>">下一页</a>|<a href="${pageContext.request.contextPath }/adminArticle_findAll.action?page=<s:property value="pageBean.totalPage"/>">尾页</a></s:if></td></tr>



原创粉丝点击