JSP中的分页查询

本文介绍了在JSP中如何实现分页查询,包括确定每页数据量、总页数计算以及编写SQL查询语句的实际操作示例。

实现分页

步骤

确定每页显示的数据量

确定分页显示所需的总页数

编写sql查询语句,实现数据查询

1)limit分页公式
(1)limit分页公式:currentPage是当前第几页;pageSize是一页多少条记录
limit currentPage-1)*pageSize,pageSize
(2)用的地方:sql语句中
select * from student limit(curPage-1)*pageSize,pageSize;

2)总页数公式
(1)总页数公式:totalRecord是总记录数;pageSize是一页分多少条记录
int totalPageNum = (totalRecord +pageSize - 1) / pageSize;
(3)查询总条数:totalRecord是总记录数
SELECT COUNT(*) FROM tablename

示例

<%@page import="kgc.pojo.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	//javaBean
<jsp:useBean id="studentService" class="kgc.service.StudentService"></jsp:useBean>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生列表</title>
<style type="text/css">
ul{
display:flex;
justify-content:space-between;
padding: 0px;
}
li{
list-style-type:none;
}
a{
display:block;
text-decoration:none;
width:50px;
text-align:center;
}
.active{
background-color:pink;
}
</style>
</head>
<%
  	String cpStr = request.getParameter("currentPage");
	//默认当前页为第一页
	int currentPage=(cpStr==null?1:Integer.parseInt(cpStr)); 
	String psStr = request.getParameter("pageSize");
	//默认当前页有10条数据
	int pageSize = (psStr==null?10:Integer.parseInt(psStr));
	//studentService.queryPageCount(),studentService.queryPage()封装在service层
	int pageCount = studentService.queryPageCount(pageSize); 
	List<Student> stus = studentService.queryPage(currentPage,pageSize);

/* String message=(String)request.getAttribute("message");

if(message!=null){
	out.print(message);
}
 */
%>
<body>
	<div style="margin: 0px auto;width: 900px" >
		<table border="1px" cellspacing="0px" style="width:900px">
			<tr>
				<th colspan="5">学生信息</th>
			</tr>
			<tr>
				<th>学生学号</th>
				<th>学生姓名</th>
				<th>学生手机</th>
				<th>学生地址</th>
				<th>操作</th>
			</tr>
			<%
				for (Student stu : stus) {
			%>

			<tr>
				<td style="display: none"><%=stu.getId()%></td>
				<td><%=stu.getStudentNo()%></td>
				<td><%=stu.getStudentName()%></td>
				<td><%=stu.getPhone()%></td>
				<td><%=stu.getAddress()%></td>
				<td><a href="update.jsp?id=<%=stu.getId()%>">修改</a> <a
					href="delete.jsp?id=<%=stu.getId()%>">删除</a></td>
			</tr>
			<%
				}
			%>
		</table>
		<ul>
		<li><a href="index.jsp?currentPage=1">首页</a></li>
		<li><a href="index.jsp?currentPage=<%=currentPage-1>0?currentPage-1:1%>">上一页</a></li>
		<li class="<%=currentPage%5==1?"active":"" %>"><a href="index.jsp?currentPage=<%=currentPage/5==0?1:(currentPage-1)/5*5+1%>"><%=currentPage/5==0?1:(currentPage-1)/5*5+1%><span class="sr-only"></span></a></li>
		<li class="<%=currentPage%5==2?"active":"" %>"><a href="index.jsp?currentPage=<%=currentPage/5==0?2:(currentPage-1)/5*5+2%>"><%=currentPage/5==0?2:(currentPage-1)/5*5+2%><span class="sr-only"></span></a></li>
		<li class="<%=currentPage%5==3?"active":"" %>"><a href="index.jsp?currentPage=<%=currentPage/5==0?3:(currentPage-1)/5*5+3%>"><%=currentPage/5==0?3:(currentPage-1)/5*5+3%><span class="sr-only"></span></a></li>
		<li class="<%=currentPage%5==4?"active":"" %>"><a href="index.jsp?currentPage=<%=currentPage/5==0?4:(currentPage-1)/5*5+4%>"><%=currentPage/5==0?4:(currentPage-1)/5*5+4%><span class="sr-only"></span></a></li>
		<li class="<%=currentPage%5==0?"active":"" %>"><a href="index.jsp?currentPage=<%=currentPage/5==0?5:(currentPage-1)/5*5+5%>"><%=currentPage/5==0?5:(currentPage-1)/5*5+5%><span class="sr-only"></span></a></li>
		<li><a href="index.jsp?currentPage=<%=currentPage+1>pageCount?currentPage:currentPage+1 %>">下一页</a></li>
		<li><a href="index.jsp?currentPage=<%=pageCount%>">末页</a></li>
		<li><input type="number" max="<%=pageCount %>" min="1"/><span>页</span><button id="btn1">跳转</button></li>
		<li><input type="number" max="20" min="5"/><span>条/页</span><button id="btn2">跳转</button></li>
		</ul>
	</div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$("#btn1").click(function(){
	//获取参数
	var num1 =$($(this).parent().children(0)).val();
	window.location.href="index.jsp?currentPage="+num1;
})
$("#btn2").click(function(){
	//获取参数
	var num2 =$($(this).parent().children(0)).val();
	window.location.href="index.jsp?pageSize="+num2;
})
</script>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值