相信大家在搭建后台的时候,经常会使用到分页功能,当然,目前有不少框架(如esayUI)都自带分页的实现,为了更好的理解分页原理,近期本人自己摸索了关于分页查询的一些心得。

归根结底,分页的核心还是在封装PageBean,并通过一定的算法对其进行判断,赋值

public class PageBean<T> implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int currentPage; //当前第几页 ,请求传过来
private int pageCount; // 每页显示多少条数据
private int totalCount; //总记录数,查询数据获得 private int totalPage; //总页数, 通过 totalCount 和 pageCount计算获得 private int startIndex; //开始索引,与pageCount 组成 limit 条件 private List<T> pageData; //分页显示的页数,如 1,2,3,4
private int start;
private int end; public PageBean(int currentPage,int pageCount,int totalCount){
this.currentPage=currentPage;
this.pageCount=pageCount;
this.totalCount=totalCount; if(totalCount % pageCount==0){
this.totalPage = totalCount/ pageCount;
}else{
this.totalPage= totalCount/pageCount+1;
} this.startIndex = (currentPage-1)*pageCount; this.start=1;
this.end=5; if(pageCount<=5){
this.end= this.totalPage;
}else{ this.start=currentPage-2;
this.end = currentPage+2; if(start<=0){
this.start=1;
this.end=5;
}
if(this.end > this.totalPage){
this.end=totalPage;
this.start=end-4;
}
}
} public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public int getPageCount() {
return pageCount;
} public void setPageCount(int pageCount) {
this.pageCount = pageCount;
} public int getTotalCount() {
return totalCount;
} public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
} public int getTotalPage() {
return totalPage;
} public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
} public int getStartIndex() {
return startIndex;
} public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
} public List<T> getPageData() {
return pageData;
} public void setPageData(List<T> pageData) {
this.pageData = pageData;
} public int getStart() {
return start;
} public void setStart(int start) {
this.start = start;
} public int getEnd() {
return end;
} public void setEnd(int end) {
this.end = end;
} public static long getSerialversionuid() {
return serialVersionUID;
} }

Controller层

         int currentPage = Integer.valueOf(request.getParameter("currentPage"));

         int pageCount=7;

         BlogTypeService blogType = new BlogTypeServiceImpl();

         PageBean<BlogType> page = blogType.page(currentPage, pageCount);
request.setAttribute("page", page);
request.getRequestDispatcher("/jsp/blogType.jsp").forward(request, response);

service层

public PageBean<BlogType> page(int currentPage,int pageCount) {
int totalCount=blogType.getTotal(); PageBean<BlogType> pageBean = new PageBean<>(currentPage, pageCount, totalCount);
int startIndex = pageBean.getStartIndex();
pageBean.setPageData(blogType.page(startIndex, pageCount));
return pageBean;
}

dao层

public List<BlogType> page(int currentPage,int pageCount){
String sql = "select * from t_blogtype limit ?,?";
PageBean<BlogType> pageBean =null;
Connection conn =null;
PreparedStatement ps =null;
ResultSet rs =null;
BlogType blogType =null;
try {
conn =DBDao.connection();
ps=conn.prepareStatement(sql);
ps.setInt(1, currentPage);
ps.setInt(2, pageCount);
rs=ps.executeQuery();
List<BlogType> list =new ArrayList<BlogType>();
while(rs.next()){
blogType = new BlogType();
blogType.setId(rs.getInt("id"));
blogType.setTypeName(rs.getString("typeName"));
blogType.setOrderNum(pageCount);
list.add(blogType);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public int getTotal() {
String sql ="select count(id) from t_blogtype";
Connection conn =null;
PreparedStatement ps=null;
ResultSet rs=null;
int count=0;
try{
conn= DBDao.connection();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
rs.next();
count=rs.getInt(1); }catch(Exception e){
e.printStackTrace();
}
return count; }

  因为本文主要在说分页查询,所以关于 数据库的连接就一并放到dao里了!

  (其实本人是不知道将数据库的连接放在哪里。。。。嘻嘻)

jsp

 <body>
共有${page.totalCount }个分类,共有${page.pageCount }页,当前为第${page.currentPage }页,每页显示${page.pageCount }条
<table align="center" width="100%">
<thead>
<th>选择</th>
<th>序号</th>
<th>博客类别</th>
<th>博客排序</th>
<th>操作</th>
</thead>
<tbody align="center" border="1">
<c:forEach items="${page.pageData }" var="page">
<tr>
<td><input type="checkbox" name="id"/></td>
<td>${page.id }</td>
<td>${page.typeName }</td>
<td>${page.orderNum }</td>
<td><a>修改</a>|<a>删除</a></td>
<td></td>
</tr>
</c:forEach>
</tbody> </table>
<!-- 分页 -->
<div style="text-align:center;">
<a href="${pageContext.request.contextPath }/blogType?currentPage=1">首页</a>
<!-- 如果当前页为第一页,就没有上一页这个标签 -->
<c:if test="${page.currentPage==1 }">
<c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
<c:if test="${page.currentPage == i}">
${i}
</c:if>
<c:if test="${page.currentPage != i}">
<a href="${pageContext.request.contextPath}/blogType?currentPage=${i}">${i}</a>
</c:if>
</c:forEach>
<a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage+1}">下一页</a>
</c:if>
<!-- 如果不是首页也不是尾页,就与上一页和下一页 -->
<c:if test="${page.currentPage>1 && page.currentPage<page.totalPage }">
<a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage-1}">上一页</a>
<c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
<c:if test="${page.currentPage == i}">
${i}
</c:if>
<c:if test="${page.currentPage != i}">
<a href="${pageContext.request.contextPath}/blogType?currentPage=${i}">${i}</a>
</c:if>
</c:forEach>
<a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage+1}">下一页</a>
</c:if>
<!-- 如果是最后一页,则没有下一页 -->
<c:if test="${page.currentPage==page.totalPage }">
<a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage-1}">上一页</a>
<c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
<c:if test="${page.currentPage == i}">
${i}
</c:if>
<c:if test="${page.currentPage != i}">
<a href="${pageContext.request.contextPath}/blogType?currentPage=${i}">${i}</a>
</c:if>
</c:forEach> </c:if>
<a href="${pageContext.request.contextPath }/blogType?currentPage=${page.totalPage}">尾页</a>
</div> </body>

Servlet实现后台分页查询的更多相关文章

  1. Servlet 分页保存查询条件

    第一种情况:一个页面走一个JSP页面和Servlet 解决办法: /** 把用户这一次选择的所有条件保存Map集合中,再把 map存到Session会话中,点击分页时进入将Servlet中再将Sess ...

  2. Servlet实现数据库查询(MyEclipse10,Tomcat7.0,JDK1.7,)——Java Web练习(三)

    1.MyEclipse | New Web Project :TestServlet01,修改index.jsp的代码: <%@ page language="java" i ...

  3. jsp+servlet实现模糊查询和分页效果

    ---恢复内容开始--- 1.DAO+MVC包 2.DAO接口方法定义 package com.wanczy.dao; import java.math.BigDecimal;import java. ...

  4. 最简单的Servlet继承HttpServlet查询数据库登录验证

    <%-- Created by IntelliJ IDEA. User: yunqing Date: 2017-12-06 Time: 9:11 To change this template ...

  5. java使用插件pagehelper在mybatis中实现分页查询

    摘要: com.github.pagehelper.PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件 PageHelper是国内牛人的一个开源项目,有兴趣的可以去看源码,都有 ...

  6. java servlet手机app访问接口(三)高德地图云存储及检索

    这篇关于高德地图的随笔内容会多一点, 一.业务说明     对应APP业务中的成员有两类,一是服务人员,二是被服务人员,  主要实现功能, 对APP中的服务人员位置进行时时定位, 然后通过被服务人员登 ...

  7. java servlet手机app访问接口(一)数据加密传输验证

    前面几篇关于servlet的随笔,算是拉通了 servlet的简单使用流程,接下去的文章将主要围绕手机APP访问接口这块出发续写,md5加密传输--->短信验证--->手机推送---> ...

  8. 基于jsp+servlet图书管理系统之后台用户信息删除操作

    上一篇的博客写的是修改操作,且附有源码和数据库,这篇博客写的是删除操作,附有从头至尾写的代码(详细的注释)和数据库! 此次删除操作的源码和数据库:http://download.csdn.net/de ...

  9. Servlet+jsp的分页案例

    查询的分页,在web中经常用到.一般,分页要维护的信息很多,我们把这些相关的信息,分装到一个类中,PageBean.具体如下: package cn.itcast.utils; import java ...

随机推荐

  1. ceph存储引擎bluestore解析

    原文链接:http://www.sysnote.org/2016/08/19/ceph-bluestore/ ceph后端支持多种存储引擎,以插件式的方式来进行管理使用,目前支持filestore,k ...

  2. 展示博客(Beta阶段)

    展示博客 0x00 团队成员 成员 博客地址 简介 黄建英 http://www.cnblogs.com/smilehjy/ beta阶段的新成员,负责前端界面调整 谢晓萍 http://www.cn ...

  3. 软件工程资料 - UCSD 怎么教软件工程

    2012年,在上软件工程课的过程中,收集到这个博客,作为学习资料. 原网站 (http://www.arc-trooper.com)已经不存在,这是一个备份. ----------------- 以下 ...

  4. 【Alpha】Daily Scrum Meeting——Day5

    站立式会议照片 1.本次会议为第五次Meeting会议: 2.本次会议在上午大课间09:40,在陆大楼召开,本次会议为30分钟讨论昨天的任务完成情况以及接下来的任务安排. 燃尽图 每个人的工作分配 成 ...

  5. 201521044091 《Java学习笔记》 第六周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结.注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖面 ...

  6. 201521123071《Java程序设计》第1周学习总结

    1. 本章学习总结 通过本周的学习,对java的一些语法以及java的发展史有了一定的基础认识,也了解了JDK的安装,以及环境变量定义和配置等知识.还有对码云,Markdown等的使用,大大方便了我们 ...

  7. 201521123065《java程序设计》第14周学习总结

    1. 本周学习总结 1.大部分情况下使用的数据库是关系型的数据库,使用表存储数据: 2.关系型数据库可以通过唯一的主键查找记录,也可以通过多个信息确定主键: 3.Mysql操作:显示-show dat ...

  8. 201521123025《java程序设计》第10周学习总结

    1. 本周学习总结 2. 书面作业 Q1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? 只有执行过try语句块,finall ...

  9. 201521123018 《Java程序设计》第14周学习总结

    1. 本章学习总结 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语句(截图) 添 ...

  10. 超简单的js评价小星星

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...