hibernate3.4+struts1.3分页封装,有兴趣者可以看一下

Hibernate3.4+struts1.3分页的封装
环境:hibernate3.4 +struts1.3 +jsp
Page.java
package com.nongxue.util;

public class Page {
private int totalPage;//总页数
private int curPage;//当前页数
public static int maxResult = 2;//每页的最大记录条数
public Page(int toPage,int curPage){
this.setTotalPage(toPage);
this.setCurPage(curPage);
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
if(totalPage == 0){
this.totalPage = 1;
return;
}
this.totalPage = totalPage%maxResult == 0?totalPage/maxResult : totalPage/maxResult+1;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
}
QueryResult.java
package com.nongxue.util;

import java.util.List;

public class QueryResult<T> {
private List<T> records;
private Page page;
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}

查询总记录数的代码
public int getArticleAccount(StringBuffer HQL)
{
Session session = new ComonUtil().getSession();
org.hibernate.Transaction tr  = session.beginTransaction();
int temp=0;
        try {
String Hquery = "from Article"+HQL;
Query query = session.createQuery(Hquery);
    temp =query.list().size();
    System.out.println("ddddddddddd"+temp);
    tr.commit();
} catch (HibernateException e) {
tr.rollback();
e.printStackTrace();
} finally{
session.close();
}
        return temp;
}
分页查询代码
public QueryResult<Article> getAllArticle(int page,StringBuffer HQL) {
List<Article> list = new ArrayList<Article>();
Session session =new  ComonUtil().getSession();
org.hibernate.Transaction tr = session.beginTransaction();
QueryResult<Article> queryResult = null;
try {
Page pae = new Page(this.getArticleAccount(HQL),page);
System.out.println("==hql=="+HQL);
String hquery="from Article"+HQL+"order by articleId desc";
System.out.println("=======HQL2======"+hquery);
Query query = session.createQuery(hquery);
query.setFirstResult(Page.maxResult*(page-1));
query.setMaxResults(Page.maxResult);
list = query.list();
queryResult = new QueryResult<Article>();
queryResult.setPage(pae);
queryResult.setRecords(list);
} catch (HibernateException e) {
tr.rollback();
e.printStackTrace();
}
return queryResult;
}
其中ComonUtil().getSession()调用的代码是
public class ComonUtil {
     //拿到session
public Session getSession()
{
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
return session;
}
……

Action中调用
public ActionForward getAllArticleByPage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String forwardName = null;
int page = 0;
try{
page = Integer.parseInt(request.getParameter("page"));
}catch(Exception e){
page = 1;
}
QueryResult<Article> articleList = null;

int stutus =0;
StringBuffer HQL = new StringBuffer();
//判断是否有状态值
try {
stutus = Integer.parseInt(request.getParameter("stutus"));
try {
Student student = (Student) request.getSession().getAttribute("student");
String sname = student.getStudentTrueName();
System.out.println("ddddddddddddd "+sname);
articleList = CommServce.getInstence().getAllArticle(page,HQL.append(" where articleUser='"+sname+"' "));
forwardName="allarticle";
} catch (Exception e) {
articleList = CommServce.getInstence().getAllArticle(page,HQL.append(" where stutus="+stutus+" "));
forwardName="articleList";
}
} catch (Exception e) {
Student student = (Student) request.getSession().getAttribute("student");
String sname = student.getStudentTrueName();
System.out.println("ddddddddddddd "+sname);
articleList = CommServce.getInstence().getAllArticle(page,HQL.append(" where articleUser='"+sname+"' "));
forwardName="allarticle";
}
request.setAttribute("article", articleList);
request.setAttribute("curPage", page);
return mapping.findForward(forwardName);
}
页面jsp上的显示
<center>
     <a href="ArticleAction.do?comm=getAllArticleByPage&&page=1">首页</a>
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <c:if test="${curPage==1}">
               上一页
     </c:if>
     <c:if test="${curPage>1}">
       <a href="ArticleAction.do?comm=getAllArticleByPage&&page=${article.page.curPage-1}">上一页</a>
     </c:if>
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <c:if test="${curPage==article.page.totalPage}">
              下一页
     </c:if>
     <c:if test="${curPage<article.page.totalPage}">
       <a href="ArticleAction.do?comm=getAllArticleByPage&&page=${article.page.curPage+1}">下一页</a>
     </c:if>
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <a href="ArticleAction.do?comm=getAllArticleByPage&&page=${article.page.totalPage}">末页</a>
     &nbsp;&nbsp;共<font color="red">${article.page.totalPage}</font>页
  </center>
这是一个非常通用 的分页方法,各位可以进行拓展,如果有不了解的地方也可以联系我:QQ443628678

你可能感兴趣的:(jsp,Hibernate)