Struts2+Hibernate分页显示实例

下面是一个struts2+hibernate的分页显示,主要是用到了hibernate的相应分页方法大大简单了分页的代码

版本:Struts2.1.8

         Hibernate3.2

         Oracle9g

具体代码如下:

1.Plan.java

package com.creattech.plan; public class Plan { private Integer id; private String planId; private Integer materialId; private Integer materialIdl; private String materialName; private String speciFication; private String unit; private String planPurchase; private Integer completionrate; private Integer expuintPrice; private Integer expunitPriceo; private Integer exptotalPrice; private Integer exptotalPriceo; private Integer demandDepartment; public Integer getCompletionrate() { return completionrate; } public void setCompletionrate(Integer completionrate) { this.completionrate = completionrate; } public Integer getDemandDepartment() { return demandDepartment; } public void setDemandDepartment(Integer demandDepartment) { this.demandDepartment = demandDepartment; } public Integer getExptotalPrice() { return exptotalPrice; } public void setExptotalPrice(Integer exptotalPrice) { this.exptotalPrice = exptotalPrice; } public Integer getExptotalPriceo() { return exptotalPriceo; } public void setExptotalPriceo(Integer exptotalPriceo) { this.exptotalPriceo = exptotalPriceo; } public Integer getExpuintPrice() { return expuintPrice; } public void setExpuintPrice(Integer expuintPrice) { this.expuintPrice = expuintPrice; } public Integer getExpunitPriceo() { return expunitPriceo; } public void setExpunitPriceo(Integer expunitPriceo) { this.expunitPriceo = expunitPriceo; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getMaterialId() { return materialId; } public void setMaterialId(Integer materialId) { this.materialId = materialId; } public Integer getMaterialIdl() { return materialIdl; } public void setMaterialIdl(Integer materialIdl) { this.materialIdl = materialIdl; } public String getPlanId() { return planId; } public void setPlanId(String planId) { this.planId = planId; } public String getPlanPurchase() { return planPurchase; } public void setPlanPurchase(String planPurchase) { this.planPurchase = planPurchase; } public String getSpeciFication() { return speciFication; } public void setSpeciFication(String speciFication) { this.speciFication = speciFication; } public String getUnit() { return unit; } public void setUnit(String unit) { this.unit = unit; } public String getMaterialName() { return materialName; } public void setMaterialName(String materialName) { this.materialName = materialName; } }

 

2.Plan.hbm.xml

<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.creattech.plan"> <class name="Plan" table="PLAN" lazy="false" > <id name="id" column="ID"> <generator class="native" /> </id> <property name="planId" column="PLANID" type="string" /> <property name="materialId" column="MATERIALID" /> <property name="materialIdl" column="MATERIALIDL" /> <property name="materialName" column="MATERIALNAME" /> <property name="speciFication" column="SPECIFICATION" /> <property name="unit" column="UNIT" type="string" /> <property name="planPurchase" column="PLANPURCHASE" /> <property name="completionrate" column="COMPLETIONRATE" /> <property name="expuintPrice" column="EXPUINTPRICE" /> <property name="expunitPriceo" column="EXPUNITPRICEO" /> <property name="exptotalPrice" column="EXPTOTALPRICE" /> <property name="exptotalPriceo" column="EXPTOTALPRICEO" /> <property name="demandDepartment" column="DEMANDDEPARTMENT" /> </class> </hibernate-mapping>

 

3.建表

     大家可以根据实体类和配置文件自己建表

 

4.PlanDao.java

package com.creattech.plan.dao; import java.util.List; import com.creattech.plan.Plan; public interface PlanDao { public int getPlanTotalPage(int rowsPerPage); public List<Plan> findPlantByPage(int page, int rowsPerPage); public int getPlanNum(); }

 5.HibernateUtils.java

package com.creattech.plan.dao.impl; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static Configuration conf; private static SessionFactory factory; static { conf = new Configuration(); conf.configure(); factory = conf.buildSessionFactory(); } public static Session getSession() { return factory.openSession(); } public static SessionFactory getSessionFactory() { return factory; } public static void main(String[] args) { Session session = HibernateUtils.getSession(); System.out.println(session); } }

6.PlanDaoImpl.java

package com.creattech.plan.dao.impl; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.creattech.plan.Plan; import com.creattech.plan.dao.PlanDao; public class PlanDaoImpl implements PlanDao { /** * 查找并返回所有计划 */ public List<Plan> findPlantByPage(int page, int rowsPerPage) { Session session = HibernateUtils.getSession(); Query query = session.createQuery("from Plan order by planId desc"); query.setMaxResults(rowsPerPage); // 每页最多显示几条 query.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始 List<Plan> list = query.list(); for (int i = 0; i < list.size(); i++) { System.out.println("findPlantByPage:" + list.get(i).getMaterialName()); } session.close(); return list; } /** * 共多少页计划数据 */ public int getPlanTotalPage(int rowsPerPage) { // System.out.println("rowsPerPage:" + rowsPerPage); int rows = 0; String hql = "select count(*) from Plan"; Session session = HibernateUtils.getSession(); Query query = session.createQuery(hql); rows = ((Integer) query.iterate().next()).intValue(); // System.out.println("rows:" + rows); session.close(); if (rows % rowsPerPage == 0) { return rows / rowsPerPage; } else { return rows / rowsPerPage + 1; } } public int getPlanNum() { String hql = "select count(*) from Plan "; int rows = 0; Session session = HibernateUtils.getSession(); Query query = session.createQuery(hql); rows = ((Integer) query.iterate().next()).intValue(); session.close(); return rows; } /** * 条件查询后返回的计划总页数 */ public int getPlanTotalPage(int rowsPerPage, String type, String search) { int rows = 0; Session session = HibernateUtils.getSession(); String hql = "select count(*) from Plan p where p." + type + " like :type"; Query query = session.createQuery(hql); query.setString("type", "%" + search + "%"); rows = ((Integer) query.iterate().next()).intValue(); // System.out.println("rows:" + rows); session.close(); if (rows % rowsPerPage == 0) { return rows / rowsPerPage; } else { return rows / rowsPerPage + 1; } } /** * 条件查询后返回的计划数据总数 */ public int getPlanNum(String type, String search) { int rows = 0; Session session = HibernateUtils.getSession(); String hql = "select count(*) from Plan p where p." + type + " like :type"; Query query = session.createQuery(hql); query.setString("type", "%" + search + "%"); rows = ((Integer) query.iterate().next()).intValue(); // System.out.println("rows:" + rows); session.close(); return rows; } }

 

7.struts.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="page" extends="default" namespace="/"> <action name="showpage" class="com.com.creattech.plan.action.ShowAction"> <result name="success" type="dispatcher"> /WEB-INF/jsp/showpage.jsp</result> </action> </package> </struts>

 8.ShowAction.java

package com.creattech.plan.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import com.creattech.plan.Plan; import com.creattech.plan.service.PlanService; import com.creattech.plan.service.impl.PlanServiceImpl; public class ShowAction { PlanService ps = new PlanServiceImpl(); List<Plan> pagePlanList = new ArrayList(); private int rowsPerPage = 10;// 每页显示几条 private int page = 1; // 默认当前页 private int totalPage;// 总共多少页 private int planNum;// 总过多少条 public String show() { System.out.println("Page:" + page); pagePlanList = ps.findPlantByPage(page, rowsPerPage); totalPage = ps.getPlanTotalPage(rowsPerPage); planNum = ps.getPlanNum(); return "success"; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getRowsPerPage() { return rowsPerPage; } public void setRowsPerPage(int rowsPerPage) { this.rowsPerPage = rowsPerPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<Plan> getPagePlanList() { return pagePlanList; } public void setPagePlanList(List<Plan> pagePlanList) { this.pagePlanList = pagePlanList; } public int getPlanNum() { return planNum; } public void setPlanNum(int planNum) { this.planNum = planNum; } }

9.showpage.jsp

<%@ page contentType="text/html; charset=utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% request.setCharacterEncoding("utf-8"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <link href="../css/common.css" mce_href="css/common.css" rel="stylesheet" type="text/css" /> <link href="../../css/css.css" mce_href="css/css.css" rel="stylesheet" type="text/css"> <mce:style type="text/css"><!-- .none { border-color:#FFFFFF; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; } .style1 {color: #0000FF} .style2 {color: #FF0000} --></mce:style><style type="text/css" mce_bogus="1">.none { border-color:#FFFFFF; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; } .style1 {color: #0000FF} .style2 {color: #FF0000}</style> <mce:script language="javascript" src="../js/edit.js" mce_src="js/edit.js" /><!-- <script language="javascript" src="../js/prototype-1.6.0.3.js" mce_src="js/prototype-1.6.0.3.js" /> <script language="javascript"> function cClose(){ parent.window.close(); } // --></mce:script> </head> <body> <table id="nav" width="850" border="0" cellpadding="0" cellspacing="0" height="30"> <tr> <td>   您现在的位置: <strong> 修改计划</strong>               <s:property value="message"/></td> </tr> </table> <form name="form1" id="form1" action="" method="get"> <font color="red"> <ww:property value="#session.msg"/></font> <table border="1" cellspacing="0" cellpadding="0" bordercolor="#999999" > <tr bgcolor="#DDDDDD"> <td width="40" height="30"><div align="center"><strong>选择</strong></div></td> <td width="150" ><div align="center"><strong>计划编号</strong></div></td> <td width="150"><div align="center"><strong>物资编码(8位)</strong></div></td> <td width="200"><div align="center"><strong>物资名称</strong></div></td> <td width="300"><div align="center"><strong>需求单位</strong></div></td> </tr> </table> <table border="0"> <% int i=1; %> <s:iterator value="pagePlanList"> <%if (i==1){ %> <tr bgcolor="rgb(214,223,247)"><%i=0;}else {i=1; %><tr bgcolor="rgb(122,161,230)"><%} %> <td width="37" height="30"><div align="center"><input type="radio" name="selectContract" value="<s:property value="planId" />" /></div></td> <td width="148" ><div align="center" ><a href="editPlan.action?planId=<s:property value=" mce_href="editPlan.action?planId=<s:property value="planId" ></a>"><s:property value="planId" /><a></a></div></td> <td width="148"><div align="center"><s:property value="materialId" /></div></td> <td width="198"><div align="center"><s:property value="materialName" /></div></td> <td width="300"><div align="center"><s:property value="demandDepartment" /></div></td> </tr> </s:iterator> </table> <table width="850" border="0" cellpadding="0" cellspacing="0"> <tr> <td bgcolor="E3E3E3" class="wang" align="center">  <span class="x2" align="center"> <font color="#0072BC"><b> 当前第<s:property value="page"/> 页,共<s:property value="planNum"/>条记录, 共分<s:property value="totalPage"/>页</b></font> </span> </td> </tr> <input type="hidden" name="maxNum" value=""> <tr align="center" valign="top" > <td height="20"> <p align="center"> <span class="x2"><a href="showEditPlan.action?page=1" mce_href="showEditPlan.action?page=1">首 页</a> <s:if test="page<=1"> 上一页<img src="../images/aleft.gif" mce_src="images/aleft.gif" width="18" height="16" border="0" align="absbottom"> </s:if> <s:else> <a href="showEditPlan.action?page=<s:property value=" mce_href="showEditPlan.action?page=<s:property value="page-1"></a>">上一页 <img src="../images/aleft.gif" mce_src="images/aleft.gif" width="18" height="16" border="0" align="absbottom"></a> </s:else> <s:if test="page>=totalPage"> <img src="../images/aright.gif" mce_src="images/aright.gif" width="18" height="16" border="0" align="absbottom">下一页 </s:if> <s:else> <a href="showEditPlan.action?page=<s:property value=" mce_href="showEditPlan.action?page=<s:property value="page+1"></a>"> <img src="../images/aright.gif" mce_src="images/aright.gif" width="18" height="16" border="0" align="absbottom">下一页</a> </s:else> <a href="showEditPlan.action?page=<s:property value=" mce_href="showEditPlan.action?page=<s:property value="totalPage"></a>">最后一页</a> </span> </p> </td> </tr> </table> <hr width="850" align="left"/> <table width="100%"> <tr bgcolor="#ffffff"> <td align="center" > <input type="hidden" value="" id="pId" name="pId"/> <input type="submit" value="查看" style="width:70px" onClick="return rCheck()" /> <input type="button" value="关闭" style="width:70px" onClick="cClose()" /> </td> </tr> </table> </form> </body> </html>

 

PS:由于没有把CSS,JS等代码粘上来可能此实例不能直接使用,但核心代码以写了出来(PlanDaoImpl.java)

大家可以参考参考,如果有什么不明白的地方可以留言

你可能感兴趣的:(Hibernate,session,String,struts,Integer,border)