01 原始案例
2 JavaWeb分页技术
02 封装PageBean
我们需要的元素?
currentPage(当前页),pageSize(每页记录数)
totalPage(总页数)
pageList(当前页面封装的数据集合)
03 改造后端代码——BookInfoServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if ("list".equals(action)) {
// 该方式作废
listBook(request, response);
} else if ("listByPage".equals(action)) {
//分页查询列表
listByPage(request, response);
}
}
private void listByPage(HttpServletRequest request, HttpServletResponse response) {
// 获取请求参数:当前页码,每页信息数
String cp = request.getParameter("currentPage");
String is = request.getParameter("initSize");
// 赋予默认初值
Integer currentPage = 1;Integer initSize = 5;
if (cp != null && !"".equals(cp)) {currentPage = Integer.valueOf(cp);}
if (is != null && !"".equals(is)) {initSize = Integer.valueOf(is);}
// 获取当前页的书籍记录
List<BookInfo> bookPageList = bookInfoService.getPageList(currentPage, initSize);
// 获取总记录数
int count = bookInfoService.getBookCount();
PageBean<BookInfo> bookPageBeans = new PageBean<BookInfo>(initSize, count, currentPage, bookPageList);
request.setAttribute("bookPageBeans", bookPageBeans);
System.out.println(bookPageBeans);
try {request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (Exception e) {e.printStackTrace();}
}
03 改造后端代码——Service
//获取总页数
public int getBookCount() {
// TODO Auto-generated method stub
return dao.getBookCount();
}
//获取当前页记录列表
public List<BookInfo> getPageList(Integer currentPage, Integer initSize) {
// TODO Auto-generated method stub
return dao.getPageList(currentPage, initSize);
}
03 改造后端代码—— Dao层
public int getBookCount() {
String sql = "select count(*) from book_info;";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
int count = rs.getInt(1);
return count;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
04 改造前端代码
<table class="table table-hover">
<tr>
<th>图书编号</th>
<th>类别</th>
<th>图书名称</th>
<th>作者</th>
<th>价格</th>
<th>出版社</th>
<th>出版时间</th>
</tr>
<c:forEach items="${bookPageBeans.pageList}" var="book">
<tr>
<td>${book.bookId}</td>
<td>${book.typeName}</td>
<td>${book.bookName}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>${book.publisher}</td>
<td>${book.publishTime}</td>
</tr>
</c:forEach>
</table>
05 效果展示
3 MyBatis分页插件
MyBatis jar包
MySql驱动 jar包
所需jar包汇总
02 案例准备
03 简单使用PageHelper插件
04 PageHelper插件说明