筆記如下
- 思路:
1.每一頁里有十條記錄,每頁就是一個(gè)PageBean,封裝了總記錄數(shù),當(dāng)前是第幾頁,每頁條數(shù),當(dāng)前是第幾頁的數(shù)據(jù)
2018-03-01_214242.png
2.兩個(gè)公式
2018-03-01_214242.png
- *jsp
<body>
<c:if test="${empty pageBean.customers}">
當(dāng)前頁沒有數(shù)據(jù)
</c:if>
<c:if test="${not empty pageBean.customers}">
<h3 style="text-align: center; ">當(dāng)前是${pageBean.pageNum}頁數(shù)據(jù)</h3>
<table border="1" width="100%">
<tr>
<th>客戶姓名</th>
<th>客戶性別</th>
<th>客戶生日</th>
<th>客戶郵箱</th>
<th>客戶手機(jī)</th>
<th>客戶愛好</th>
<th>客戶類型</th>
<th>客戶描述</th>
</tr>
<c:forEach items="${pageBean.customers}" var="customer">
<tr>
<td>${customer.name}</td>
<td>${customer.gender}</td>
<td>${customer.birthday}</td>
<td>${customer.email}</td>
<td>${customer.cellphone}</td>
<td>${customer.preference}</td>
<td>${customer.type}</td>
<td>${customer.description}</td>
</tr>
</c:forEach>
</table>
<br/>
<div style="text-align: center; ">
<c:if test="${pageBean.pageNum != 1 }">
<a href="${pageContext.request.contextPath}/pagequery?pagenum=1">首頁</a>
<a href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.pageNum-1}">上一頁</a>
</c:if>
<!-- 參考百度左五右四 -->
<c:forEach begin="${pageBean.pageNum-5>0?pageBean.pageNum-5:1}" end="${pageBean.pageNum+4<pageBean.totalPageNum?pageBean.pageNum+4:pageBean.totalPageNum}" var="i">
<c:if test="${pageBean.pageNum == i}">
<font color="red">${i}</font>
</c:if><c:if test="${pageBean.pageNum != i}">
<a href="${pageContext.request.contextPath}/pagequery?pagenum=${i}">${i}</a>
</c:if>
</c:forEach>
<c:if test="${pageBean.pageNum != pageBean.totalPageNum }">
<a href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.pageNum+1}">下一頁</a>
<a href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.totalPageNum}">尾頁</a>
</c:if>
</div>
</c:if>
</body>
- web層(PageQueryServlet.java)
//拿到需要看到頁是那一頁
String pagenum = request.getParameter("pagenum");
//調(diào)用業(yè)務(wù)層去查詢目標(biāo)頁的數(shù)據(jù)
CustomerService cs = new CustomerService();
//返回當(dāng)前頁的目標(biāo)數(shù)據(jù)是不夠的,還要做導(dǎo)航條
//為了做分頁,需要引入一個(gè)新的javaBean
PageBean pageBean = cs.pageQuery(pagenum);
//將pagebean存到request域中
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/bean.jsp").forward(request, response);
- 業(yè)務(wù)層(CustomerService.java)
//完成分頁查詢的業(yè)務(wù)方法:
public PageBean pageQuery(String num) {
int numberPerPage = 10;//默認(rèn)每頁十條
int pageNum = Integer.parseInt(num);//當(dāng)前頁是那一頁
//從數(shù)據(jù)庫里查找總記錄數(shù)
int totalRecordsCount = cdao.getToltalCount();
//運(yùn)用公式
int totalPageNum = (totalRecordsCount + numberPerPage-1)/numberPerPage;//總頁數(shù)
//select * from customers limit ?,?
int startIndex = (pageNum-1)*numberPerPage;
//從數(shù)據(jù)庫里查找10條記錄
List<Customer> customers = cdao.getCurrectPageData(startIndex,numberPerPage);
//封裝數(shù)據(jù)
PageBean pageBean = new PageBean();
pageBean.setNumberPerPage(numberPerPage);
pageBean.setCustomers(customers);
pageBean.setPageNum(pageNum);
pageBean.setTotalPageNum(totalPageNum);
pageBean.setTotalRecordsCount(totalRecordsCount);
return pageBean;
}
dao層(CustomerDaoImpl.java)
//獲得總記錄條數(shù)
@Override
public int getToltalCount() {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
try {
long count = (long) runner.query("select count(*) from customers", new ScalarHandler());
return (int)count;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//查詢目標(biāo)頁的數(shù)據(jù)返回
@Override
public List<Customer> getCurrectPageData(int startIndex, int numberPerPage) {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from customers limit ?,?";
try {
return runner.query(sql, new BeanListHandler<Customer>(Customer.class),startIndex,numberPerPage);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
實(shí)現(xiàn)效果:
4.png