现在的位置: 首页 > 综合 > 正文

使用Hibernate进行简单分页

2018年05月22日 ⁄ 综合 ⁄ 共 3968字 ⁄ 字号 评论关闭

daoImpl类中的方法

 

 public List<Details> queryDetailByPage(int cPage, int pagesize) throws Exception

{

     Session  session =HibernateSessionFactory.getSession();
     Transaction trans = session.beginTransaction();

    //cPage为页号,pagesize为每页显示的行数,start为起始点
     int start = (cPage - 1) * pagesize;
     Query q = session.createQuery(
    "from Details order by time desc,mid desc");

    q.setFirstResult(start);
    q.setMaxResults(pagesize);
    @SuppressWarnings("unused")
    List<Details> l = q.list();
     trans.commit();
     session.close();
     return l;
 }

 

2.这是Action中的调用方法

public ActionForward execute(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response) {
 // TODO Auto-generated method stub
     DetailsDaoImpl dao = new DetailsDaoImpl();
     Integer cPage =null;
     List<Details> l = null;
     Double m=null;
     int countPage=0;
     Double less = null;
     try

        {
          cPage =Integer.parseInt(request.getParameter("cPage"));
          l  = dao.queryDetailByPage(cPage, 10);
          m = dao.countDetailsMoney();
          less = dao.getLeftMoney();
          countPage = (dao.countDetails()+10-1)/10;
          i f(cPage>countPage)
              {
                       throw new Exception();
              }
      
         } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return new ActionForward("/error.html");
 }
     request.setAttribute("less",less);
     request.setAttribute("countPage", countPage);
     request.setAttribute("m", m);
     request.setAttribute("l", l);
     request.setAttribute("cPage", cPage);
     return mapping.findForward("scuess");
 }

 

在JSP中的代码如下

 

     <table style="border: dotted #E6E6E6">
       <tr bgcolor="#CCCCCC">
        <td width="300" align="center">
         <strong>消费标题</strong>
        </td>
        <td width="220" align="center">
         <strong>消费时间</strong>
        </td>
        <td width="110" align="center">
         <strong>消费金额</strong>
        </td>
       </tr>
       <logic:present name="l">
        <logic:iterate id="one" name="l">
         <logic:present name="one">
          <tr bgcolor="#CCCCCC">
           <td width="300" align="center">
            <a href="showDetails.do?detailsId=${one.mid}">${one.title
             }</a>
           </td>
           <td width="220" align="center">
            <fmt:setLocale value='zh-CN' />
            <fmt:formatDate value='${one.time}' dateStyle='long' />

           </td>
           <td width="110" align="center">
            ${one.money }
           </td>
          </tr>
         </logic:present>
        </logic:iterate>
       </logic:present>
       <tr bgcolor="#CCCCCC">
        <td width="300" align="center" style="background-color: #FFFFFF">
         <%
             if ((Integer.parseInt(request.getParameter("cPage")) - 1) < 1) {
         %>
         <font size="2"><strong>第一页</strong> </font>
         <%
             } else {
         %>
         <a
          href="showAddDetails.do?cPage=<%=Integer.parseInt(request.getParameter("cPage"))
   - 1%>"><font
          size="2"><strong>上一页</strong> </font> </a>

         <%
             }
         %>
         /
         <%
             if ((Integer.parseInt(request.getParameter("cPage"))) == (Integer) request
               .getAttribute("countPage")) {
         %>
         <font size="2"><strong>下一页</strong> </font>
         <%
             } else {
         %>
         <a
          href="showAddDetails.do?cPage=<%=Integer.parseInt(request.getParameter("cPage"))
   + 1%>"><font
          size="2"><strong>下一页</strong> </font> </a>
         <%
             }
         %>
         (
         <font size="2"><strong>${cPage}/${countPage}页</strong> </font>)
        </td>
        <td width="220" align="center" style="background-color: #FFFFCC">
         <strong>消费/余额</strong>
        </td>
        <td width="110" align="center" style="background-color: #FFFFCC">
         <strong>${m}/ <%
             double ll = (Double) request.getAttribute("less");
             System.out.println(ll);
             if (ll < 10) {
         %> <font color="red"> <%
     } else {
 %> <font color="blue"> <%
     }
 %> ${less } </font>
         </strong>
        </td>
       </tr>

 </table>

抱歉!评论已关闭.