程序包
链接:https://pan.baidu.com/s/1sCSFE3kcLFNin04rZTl46Q
提取码:0000
复制这段内容后打开百度网盘手机App,操作更方便哦
分页
-
实现思路:
-
使用limit语句
-
select *from 表名 limit 0,20; #第一页 select *from 表名 limit 20,20; #第二页 select *from 表名 limit 40,20; #第三页
-
-
步骤
- 确定每页显示的数据总量 PageSize
- 确定分页显示所需的总页数 PageCount
- 确定分页的页码 PageNum
- 确定数据总数 totalSize
- 确定每页数据的存储 List date
- 编写SQL查询语句,实现数据查询
- 在JSP页面中进行分页显示设置
-
实践代码
//实体类
public class PageBean<T>{
private int pageSize; //页大小
private int pageNum; //页码
private int pageCount; //总页数
private long totalSize; //数据总数
private List<T> date; //页数据
private int startPage; //开始页码
private int endPage; //结束页
public PageBean() {
}
public PageBean(int pageSize, int pageNum, long totalSize, List<T> date) {
this.pageSize = pageSize;
this.pageNum = pageNum;
this.totalSize = totalSize;
this.date = date;
this.pageCount = (int) (totalSize%pageSize==0?totalSize/pageSize:totalSize/pageSize+1);
//设置开始页码和结束页码
//正常情况
this.startPage = this.pageNum-4;
this.endPage = this.pageNum+5;
//查看页码小于等于5
if (this.pageNum<=5){
this.startPage=1;
this.endPage = 10;
}
//查看的页大于等于 pageCount-5;
if (this.pageNum>=this.pageCount-5){
this.startPage=this.pageCount-9;
this.endPage = this.pageCount;
}
//如果页码小于10
if (this.pageCount<=10){
this.startPage=1;
this.endPage=this.pageCount;
}
}
//daoImpl
@Override
public long selectNum(String where, List<Object> params) {
String sql = "select count(*) from book "+where;
System.out.println(sql);
try {
return qr.query(sql, new ScalarHandler<>(), params.toArray());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<Book> selectBookByPage(int page_num, int page_size, String where, List<Object> params) {
params.add((page_num-1)*page_size);
params.add(page_size);
String sql = "select *from book "+ where +" order by id limit ?,? ";
System.out.println(sql);
try {
return qr.query(sql, new BeanListHandler<>(Book.class), params.toArray());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//serviceImpl
@Override
public PageBean<Book> queryBookByPage(int page_num, int page_size, String where, List<Object> params) {
long totalSize = bookDao.selectNum(where, params);
try {
List<Book> books = bookDao.selectBookByPage(page_num, page_size, where, params);
PageBean<Book> pageBean = new PageBean<>(page_size, page_num, totalSize, books);
return pageBean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = (String)request.getSession().getAttribute("username");
if (username==null){
response.sendRedirect(request.getContextPath()+"/login.html");
return;
}
int page_size = 10; //默认页码数据大小
int page_num = 1; //默认页码
String pageSize = request.getParameter("pageSize");
String pageNum = request.getParameter("pageNum");
if (!StringUtils.isEmpty(pageSize)){
page_size = Integer.parseInt(pageSize);
if (page_size<1){
page_size = 10;
}
}
if (!StringUtils.isEmpty(pageNum)){
page_num = Integer.parseInt(pageNum);
if (page_num<1){
page_num = 1;
}
}
//模糊查询加分页
String title = request.getParameter("title");
String author = request.getParameter("author");
String cid = request.getParameter("cid");
//封装条件和参数
StringBuilder where = new StringBuilder("where 1=1");
List<Object> params = new ArrayList<>();
if (!StringUtils.isEmpty(title)){
where.append(" and title like ?");
params.add("%"+title+"%");
}
if (!StringUtils.isEmpty(author)){
where.append(" and author like ?");
params.add("%"+author+"%");
}
if (!StringUtils.isEmpty(cid)){
where.append(" and cid= ?");
params.add(cid);
}
BookService bookService = new BookServiceImpl();
try {
// PageBean<Book> pageBean = bookService.queryBookByPage(page_num,page_size);
// request.setAttribute("pageBean", pageBean);
PageBean<Book> pageBean = bookService.queryBookByPage(page_num,page_size,where.toString(),params);
request.setAttribute("pageBean", pageBean);
//把查询条件加入request域中
request.setAttribute("title", title);
request.setAttribute("author", author);
request.setAttribute("cid", cid);
request.getRequestDispatcher("/bookList.jsp").forward(request, response);
} catch (Exception e) {
request.setAttribute("msg","查询失败"+e.getMessage());
System.out.println(e.getMessage());
request.getRequestDispatcher("/message.jsp").forward(request, response);
return;
}
//jsp
<h1>书籍展示</h1>
<a href="${pageContext.request.contextPath}/add.jsp">添加书籍</a>
<form action="pagequery" method="post" style="width: 90%; margin: 0 auto;">
<input type="text" name="title" placeholder="请输入书名" value="${title}">
<input type="text" name="author" placeholder="请输入作者" value="${author}">
<input type="text" name="cid" placeholder="请输入书籍类别" value="${cid}">
<input type="submit" value="搜索">
</form>
<table style="margin: 0 auto" width="90%" border="1">
<tr>
<th>id</th>
<th>书名</th>
<th>作者</th>
<th>出版时间</th>
<th>出版社</th>
<th>isbn</th>
<th>价格</th>
<th>图片</th>
<th>类别</th>
<th>操作</th>
</tr>
<%--分页显示--%>
<c:forEach items="${pageBean.date}" var="book">
<tr align="center">
<td>${book.id}</td>
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.publicDate}</td>
<td>${book.publisher}</td>
<td>${book.isbn}</td>
<td>${book.price}</td>
<%-- <td>${book.picture}</td>--%>
<td><img src="${pageContext.request.contextPath}/bookimages?picture=${book.picture} " width="50" height="50"></td>
<td>${book.cid}</td>
<td><a href="${pageContext.request.contextPath}/update?id=${book.id}">更新</a> <a href=${pageContext.request.contextPath}/deleteBook?id=${book.id} οnclick="return confirm('确定要删除吗?')">删除</a></td>
</tr>
</c:forEach>
</table>
<div style="width: 90%; margin: 0 auto">
<a href="${pageContext.request.contextPath}/pagequery?pageNum=1&pageSize=${pageBean.pageSize}">首页</a>
<a href="${pageContext.request.contextPath}/pagequery?pageNum=${pageBean.pageNum-1}&pageSize=${pageBean.pageSize}">上一页</a>
<c:forEach begin="${pageBean.startPage}" end="${pageBean.endPage}" var="num">
<c:if test="${pageBean.pageNum==num}">
<a class="active" href="${pageContext.request.contextPath}/pagequery?pageNum=${num}&pageSize=${pageBean.pageSize}">${num}</a>
</c:if>
<c:if test="${pageBean.pageNum!=num}">
<a href="${pageContext.request.contextPath}/pagequery?pageNum=${num}&pageSize=${pageBean.pageSize}">${num}</a>
</c:if>
</c:forEach>
<a href="${pageContext.request.contextPath}/pagequery?pageNum=${pageBean.pageNum+1}&pageSize=${pageBean.pageSize}">下一页</a>
<a href="${pageContext.request.contextPath}/pagequery?pageNum=${pageBean.pageCount}&pageSize=${pageBean.pageSize}">尾页</a>
【共${pageBean.pageNum}/${pageBean.pageCount}页】
</div>