分页
有一个limit关键字可以查询指定的行数:
Limitstart,limit
Start :从第几行以后开始的行号。>start
Limit :一共需要多少行。
第一步:向usre表中写入100行数据
第二步:先在sqlyog中用select做一个分页的分析
SELECT * FROM users LIMIT 10,10;
第三步:分析一共显示多少页
自己确定每一页显示几行:10行
pageSize= 10;
计算确定数据表一共分几页:
101/pageSize=100/10=10页。
rows= select count(1) from users;
方法1:pageCount = rows/pageSize +(rows%pageSize==0?0:1);
方法2:pageCount= (rows+ (pageSize-1))/(pageSize);
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int pageSize =5;
String page = request.getParameter("page");
int currentPage = 0;
if (null == page || page.trim().equals("")) {
currentPage = 1;
}else {
try {
currentPage = Integer.parseInt(page);
} catch (Exception e) {
}
}
request.setAttribute("currentPage", currentPage);
//第二步:获取数据表中有多少行
QueryRunner run = new QueryRunner(DataSourceUtils.getDatasSource());
String sql = "select count(*) from users";
try {
Object o = run.query(sql, new ScalarHandler());
int rows = Integer.parseInt(o.toString());
//第三步:计算一共分多少页
int pageCount = rows/pageSize+(rows%pageSize==0 ? 0:1);
if (currentPage < 1) {
currentPage = 1;
}
if (currentPage>pageCount) {
currentPage = pageCount;
}
//将页数放到req
request.setAttribute("pageCount",pageCount);
//第四步:查询指定的页面的数据
int start = (currentPage-1)*pageSize;
sql = "select * from users limit "+start+","+pageSize;
System.err.println(sql);
List<Map<String, Object>> datas
= run.query(sql, new MapListHandler());
//将数据封装到req
request.setAttribute("datas",datas);
//对分页的页码再分页
//定义每个页面显示多少个页码
int pageNum = 10;
int startNo = 0;
int endNo = 0;
if (pageCount < pageNum) {
startNo = 1;
endNo = pageCount;
}else{
if (currentPage <= pageNum/2) {
startNo =1;
endNo = pageNum;
}else {
startNo = currentPage-(pageNum/2-1);
endNo = startNo+(pageNum-1);
}
if (endNo >= pageCount) {
endNo = pageCount;
startNo = endNo - (pageNum-1);
}
}
System.err.println("startno:"+startNo);
request.setAttribute("startNo",startNo);
request.setAttribute("endNo",endNo);
} catch (SQLException e) {
e.printStackTrace();
}
//转发到
request.getRequestDispatcher("/show.jsp").forward(request, response);
}
第四步:分析limit的开始位置
用户请求的面码 | start | 算法: |
1 | Limit 0,pageSize | Start = (currentPage-1)*pageSize |
2 | Limit 10,pageSize | |
3 | Limit 20,pageSize |
第五步:对分页以后页码再分页
每个页面,最多显示10个页码
pageNum=10;
startNo
endNo
当前页码 | 页码范围 | 算法 |
1 | 1~10 | If(currentPage<=pageNum/2) 1~10 |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 | 2~11 | 6-4=2 = 6-(pageNum/2-1)=2 = startNo endNo = startNo+(pageNum-1)=11 |
7 | 3~12 | |
8 | 4~13 | |
9 |
|
|
10 |
|
|
11 |
| 11-4 = 7 endNo = 7+9=16 endNo = 11; startNo = 2 = endNo-(pageNum-1);
|
|
|
页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<p>以下是数据</p>
<c:forEach items="${datas}" var="data" varStatus="idx">
${idx.count},${data.id},${data.name}<br />
</c:forEach>
<hr />
<c:if test="${currentPage!=1}">
<a href="<c:url value='/page?page=${currentPage-1}'/>">上一页</a>
</c:if>
<c:forEach var="page" begin="1" end="${pageCount}"> <!--pageCount 总共多少页数 -->
<c:choose>
<c:when test="${currentPage==page}">
[
<font color="red"> ${page} </font>
]
</c:when>
<c:otherwise>
[<a href="<c:url value='/page?page=${page}'/>">${page}</a>]
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${currentPage!=pageCount}">
<a href="<c:url value='/page?page=${currentPage+1}'/>">下一页</a>
</c:if>
<hr />
<c:if test="${currentPage!=1}">
<a href="<c:url value='/page?page=${currentPage-1}'/>">上一页</a>
</c:if>
当前是第${currentPage},一共${pageCount }页
<input type="text" id="pc" />
<button>Go</button>
<c:if test="${currentPage!=pageCount}">
<a href="<c:url value='/page?page=${currentPage+1}'/>">下一页</a>
</c:if>
<hr color="red" />
<c:forEach var="page" begin="${startNo}" end="${endNo}">
<c:choose>
<c:when test="${currentPage==page}">
[
<font color="red"> ${page} </font>
]
</c:when>
<c:otherwise>
[<a href="<c:url value='/page?page=${page}'/>">${page}</a>]
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>