java web分页查询初试

本文详细介绍了如何使用SSH2框架进行分页查询,结合学生实体类的实现,展示了一套完整的数据分页获取流程,包括实体类定义、数据库映射、DAO层接口实现、服务层调用及分页展示逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ssh2分页查询初试,放着记录学习一下。

entity:student.java:

[html]  view plain copy print ?
  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:

[html]  view plain copy print ?
  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

[html]  view plain copy print ?
  1. public interface StudentDao<T>  
  2. {  
  3.     public QueryResult<T> getScrollData(int firstindex, int maxresult); // 获得分页记录  
  4. }  

StudentDaoImpl.java:

[html]  view plain copy print ?
  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:

[html]  view plain copy print ?
  1. public interface StudentService<T>  
  2. {  
  3.     public QueryResult<T> getScrollData(int firstindex, int maxresult);  
  4. }  
StudentServiceImpl.java:

[html]  view plain copy print ?
  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:

[html]  view plain copy print ?
  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

[html]  view plain copy print ?
  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,分页工具类:

[html]  view plain copy print ?
  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:

[html]  view plain copy print ?
  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,数据集

[html]  view plain copy print ?
  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:

[html]  view plain copy print ?
  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>  

分页的页面:

[html]  view plain copy print ?
  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.youkuaiyun.com/csh159/article/details/9223293


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值