SpringMVC框架下實現原生分頁功能
阿新 • • 發佈:2020-08-20
本文暫不講解Mybatis外掛PageHelper分頁(雖然它很好用)
1、建立實體類Page.java
@Entity public class Page { private int totalRecord;// 表示查詢後一共得到多少條結果記錄 private int pageSize; // 表示頁面一次要顯示多少條記錄 private int totalPage;// 表示將所有的記錄進行分頁後,一共有多少頁 private int startIndex;// 表示從所有的結果記錄中的哪一個編號開始分頁查詢 private int currentPage; //表示使用者想看的頁數 @SuppressWarnings("unchecked") private List list =null;// list集合是用來裝載一個頁面中的所有記錄的 public Page(int pageNum, int totalRecord) { this.currentPage = pageNum; this.totalRecord = totalRecord; this.pageSize = 5;// 設定一頁預設顯示10條查詢記錄 this.startIndex = (this.currentPage - 1) * this.pageSize;// 至於為什麼this.page要減1, // 是因為mysql資料庫對於分頁查詢時,得到的所有的查詢記錄,第一條記錄的編號是從0開始。 if (this.totalRecord % this.pageSize == 0) { this.totalPage = this.totalRecord / this.pageSize; } else { this.totalPage = this.totalRecord / this.pageSize + 1; } }//****此處省略了set和get方法****// }
2、建立Dao層實現類PageDaoImpl.java
@Repository public class PageDaoImpl implements PageDao { @Autowired private JdbcTemplate jdbcTemplate; /* * 獲得總記錄數 */ @SuppressWarnings("deprecation") public int getTotalRecord(String sql, Object... arrayParameters) { int totalRecord = jdbcTemplate.queryForInt(sql, arrayParameters); return totalRecord; } /* * 獲取當前頁資料 */ @SuppressWarnings("unchecked") public Page getPage(int pageNum, Class clazz, String sql, int totalRecord, Object... parameters) { Page page = new Page(pageNum, totalRecord); sql = sql+" limit "+page.getStartIndex()+","+page.getPageSize(); List list=jdbcTemplate.query(sql, parameters, ParameterizedBeanPropertyRowMapper.newInstance(clazz)); page.setList(list); return page; } }
3、在服務層實現類中新增程式碼
public Page getClassifyPage(int pageNum) { String sql = "select count(*) from t_classify"; int totalRecord = pageDao.getTotalRecord(sql); sql = "select * from t_classify"; Page page = pageDao.getPage(pageNum, Classify.class, sql, totalRecord); return page; }
4、在控制層中新增程式碼
@RequestMapping("/list") public String list(HttpServletRequest request) { String pageNum=request.getParameter("p")==null?"1":request.getParameter("p");//獲取頁碼,預設1 request.setAttribute("page", classifyService.getClassifyPage(Integer.valueOf(pageNum))); return "admin/classify/list"; }
5、在jsp頁面中佈局
內容部分:
<c:forEach var="classify" items="${page.list}" varStatus="s"> <tr class="column_${s.count}"> <td class="list-text color999">${classify.name}</td> <td class="list-text color999">${classify.id}</td> </tr> </c:forEach>
分頁按鈕部分:
頁次:${page.currentPage}/${page.totalPage} 每頁${page.pageSize} 總數${page.totalRecord} <a href="<c:url value='/admin/user/list.htm?p=1'/>">首頁</a> <c:choose> <c:when test="${page.currentPage>1}"> <a href="<c:url value='/admin/classify/list.htm?p=${page.currentPage-1}'/>">上一頁</a> </c:when> <c:otherwise> <a href="#">上一頁</a> </c:otherwise> </c:choose> <%-- <c:forEach var="i" begin="1" end="${page.totalPage}"> <a href="<c:url value='/classify.htm?c=${page.list[0].classifyid}&p=${i}'/>">${i}</a> </c:forEach> --%> <c:choose> <c:when test="${page.currentPage<page.totalPage}"> <a href="<c:url value='/admin/classify/list.htm?p=${page.currentPage+1}'/>">下一頁</a> </c:when> <c:otherwise> <a href="#">下一頁</a> </c:otherwise> </c:choose>
轉 :https://www.cnblogs.com/hehaiyang/p/4190295.html