剑三宠物明教:用Hibernate+Struts实现分页
来源:百度文库 编辑:偶看新闻 时间:2024/05/11 17:22:55
上个星期偶然的一次机会,帮别人忙做一个Hibernate得东西,正好自己也有兴趣学习学习(^_^),对Hibernate基础掌握之后,于是乎自然而然想到了Paging问题。当时Google以下,大家都与Hibernate Paging 原理都转贴了不少,但是真正结合起来的例子然而很少(几乎没有)。那天在论坛看到一个兄弟求一个Hibernate+Struts实现分页得例子。于是昨天下午4点-6点开始着手(下班回家有天下足球阿!),后来晚上睡觉都在向这个问题(失眠才想的,不是因为想而失眠 ~郁闷)。今天上午来了看了几十封垃圾邮件后,终于作了出来(由不足的地方情大家指出)。在此鸣谢“Norther ”兄弟对我HQL的支持(才开始学哈,多谢好心人啊)。废话少说,开始写:前言,必读1:由于长促写的,难免有不周到或者还有格式上面凌乱的问题。
2:对于Page function进行了一定的封装。用户只需要在PageBean 里面设置以下pageRecorders (每页显示条数据),然后在自己的Action里面组装自己需要的query就可以了。
3:对于Hibernate没有进行深入研究(时间有限),所以采取了每次翻页都采用一次查询。第一次显示的时候采用了两次查询(第一次是查询Hibernate.xml里面设定的一次查询长度的所有纪录,由此推算出总数据数,总页数等等,然后赋予第一页的起始数和第一页的终止数,由query.list(); 得出第一页的数据,这里可能会有更好的改进,希望大家指点。后面得翻页主要是得到第N页的起始数和第N页的终止数)
4: 在此鸣谢各位兄弟的支持,
正文:
关于Hibernate得基本配置什么的就不多说了。现在就只是写上Paging相关的代码:
1:
PageBean.java
/*
* Created on 2006-3-21
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.ibm.hibernate.beans;import java.util.Iterator;
import java.util.List;import javax.servlet.http.HttpServletRequest;import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;/**
* @author Johnny //PS: My English Name
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class PageBean {
// public static final String HQL_DEMO_BYNAME =
// "from com.ibm.hibernate.beans.Demo as e where e.name=:tempName";
// public static final String HQL_DEMO_BYALL =
// "from com.ibm.hibernate.beans.Demo as e "; int currentPage = 1; //当前页
public int totalPages = 0; //总页数
int pageRecorders = 5; //每页5条数据
int totalRows = 0; //总数据数
int pageStartRow = 0; //每页的起始数
int pageEndRow = 0; //每页显示数据的终止数
boolean hasNextPage = false; //是否有下一页
boolean hasPreviousPage = false; //是否有前一页
List list;
private Demo demo;
Iterator it;
static PageBean pb; public PageBean() {
} public PageBean(List list) {
this.list = list;
totalRows = list.size();
it = list.iterator();
hasPreviousPage = false;
currentPage = 1;
if ((totalRows % pageRecorders) == 0) {
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders + 1;
} if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
} if (totalRows < pageRecorders) {
this.pageStartRow = 0;
this.pageEndRow = totalRows;
} else {
this.pageStartRow = 0;
this.pageEndRow = pageRecorders;
} } /**
* @return Returns the currentPage.
*/
public String getCurrentPage() {
return this.toString(currentPage);
}
/**
* @param currentPage The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageRecorders.
*/
public int getPageRecorders() {
return pageRecorders;
}
/**
* @param pageRecorders The pageRecorders to set.
*/
public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}
/**
* @return Returns the pageEndRow.
*/
public int getPageEndRow() {
return pageEndRow;
}
/**
* @return Returns the pageStartRow.
*/
public int getPageStartRow() {
return pageStartRow;
}
/**
* @return Returns the totalPages.
*/
public String getTotalPages() { return this.toString(totalPages);
}
/**
* @return Returns the totalRows.
*/
public String getTotalRows() {
return this.toString(totalRows);
}
/**
* @return Returns the hasNextPage.
*/
public boolean isHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return Returns the hasPreviousPage.
*/
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
/**
* @param hasPreviousPage The hasPreviousPage to set.
*/
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public void getNextPage() { currentPage = currentPage + 1;
System.out.println("PageBean.getNextPage()正在执行;");
System.out.println("参数currentPage=" + currentPage); if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
} if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
System.out.println("参数hasNextPage=" + hasNextPage);
if (currentPage * pageRecorders < totalRows) { //判断是否为最后一页
pageEndRow = currentPage * pageRecorders;
pageStartRow = pageEndRow - pageRecorders;
} else {
pageEndRow = totalRows;
pageStartRow = pageRecorders * (totalPages - 1);
}
System.out.println("参数pageStartRow=" + pageStartRow);
System.out.println("参数pageEndRow=" + pageEndRow);
// Demo[] Demos = getDemos();
this.description(); } public void getPreviouspage() { currentPage = currentPage - 1; if (currentPage == 0) {
currentPage = 1;
} if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currentPage * pageRecorders < totalRows) { //判断是否为最后一页
pageEndRow = currentPage * pageRecorders;
pageStartRow = pageEndRow - pageRecorders;
} else {
pageEndRow = totalRows;
pageStartRow = pageRecorders * (totalPages - 1);
}
System.out.println("参数pageStartRow=" + pageStartRow);
System.out.println("参数pageEndRow=" + pageEndRow);
this.description(); } public String toString(int temp) {
String str = Integer.toString(temp);
return str;
} public void description() { String description =
"共有数据数:"
+ this.getTotalRows()
+ "共有页数: "
+ this.getTotalPages()
+ "当前页数为:"
+ this.getCurrentPage()
+ " 是否有前一页: "
+ this.isHasPreviousPage()
+ " 是否有下一页:"
+ this.isHasNextPage()
+ " 开始行数:"
+ this.getPageStartRow()
+ " 终止行数:"
+ this.getPageEndRow(); System.out.println(description); }
/**
* @return
*/
public Demo getDemo() {
return demo;
} /**
* @param demo
*/
public void setDemo(Demo demo) {
this.demo = demo;
} /**
* @return
*/
public List getList() {
return list;
} /**
* @param list
*/
public void setList(List list) {
this.list = list;
} /**
*
*/
public PageBean pagingFirsttime(
SessionFactory factory,
Session session,
Transaction transaction,
String name,
Query query,
List list,
PageBean pb,
HttpServletRequest request)
throws HibernateException { list = query.list(); pb = new PageBean(list);
query.setFirstResult(pb.getPageStartRow());
query.setMaxResults(pb.getPageRecorders());
list = query.list(); pb.setList(list);
pb.description();
transaction.commit();
session.close();
return pb; }
public PageBean paging(
SessionFactory factory,
Session session,
Transaction transaction,
String name,
Query query,
List list,
PageBean pb,
HttpServletRequest request)
throws HibernateException {
query.setFirstResult(pb.getPageStartRow());
query.setMaxResults(pb.getPageRecorders());
list = query.list(); pb.setList(list);
pb.description();
transaction.commit();
session.close();
return pb;
}}
PagingAction.java
/
*
* Created on 2006-3-21
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.ibm.hibernate.action;import java.util.ArrayList;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.ibm.hibernate.beans.Demo;
import com.ibm.hibernate.beans.PageBean;
import com.ibm.hibernate.form.QueryForm;
import com.strutsrecipes.hibernate.plugin.HibernatePlugIn;/**
* @author Johnny
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class PagingAction extends Action {
public static final String HQL_DEMO_BYNAME =
"from com.ibm.hibernate.beans.Demo as e where e.name=:tempName";
public static final String HQL_DEMO_BYALL =
"from com.ibm.hibernate.beans.Demo as e ";
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws HibernateException { SessionFactory factory = null;
Session session = null;
Transaction transaction = null;
Query query = null;
List list = null;
PageBean pb = (PageBean) request.getSession().getAttribute("page");
String action = request.getParameter("action"); QueryForm queryForm = (QueryForm) form;
String name = queryForm.getName(); try {
factory =
(SessionFactory) servlet.getServletContext().getAttribute(
HibernatePlugIn.SESSION_FACTORY_KEY);
session = factory.openSession();
transaction = session.beginTransaction();
//组装Query
if (name == null || name.equals("")) { query = session.createQuery(HQL_DEMO_BYALL); } else { query = session.createQuery(HQL_DEMO_BYNAME); query.setString("tempName", name); } if (action == null || action.equals("null")) { //第一次读取数据
pb = new PageBean();
pb =
pb.pagingFirsttime(
factory,
session,
transaction,
name,
query,
list,
pb,
request); System.out.println("第一步,数据已经成功传递到Action,action=" + action); } else {
if (action == "nextPage" || action.equals("nextPage")) {//nextPage
System.out.println("参数action=" + action);
System.out.println("函数pb.getNextPage()准备执行"); pb.getNextPage();
pb =
pb.paging(
factory,
session,
transaction,
name,
query,
list,
pb,
request);
}
if (action == "previousPage"
|| action.equals("previousPage")) {//previousPage
System.out.println("参数action=" + action);
System.out.println("函数pb.getNextPage()准备执行"); pb.getPreviouspage();
pb =
pb.paging(
factory,
session,
transaction,
name,
query,
list,
pb,
request); }
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库连接出现异常");
}
request.setAttribute("page", pb);
request.getSession().setAttribute("page", pb);
return (mapping.findForward("QuerySuccess"));
}}
Jsp 相关部分
Total Records: Total Pages: CurrentPage: |
---|
,以及后面的图片
msn: Johnny_issc@hotmail.com
问题提交错了,Struts+Hibernate的问题,200分!
问题提交错了,Struts+Hibernate的问题,200分!
Struts+Hibernate问题
hibernate,struts,ejb
struts+hibernate如何整合在一起?
eclipse+struts+hibernate现在很火么?
[200分+]Struts+hibernate,答辩时可能问到的问题?要全面。
jsp 与 struts hibernate spring 的关系
谁有struts+hibernate+spring的例子?
求struts spring hibernate等j2ee教程
我在用Eclipse+hibernate+struts时,我想在ApplicationResources里解决中文的问题,可是始终是乱码
Struts、Spring和Hibernate的学习该如何安排?
请问struts,spring,hibernate间有什么异同?
hibernate
appFuse跟Spring、Hibernate、ibatis、struts等有什么区别和联系?
要在MyEclipse上编写Hibernate与struts的程序,需要装什么软件?
现在开发Struts+Spring+Hibernate的程序员一个月到手应该多少钱?
java, jsp, struts, weblogic ,Hibernate, j2ee ,xml,等等,学习的最好顺序是什么
struts怎么用
hibernate中怎么实现connect by的查询
struts是怎么实现用户注册和登录的?
如何用Struts实现管理大型复杂的网站
什么是struts,有什么用
怎么用struts+iReport+jasperReport制作报表