分页操作要点
- 每页显示的记录数—pageSize
- 总共的记录数—totalCount
- 总共页数—totalPage
- 当前页数—currentPage
将要点封装成类: PageUtil
public class PageUtil {
private int pageSize; // 每页记录数
private int totalCount; // 总记录数
private int currentPage; // 当前页数
private int totalPage; // 总页数
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize + 1);
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
Mysql数据库分页语句: 通过limit实现
- limit: index, size: 从下标index开始(从0开始),查询size条记录
- 示例: 每页显示3条记录,显示第二页记录,mysql语句为
limit 3, 3
- 语句的传参写法:
limit (CurrentPage - 1) * PageSize, PageSize
修改Dao查询语句
public List<Book> getAll(PageUtil pu) {
List<Book> list = new ArrayList<Book> ();
String sql = "select * from book limit ?, ?";
try {
ResultSet rs = this.executeQuery(sql, (pu.getCurrentPage() - 1) * pu.getPageSize(), pu.getPageSize());
while(rs.next()) { // 当rs还有下一个数据时
// 从rs中取得不同属性,并创建新对象后存入容器
list.add(new Book(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getDate(5), rs.getInt(6)));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close();
}
return list;
}
- 实体Dao查询代码
private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PageUtil pageUtil = new PageUtil();
// 设置总共记录数
pageUtil.setTotalCount(bookDao.count());
// 设置每页显示数量
pageUtil.setPageSize(5);
//获取当前页数 --- 默认为第一页
int currentPage = 1;
if(req.getParameter("currentPage") != null) {
currentPage = Integer.parseInt(req.getParameter("currentPage"));
}
pageUtil.setCurrentPage(currentPage);
//调用Dao直接查询
List<Book> list = bookDao.getAll(pageUtil);
List<Category> clist = categoryDao.getList();
req.setAttribute("list", list);
req.setAttribute("clist", clist);
req.setAttribute("page", pageUtil);
req.getRequestDispatcher("list.jsp").forward(req, resp);
}
- jsp页面处理
<td colspan=7 align="right">
<a href="book?op=list¤tPage=${page.currentPage-1 }"
<c:if test="${page.currentPage == 1 }">style="color:#ccc" onclick="return false"</c:if>
>previous</a>
<c:forEach begin="1" step="1" end="${page.totalPage }" var="pg">
<a href="book?op=list¤tPage=${pg }">${pg }</a>
</c:forEach>
<a href="book?op=list¤tPage=${page.currentPage+1 }"
<c:if test="${page.currentPage == page.totalPage }">style="color:#ccc" onclick="return false"</c:if>
>next</a>
<span>Total ${page.totalPage } pages</span> <%-- 直接取值代表着调用对应的get方法 --%>
go to<input type="text" id="cp" style="width:15px"/>page<input type="button" value="goto" onclick="search();"/>
</td>
<script>
function search() {
var cp = document.getElementById("cp").value;
location.href = "book?op=list¤tPage=" + cp;
}
</script>