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.youkuaiyun.com/csh159/article/details/9223293