ssh2分頁查詢初試,放着記錄學習一下。
entity:student.java:
- package com.zte.entity;
- /**
- * 數據持久化,跟數據庫的的相應的表的字段是對應的。
- *
- *
- */
- public class Student
- {
- private Integer id;
- private String name;
- private Integer age;
- private Integer score;
- private String email;
- private String phone;
- public String getEmail()
- {
- return email;
- }
- public void setEmail(String email)
- {
- this.email = email;
- }
- public String getPhone()
- {
- return phone;
- }
- public void setPhone(String phone)
- {
- this.phone = phone;
- }
- public Integer getId()
- {
- return id;
- }
- public void setId(Integer id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public Integer getAge()
- {
- return age;
- }
- public void setAge(Integer age)
- {
- this.age = age;
- }
- public Integer getScore()
- {
- return score;
- }
- public void setScore(Integer score)
- {
- this.score = score;
- }
- }
- <?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.zte.entity">
- <class name="Student" table="student">
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="identity">
- </generator>
- </id>
- <property name="name" column="name" type="java.lang.String"></property>
- <property name="age" column="age" type="java.lang.Integer"></property>
- <property name="score" column="score" type="java.lang.Integer"></property>
- <property name="email" column="email" type="java.lang.String"></property>
- <property name="phone" column="phone" type="java.lang.String"></property>
- </class>
- </hibernate-mapping>
dao層:StudentDao.java
- public interface StudentDao<T>
- {
- public QueryResult<T> getScrollData(int firstindex, int maxresult); // 獲得分頁記錄
- }
StudentDaoImpl.java:
- public class StudentImpl<T> implements StudentDao
- {
- private SessionFactory sessionFactory;// 通過spring注入數據持久化工廠(相當於spring幫你設置好了
- // 對象,直接通過getter/setter的方式獲取)
- public SessionFactory getSessionFactory()
- {
- return sessionFactory;
- }
- public void setSessionFactory(SessionFactory sessionFactory)
- {
- this.sessionFactory = sessionFactory;
- }
- @Override
- public QueryResult getScrollData(int firstindex, int maxresult)
- {
- QueryResult retuslt = new QueryResult<T>();
- Query query =
- sessionFactory.getCurrentSession().createQuery("from Student");
- System.out.println("query---size---before>>>" + query.list().size());
- retuslt.setTotalrecord(query.list().size());
- query.setFirstResult(firstindex).setMaxResults(maxresult);
- System.out.println("query---size---after>>>" + query.list().size());
- retuslt.setResultlist(query.list());
- return retuslt;
- }
services層:
StudentService.java:
- public interface StudentService<T>
- {
- public QueryResult<T> getScrollData(int firstindex, int maxresult);
- }
- public class StudentServiceImpl implements StudentService
- {
- private StudentDao studentDao;// 通過spring的bean依賴注入對象
- public StudentDao getStudentDao()
- {
- return studentDao;
- }
- public void setStudentDao(StudentDao studentDao)
- {
- this.studentDao = studentDao;
- }
- @Override
- public QueryResult getScrollData(int firstindex, int maxresult)
- {
- return studentDao.getScrollData(firstindex, maxresult);
- }
Action:
BaseAction.java:
- public class BaseAction extends ActionSupport implements ServletRequestAware,
- ServletResponseAware
- {
- public Integer page; // 當前頁信息
- public String query; // 是否爲條件查詢
- HttpServletRequest request;
- HttpServletResponse response;
- public Integer getPage()
- {// 獲得當前頁信息
- return page = (page == null || page < 1) ? 1 : page;
- }
- public void setPage(Integer page)
- {// 設置當前頁信息
- this.page = page;
- }
- public String getQuery()
- {// 獲得query信息
- return query;
- }
- public void setQuery(String query)
- {// 設置query信息
- this.query = query;
- }
- @Override
- public void setServletResponse(HttpServletResponse arg0)
- {
- this.response = arg0;
- }
- @Override
- public void setServletRequest(HttpServletRequest arg0)
- {
- this.request = arg0;
- }
QueryAction.java
- public class QueryAction extends BaseAction
- {
- public StudentService studentService;
- public StudentService getStudentService()
- {
- return studentService;
- }
- public void setStudentService(StudentService studentService)
- {
- this.studentService = studentService;
- }
- public String query()
- {
- PageView<Student> pageView = new PageView<Student>(5, getPage());
- pageView.setQueryResult(studentService.getScrollData(
- pageView.getFirstResult(), pageView.getMaxresult()));// 查詢所有記錄
- request.setAttribute("pageView", pageView);// 保存到request範圍
- return this.SUCCESS;
- }
- }
Util,分頁工具類:
- public class PageIndex {
- private long startindex;
- private long endindex;
- public PageIndex(long startindex, long endindex) {
- this.startindex = startindex;
- this.endindex = endindex;
- }
- public long getStartindex() {
- return startindex;
- }
- public void setStartindex(long startindex) {
- this.startindex = startindex;
- }
- public long getEndindex() {
- return endindex;
- }
- public void setEndindex(long endindex) {
- this.endindex = endindex;
- }
- public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
- long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
- long endpage = currentPage+viewpagecount/2;
- if(startpage<1){
- startpage = 1;
- if(totalpage>=viewpagecount) endpage = viewpagecount;
- else endpage = totalpage;
- }
- if(endpage>totalpage){
- endpage = totalpage;
- if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
- else startpage = 1;
- }
- return new PageIndex(startpage, endpage);
- }
- }
PageView.java:
- public class PageView<T> {
- /** 分頁數據 **/
- private List<T> records;
- /** 頁碼開始索引和結束索引 **/
- private PageIndex pageindex;
- /** 總頁數 **/
- private long totalpage = 1;
- /** 每頁顯示記錄數 **/
- private int maxresult = 12;
- /** 當前頁 **/
- private int currentpage = 1;
- /** 總記錄數 **/
- private long totalrecord;
- /** 頁碼數量 **/
- private int pagecode = 10;
- /** 要獲取記錄的開始索引 **/
- public int getFirstResult() {
- return (this.currentpage-1)*this.maxresult;
- }
- public int getPagecode() {
- return pagecode;
- }
- public void setPagecode(int pagecode) {
- this.pagecode = pagecode;
- }
- public PageView(int maxresult, int currentpage) {
- this.maxresult = maxresult;
- this.currentpage = currentpage;
- }
- public void setQueryResult(QueryResult<T> qr){
- setTotalrecord(qr.getTotalrecord());
- setRecords(qr.getResultlist());
- }
- public long getTotalrecord() {
- return totalrecord;
- }
- public void setTotalrecord(long totalrecord) {
- this.totalrecord = totalrecord;
- setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
- }
- public List<T> getRecords() {
- return records;
- }
- public void setRecords(List<T> records) {
- this.records = records;
- }
- public PageIndex getPageindex() {
- return pageindex;
- }
- public long getTotalpage() {
- return totalpage;
- }
- public void setTotalpage(long totalpage) {
- this.totalpage = totalpage;
- this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
- }
- public int getMaxresult() {
- return maxresult;
- }
- public int getCurrentpage() {
- return currentpage;
- }
QueryResult.java,數據集
- /**
- * 分頁實體類封裝
- *
- */
- public class QueryResult<T> {
- /** 獲得結果集 **/
- private List<T> resultlist;
- /** 獲得總的記錄數 **/
- private long totalrecord;
- public List<T> getResultlist() {
- return resultlist;
- }
- public void setResultlist(List<T> resultlist) {
- this.resultlist = resultlist;
- }
- public long getTotalrecord() {
- return totalrecord;
- }
- public void setTotalrecord(long totalrecord) {
- this.totalrecord = totalrecord;
- }
- }
分頁jsp:
- <%@ page language="java" pageEncoding="GB18030"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <font color="blue"> 當前頁:第${pageView.currentpage}頁 |
- 總記錄數:${pageView.totalrecord}條 | 每頁顯示:${pageView.maxresult}條 |
- 總頁數:${pageView.totalpage}頁</font>
- <c:forEach begin="${pageView.pageindex.startindex}"
- end="${pageView.pageindex.endindex}" var="wp">
- <c:if test="${pageView.currentpage==wp}">
- <b><font color="red">第${wp}頁</font></b>
- </c:if>
- <c:if test="${pageView.currentpage!=wp}">
- <a href="javascript:topage('${wp}')" class="a03">第${wp}頁</a>
- </c:if>
- </c:forEach>
分頁的頁面:
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="utf-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <script type="text/javascript">
- //到指定的分頁頁面
- function topage(page) {
- var form = document.forms[0];
- form.page.value = page;
- form.submit();
- }
- </script>
- </head>
- <body>
- <form action="queryAction" method="post">
- <s:hidden name="page" />
- <s:hidden name="id" />
- <s:hidden name="name" />
- <s:hidden name="phone" />
- <s:hidden name="email" />
- <s:hidden name="age" />
- <s:hidden name="score" />
- <table width="800" border="0" cellPadding="0" cellSpacing="1"
- bgcolor="#6386d6">
- <!-- 列表標題欄 -->
- <tr bgcolor="#EFF3F7" class="TableBody1">
- <td width="10%" height="37" align="center"><b>客戶編號</b></td>
- <td width="10%" height="37" align="center"><B>客戶名稱</B></td>
- <td width="18%" height="37" align="center"><b>聯繫電話</b></td>
- <td width="18%" height="37" align="center"><b>聯繫地址</b></td>
- <td width="18%" height="37" align="center"><b>聯繫人</b></td>
- <td width="18%" height="37" align="center"><b>其他信息</b></td>
- <td width="10%" height="37" align="center"><b>操作</b></td>
- </tr>
- <!-- 列表數據欄 -->
- <s:if
- test="null != #request.pageView.records && !#request.pageView.records.isEmpty() ">
- <s:iterator value="#request.pageView.records" id="entity">
- <tr bgcolor="#EFF3F7" class="TableBody1"
- onmouseover="this.bgColor = '#DEE7FF';"
- onmouseout="this.bgColor='#EFF3F7';">
- <td align="center" vAlign="center">${entity.id }</td>
- <td align="center" vAlign="center">${entity.name }</td>
- <td align="center" vAlign="center">${entity.phone }</td>
- <td align="center" vAlign="center">${entity.email }</td>
- <td align="center" vAlign="center">${entity.age }</td>
- <td align="center" vAlign="center">${entity.score }</td>
- <td align="center" vAlign="center"><a href="#"
- onclick="del('customermanage_del.do?customerNO=${entity.id}');">刪除</a>
- <a href="#"
- onclick="openWin('customermanage_updateUI.do?customerNO=${entity.id}','addperson',600,200);">修改</a>
- </td>
- </tr>
- </s:iterator>
- </s:if>
- <!-- 在列表數據爲空的時候,要顯示的提示信息 -->
- <s:else>
- <tr>
- <td colspan="7" align="center" bgcolor="#EFF3F7" class="TableBody1"
- onmouseover="this.bgColor = '#DEE7FF';"
- onmouseout="this.bgColor='#EFF3F7';">沒有找到相應的記錄</td>
- </tr>
- </s:else>
- </table>
- <TABLE width="778" border=0 align=left cellPadding=0 cellSpacing=0
- borderColor=#ffffff style="FONT-SIZE: 10pt">
- <TBODY>
- <TR>
- <TD height=28 align=right vAlign=center noWrap
- background="images/list_middle.jpg"> <!-- 可以在這裏插入分頁導航條 -->
- <%@ include file="fenye.jsp"%>
- </TD>
- </TR>
- </TBODY>
- </TABLE>
- </form>
- </body>
- </html>
效果如圖:
原文鏈接:http://blog.csdn.net/csh159/article/details/9223293