java web分頁查詢初試

來源:互聯網
上載者:User

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;}}

Student.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.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;    }      @Overridepublic 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);}

StudentServiceImpl.java:

public class StudentServiceImpl implements StudentService{private StudentDao studentDao;// 通過spring的bean依賴注入對象    public StudentDao getStudentDao()    {        return studentDao;    }    public void setStudentDao(StudentDao studentDao)    {        this.studentDao = studentDao;    }@Overridepublic 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;}@Overridepublic void setServletResponse(HttpServletResponse arg0){this.response = arg0;}@Overridepublic 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:iftest="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=0borderColor=#ffffff style="FONT-SIZE: 10pt"><TBODY><TR><TD height=28 align=right vAlign=center noWrapbackground="images/list_middle.jpg">   <!-- 可以在這裡插入分頁導航條 --><%@ include file="fenye.jsp"%></TD></TR></TBODY></TABLE></form></body></html>

效果

具體的工程點這:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.