实现分页
- 因为jdbc中的占位符不允许在关键字前后进行加减乘除运算,所以需要在java代码中计算页数,在传入SQL语句。
- jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<body>
<table border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<c:forEach items="${PageInfo.list }" var="pi">
<tr>
<td>${pi.id }</td>
<td>${pi.name }</td>
<td>${pi.age }</td>
</tr>
</c:forEach>
</table>
<a href="page?pageNumber=${PageInfo.pageNumber-1 }&pageSize=${PageInfo.pageSize}" <c:if test="${PageInfo.pageNumber<=1 }"> onclick="javascript:return false;"</c:if>>上一页</a>
<a href="page?pageNumber=${PageInfo.pageNumber+1 }&pageSize=${PageInfo.pageSize}" <c:if test="${PageInfo.pageNumber>=PageInfo.total }"> onclick="javascript:return false;"</c:if>>下一页</a>
</body>
int pageSize = 2;
Map<String, Object> map = new HashMap<>();
//显示几个
map.put("pageSize", pageSize);
//第几页
map.put("pageStart", pageSize*(pageNumber-1));
List<People> p2 = session.selectList("a.b.page",map);
System.out.println(p2);
session.close();
<select id="page" resultType="xxx.xxx.xxx.People" parameterType="map">
select * from people limit #{pageStart},#{pageSize}
</select>
limit的索引从0开始。