分页查询功能分析
好处:
减轻服务器内存的开销
提升用户体验
分析具体步骤
PageBean实体类:
package com.example.domain; import java.util.List; /* 分页对象(分页查询实体类) */ public class PageBean<T> { private int totalCount;// 总记录数 private int totalPage;// 总页码 private List<T> list;// 每页数据 list集合 private int currentPage;// 当前页码 private int rows;// 每页显示的条数 @Override public String toString() { return "PageBean{" + "totalCount=" + totalCount + ", totalPage=" + totalPage + ", list=" + list + ", currentPage=" + currentPage + ", rows=" + rows + '}'; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } }
后台代码实现
UserDao接口:
/** * 查询总记录数 * @return */ int findTotalCount(); /** * 分页查询每页记录 * @param start * @param rows * @return */ List<User1> findByPage(int start, int rows);
UserDaoImpl实体类:
@Override public int findTotalCount() { //1.定义sql String sql = "select count(*) from user1"; return template.queryForObject(sql, Integer.class); } @Override public List<User1> findByPage(int start, int rows) { //1.定义sql语句 String sql = "select * from user1 limit ? , ? "; return template.query(sql, new BeanPropertyRowMapper<User1>(User1.class),start,rows); }
UserService接口:
/** * 批量删除用户 * @param ids */ void delSelectedUser(String[] ids); /** * 分页查询 * @param currentPage * @param rows * @return */ PageBean<User1> findUserByPage(String currentPage, String rows);
UserServiceImpl实体类:
@Override public PageBean<User1> findUserByPage(String _currentPage, String _rows) { int currentPage = Integer.parseInt(_currentPage); int rows = Integer.parseInt(_rows); if (currentPage <= 0){ currentPage = 1; }else if (currentPage >= 8){ currentPage = 7; } //1.创建空的PageBean对象 PageBean<User1> pd = new PageBean<User1>(); //2.设置参数 pd.setCurrentPage(currentPage); pd.setRows(rows); //3.调用dao查询totalCount总记录数 int totalCount = dao.findTotalCount(); pd.setTotalCount(totalCount); //4.调用dao查询List集合 //计算开始的记录索引 int start = (currentPage -1) * rows; List<User1> list = dao.findByPage(start,rows); pd.setList(list); //5.计算总页码 int totalPage = (totalCount % rows) == 0 ? totalCount/rows : (totalCount/rows) + 1;// 三元运算符 pd.setTotalPage(totalPage); return pd; }
FindUserByPageServlet类:
package com.example.web.servlet; import com.example.domain.PageBean; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet(name = "FindUserByPageServlet", value = "/findUserByPageServlet") public class FindUserByPageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取参数 String currentPage = request.getParameter("currentPage");// 获取当前页面参数 String rows = request.getParameter("rows");// 获取每页条数 if (currentPage == null || "".equals(currentPage) ){ currentPage = "1"; } if (rows == null || "".equals(rows)){ rows = "5"; } //2.调用service查询 UserService service = new UserServiceImpl(); PageBean<User1> pd = service.findUserByPage(currentPage,rows); System.out.println(pd); //3.将PageBean存入request存储 request.setAttribute("pd", pd); //4.转发到list.jsp页面 request.getRequestDispatcher("/list.jsp").forward(request, response); } }
前台代码
list.jsp
<div> <nav aria-label="Page navigation"> <ul class="pagination"> <c:if test="${pd.currentPage == 1}"> <li class="disabled"> </c:if> <c:if test="${pd.currentPage != 1}"> <li> </c:if> <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pd.currentPage-1}&rows=5" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <c:forEach begin="1" end="${pd.totalPage}" var="i"> <!-- 分页选中 --> <c:if test="${pd.currentPage == i}"> <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5">${i}</a></li> </c:if> <c:if test="${pd.currentPage != i}"> <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5">${i}</a></li> </c:if> </c:forEach> <li> <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pd.currentPage+1}&rows=5" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> <span style="font-size: 25px;margin-left: 5px"> 共${pd.totalCount}条记录,共${pd.totalPage}页 </span> </ul> </nav> </div>
index.jsp
<div align="center"> <a href="${pageContext.request.contextPath}/findUserByPageServlet" style="text-decoration:none;font-size:33px">查询所有用户信息 </a> </div>
标签:功能,rows,分页,int,list,查询,totalCount,currentPage,public From: https://www.cnblogs.com/qihaokuan/p/17021484.html