java web分頁查詢初試

ssh2分頁查詢初試,放着記錄學習一下。

entity:student.java:

  1. package com.zte.entity;  
  2.   
  3. /**  
  4.  * 數據持久化,跟數據庫的的相應的表的字段是對應的。  
  5.  *   
  6.  *   
  7.  */  
  8. public class Student  
  9. {  
  10.   
  11.     private Integer id;  
  12.   
  13.     private String name;  
  14.   
  15.     private Integer age;  
  16.   
  17.     private Integer score;  
  18.   
  19.     private String email;  
  20.   
  21.     private String phone;  
  22.   
  23.     public String getEmail()  
  24.     {  
  25.         return email;  
  26.     }  
  27.   
  28.     public void setEmail(String email)  
  29.     {  
  30.         this.email = email;  
  31.     }  
  32.   
  33.     public String getPhone()  
  34.     {  
  35.         return phone;  
  36.     }  
  37.   
  38.     public void setPhone(String phone)  
  39.     {  
  40.         this.phone = phone;  
  41.     }  
  42.   
  43.     public Integer getId()  
  44.     {  
  45.         return id;  
  46.     }  
  47.   
  48.     public void setId(Integer id)  
  49.     {  
  50.         this.id = id;  
  51.     }  
  52.   
  53.     public String getName()  
  54.     {  
  55.         return name;  
  56.     }  
  57.   
  58.     public void setName(String name)  
  59.     {  
  60.         this.name = name;  
  61.     }  
  62.   
  63.     public Integer getAge()  
  64.     {  
  65.         return age;  
  66.     }  
  67.   
  68.     public void setAge(Integer age)  
  69.     {  
  70.         this.age = age;  
  71.     }  
  72.   
  73.     public Integer getScore()  
  74.     {  
  75.         return score;  
  76.     }  
  77.   
  78.     public void setScore(Integer score)  
  79.     {  
  80.         this.score = score;  
  81.     }  
  82.   
  83. }  
Student.hbm.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  3. <hibernate-mapping package="com.zte.entity">  
  4.     <class name="Student" table="student">  
  5.         <id name="id" column="id" type="java.lang.Integer">  
  6.             <generator class="identity">  
  7.             </generator>  
  8.         </id>  
  9.         <property name="name" column="name" type="java.lang.String"></property>  
  10.         <property name="age" column="age" type="java.lang.Integer"></property>  
  11.         <property name="score" column="score" type="java.lang.Integer"></property>  
  12.         <property name="email" column="email" type="java.lang.String"></property>  
  13.         <property name="phone" column="phone" type="java.lang.String"></property>  
  14.     </class>  
  15. </hibernate-mapping>  

dao層:StudentDao.java

  1. public interface StudentDao<T>  
  2. {  
  3.     public QueryResult<T> getScrollData(int firstindex, int maxresult); // 獲得分頁記錄  
  4. }  

StudentDaoImpl.java:

  1. public class StudentImpl<T> implements StudentDao  
  2. {  
  3.   
  4.     private SessionFactory sessionFactory;// 通過spring注入數據持久化工廠(相當於spring幫你設置好了  
  5.                                             // 對象,直接通過getter/setter的方式獲取)  
  6.   
  7.     public SessionFactory getSessionFactory()  
  8.     {  
  9.         return sessionFactory;  
  10.     }  
  11.   
  12.     public void setSessionFactory(SessionFactory sessionFactory)  
  13.     {  
  14.         this.sessionFactory = sessionFactory;  
  15.     }  
  16.       @Override  
  17.     public QueryResult getScrollData(int firstindex, int maxresult)  
  18.     {  
  19.         QueryResult retuslt = new QueryResult<T>();  
  20.         Query query =  
  21.                 sessionFactory.getCurrentSession().createQuery("from Student");  
  22.         System.out.println("query---size---before>>>" + query.list().size());  
  23.         retuslt.setTotalrecord(query.list().size());  
  24.         query.setFirstResult(firstindex).setMaxResults(maxresult);  
  25.         System.out.println("query---size---after>>>" + query.list().size());  
  26.         retuslt.setResultlist(query.list());  
  27.         return retuslt;  
  28.     }  

services層:

StudentService.java:

  1. public interface StudentService<T>  
  2. {  
  3.     public QueryResult<T> getScrollData(int firstindex, int maxresult);  
  4. }  
StudentServiceImpl.java:

  1. public class StudentServiceImpl implements StudentService  
  2. {  
  3.   
  4.     private StudentDao studentDao;// 通過spring的bean依賴注入對象  
  5.     public StudentDao getStudentDao()  
  6.     {  
  7.         return studentDao;  
  8.     }  
  9.   
  10.     public void setStudentDao(StudentDao studentDao)  
  11.     {  
  12.         this.studentDao = studentDao;  
  13.     }  
  14.     @Override  
  15.     public QueryResult getScrollData(int firstindex, int maxresult)  
  16.     {  
  17.         return studentDao.getScrollData(firstindex, maxresult);  
  18.     }  

Action:

BaseAction.java:

  1. public class BaseAction extends ActionSupport implements ServletRequestAware,  
  2.         ServletResponseAware  
  3. {  
  4.   
  5.     public Integer page; // 當前頁信息  
  6.   
  7.     public String query; // 是否爲條件查詢  
  8.   
  9.     HttpServletRequest request;  
  10.   
  11.     HttpServletResponse response;  
  12.   
  13.     public Integer getPage()  
  14.     {// 獲得當前頁信息  
  15.         return page = (page == null || page < 1) ? 1 : page;  
  16.     }  
  17.   
  18.     public void setPage(Integer page)  
  19.     {// 設置當前頁信息  
  20.         this.page = page;  
  21.     }  
  22.   
  23.     public String getQuery()  
  24.     {// 獲得query信息  
  25.         return query;  
  26.     }  
  27.   
  28.     public void setQuery(String query)  
  29.     {// 設置query信息  
  30.         this.query = query;  
  31.     }  
  32.   
  33.     @Override  
  34.     public void setServletResponse(HttpServletResponse arg0)  
  35.     {  
  36.         this.response = arg0;  
  37.   
  38.     }  
  39.   
  40.     @Override  
  41.     public void setServletRequest(HttpServletRequest arg0)  
  42.     {  
  43.         this.request = arg0;  
  44.   
  45.     }  

QueryAction.java

  1. public class QueryAction extends BaseAction   
  2. {  
  3.   
  4.     public StudentService studentService;  
  5.   
  6.     public StudentService getStudentService()  
  7.     {  
  8.         return studentService;  
  9.     }  
  10.   
  11.     public void setStudentService(StudentService studentService)  
  12.     {  
  13.         this.studentService = studentService;  
  14.     }  
  15.   
  16.     public String query()  
  17.     {  
  18.         PageView<Student> pageView = new PageView<Student>(5, getPage());  
  19.         pageView.setQueryResult(studentService.getScrollData(  
  20.                 pageView.getFirstResult(), pageView.getMaxresult()));// 查詢所有記錄  
  21.         request.setAttribute("pageView", pageView);// 保存到request範圍  
  22.         return this.SUCCESS;  
  23.     }  
  24. }  

Util,分頁工具類:

  1. public class PageIndex {  
  2.     private long startindex;  
  3.     private long endindex;  
  4.       
  5.     public PageIndex(long startindex, long endindex) {  
  6.         this.startindex = startindex;  
  7.         this.endindex = endindex;  
  8.     }  
  9.     public long getStartindex() {  
  10.         return startindex;  
  11.     }  
  12.     public void setStartindex(long startindex) {  
  13.         this.startindex = startindex;  
  14.     }  
  15.     public long getEndindex() {  
  16.         return endindex;  
  17.     }  
  18.     public void setEndindex(long endindex) {  
  19.         this.endindex = endindex;  
  20.     }  
  21.        
  22.     public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){  
  23.             long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);  
  24.             long endpage = currentPage+viewpagecount/2;  
  25.             if(startpage<1){  
  26.                 startpage = 1;  
  27.                 if(totalpage>=viewpagecount) endpage = viewpagecount;  
  28.                 else endpage = totalpage;  
  29.             }  
  30.             if(endpage>totalpage){  
  31.                 endpage = totalpage;  
  32.                 if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;  
  33.                 else startpage = 1;  
  34.             }  
  35.             return new PageIndex(startpage, endpage);         
  36.     }  
  37. }  

PageView.java:

  1. public class PageView<T> {  
  2.     /** 分頁數據 **/  
  3.     private List<T> records;  
  4.     /** 頁碼開始索引和結束索引 **/  
  5.     private PageIndex pageindex;  
  6.     /** 總頁數 **/  
  7.     private long totalpage = 1;  
  8.     /** 每頁顯示記錄數 **/  
  9.     private int maxresult = 12;  
  10.     /** 當前頁 **/  
  11.     private int currentpage = 1;  
  12.     /** 總記錄數 **/  
  13.     private long totalrecord;  
  14.     /** 頁碼數量 **/  
  15.     private int pagecode = 10;  
  16.     /** 要獲取記錄的開始索引 **/  
  17.     public int getFirstResult() {  
  18.         return (this.currentpage-1)*this.maxresult;  
  19.     }  
  20.     public int getPagecode() {  
  21.         return pagecode;  
  22.     }  
  23.   
  24.     public void setPagecode(int pagecode) {  
  25.         this.pagecode = pagecode;  
  26.     }  
  27.   
  28.     public PageView(int maxresult, int currentpage) {  
  29.         this.maxresult = maxresult;  
  30.         this.currentpage = currentpage;  
  31.     }  
  32.       
  33.     public void setQueryResult(QueryResult<T> qr){  
  34.         setTotalrecord(qr.getTotalrecord());  
  35.         setRecords(qr.getResultlist());  
  36.     }  
  37.       
  38.     public long getTotalrecord() {  
  39.         return totalrecord;  
  40.     }  
  41.     public void setTotalrecord(long totalrecord) {  
  42.         this.totalrecord = totalrecord;  
  43.         setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);  
  44.     }  
  45.     public List<T> getRecords() {  
  46.         return records;  
  47.     }  
  48.     public void setRecords(List<T> records) {  
  49.         this.records = records;  
  50.     }  
  51.     public PageIndex getPageindex() {  
  52.         return pageindex;  
  53.     }  
  54.     public long getTotalpage() {  
  55.         return totalpage;  
  56.     }  
  57.     public void setTotalpage(long totalpage) {  
  58.         this.totalpage = totalpage;  
  59.         this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);  
  60.     }  
  61.     public int getMaxresult() {  
  62.         return maxresult;  
  63.     }  
  64.     public int getCurrentpage() {  
  65.         return currentpage;  
  66.     }  

QueryResult.java,數據集

  1. /**  
  2.  * 分頁實體類封裝  
  3.  *  
  4.  */  
  5. public class QueryResult<T> {  
  6.     /** 獲得結果集 **/  
  7.     private List<T> resultlist;  
  8.     /** 獲得總的記錄數 **/  
  9.     private long totalrecord;  
  10.       
  11.     public List<T> getResultlist() {  
  12.         return resultlist;  
  13.     }  
  14.     public void setResultlist(List<T> resultlist) {  
  15.         this.resultlist = resultlist;  
  16.     }  
  17.     public long getTotalrecord() {  
  18.         return totalrecord;  
  19.     }  
  20.     public void setTotalrecord(long totalrecord) {  
  21.         this.totalrecord = totalrecord;  
  22.     }  
  23. }  

分頁jsp:

  1. <%@ page language="java" pageEncoding="GB18030"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  3. <font color="blue"> 當前頁:第${pageView.currentpage}頁 |  
  4.     總記錄數:${pageView.totalrecord}條 | 每頁顯示:${pageView.maxresult}條 |  
  5.     總頁數:${pageView.totalpage}頁</font>  
  6. <c:forEach begin="${pageView.pageindex.startindex}"  
  7.     end="${pageView.pageindex.endindex}" var="wp">  
  8.     <c:if test="${pageView.currentpage==wp}">  
  9.         <b><font color="red">第${wp}頁</font></b>  
  10.     </c:if>  
  11.     <c:if test="${pageView.currentpage!=wp}">  
  12.         <a href="javascript:topage('${wp}')" class="a03">第${wp}頁</a>  
  13.     </c:if>  
  14. </c:forEach>  

分頁的頁面:

  1. <%@ page language="java" contentType="text/html; charset=GB18030"  
  2.     pageEncoding="utf-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags"%>  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <script type="text/javascript">  
  8.     //到指定的分頁頁面  
  9.     function topage(page) {  
  10.         var form = document.forms[0];  
  11.         form.page.value = page;  
  12.         form.submit();  
  13.     }  
  14. </script>  
  15. </head>  
  16. <body>  
  17.     <form action="queryAction" method="post">  
  18.         <s:hidden name="page" />  
  19.         <s:hidden name="id" />  
  20.         <s:hidden name="name" />  
  21.         <s:hidden name="phone" />  
  22.         <s:hidden name="email" />  
  23.         <s:hidden name="age" />  
  24.         <s:hidden name="score" />  
  25.         <table width="800" border="0" cellPadding="0" cellSpacing="1"  
  26.             bgcolor="#6386d6">  
  27.             <!-- 列表標題欄 -->  
  28.             <tr bgcolor="#EFF3F7" class="TableBody1">  
  29.                 <td width="10%" height="37" align="center"><b>客戶編號</b></td>  
  30.                 <td width="10%" height="37" align="center"><B>客戶名稱</B></td>  
  31.                 <td width="18%" height="37" align="center"><b>聯繫電話</b></td>  
  32.                 <td width="18%" height="37" align="center"><b>聯繫地址</b></td>  
  33.                 <td width="18%" height="37" align="center"><b>聯繫人</b></td>  
  34.                 <td width="18%" height="37" align="center"><b>其他信息</b></td>  
  35.                 <td width="10%" height="37" align="center"><b>操作</b></td>  
  36.             </tr>  
  37.             <!-- 列表數據欄 -->  
  38.             <s:if  
  39.                 test="null != #request.pageView.records && !#request.pageView.records.isEmpty() ">  
  40.                 <s:iterator value="#request.pageView.records" id="entity">  
  41.                     <tr bgcolor="#EFF3F7" class="TableBody1"  
  42.                         onmouseover="this.bgColor = '#DEE7FF';"  
  43.                         onmouseout="this.bgColor='#EFF3F7';">  
  44.                         <td align="center" vAlign="center">${entity.id }</td>  
  45.                         <td align="center" vAlign="center">${entity.name }</td>  
  46.                         <td align="center" vAlign="center">${entity.phone }</td>  
  47.                         <td align="center" vAlign="center">${entity.email }</td>  
  48.                         <td align="center" vAlign="center">${entity.age }</td>  
  49.                         <td align="center" vAlign="center">${entity.score }</td>  
  50.                         <td align="center" vAlign="center"><a href="#"  
  51.                             onclick="del('customermanage_del.do?customerNO=${entity.id}');">刪除</a>  
  52.                             <a href="#"  
  53.                             onclick="openWin('customermanage_updateUI.do?customerNO=${entity.id}','addperson',600,200);">修改</a>  
  54.                         </td>  
  55.                     </tr>  
  56.                 </s:iterator>  
  57.             </s:if>  
  58.             <!-- 在列表數據爲空的時候,要顯示的提示信息 -->  
  59.             <s:else>  
  60.                 <tr>  
  61.                     <td colspan="7" align="center" bgcolor="#EFF3F7" class="TableBody1"  
  62.                         onmouseover="this.bgColor = '#DEE7FF';"  
  63.                         onmouseout="this.bgColor='#EFF3F7';">沒有找到相應的記錄</td>  
  64.                 </tr>  
  65.             </s:else>  
  66.         </table>  
  67.         <TABLE width="778" border=0 align=left cellPadding=0 cellSpacing=0  
  68.             borderColor=#ffffff style="FONT-SIZE: 10pt">  
  69.             <TBODY>  
  70.                 <TR>  
  71.                     <TD height=28 align=right vAlign=center noWrap  
  72.                         background="images/list_middle.jpg">   <!-- 可以在這裏插入分頁導航條 -->  
  73.                         <%@ include file="fenye.jsp"%>  
  74.                     </TD>  
  75.                 </TR>  
  76.             </TBODY>  
  77.         </TABLE>  
  78.     </form>  
  79. </body>  
  80. </html>  

效果如圖:


原文鏈接:http://blog.csdn.net/csh159/article/details/9223293


發佈了36 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章