天天看點

sping boot 2.0 + pageHelper(分頁) +thymleaf(頁面展示) + Restful API 分頁

一、 POM導入PageHelper

sping boot 2.0 + pageHelper(分頁) +thymleaf(頁面展示) + Restful API 分頁

二、 Restful API 分頁 的Controller

1) 直接編寫Controller

@RequestMapping("/DeptDomainAPIPaging")
	    @ResponseBody
	    public List<DeptDomain> DeptDomainAPIPaging(int currentPage,int pageSize){
	        return deptService.findItemByPage(currentPage, pageSize);
	    }
           
  1. POSTMAN 直接通路 http://localhost:8080/test/DeptDomainAPIPaging?currentPage=2&pageSize=10 即可獲得分頁json。

三、通過 thymleaf 實作分頁 的Controller

1)Controller代碼

@GetMapping("/WebDeptDomainPaging")
public ModelAndView WebDeptDomainPaging(
         @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
         @RequestParam(defaultValue="5",value="pageSize")Integer pageSize, ModelAndView view){

    //為了程式的嚴謹性,判斷非空:
    if(pageNum == null){
        pageNum = 1;   //設定預設目前頁
    }
    if(pageNum <= 0){
        pageNum = 1;
    }
    if(pageSize == null){
        pageSize = 5;    //設定預設每頁顯示的資料數
    }
    System.out.println("目前頁是:"+pageNum+"顯示條數是:"+pageSize);

    //1.引入分頁插件,pageNum是第幾頁,pageSize是每頁顯示多少條,預設查詢總數count
    PageHelper.startPage(pageNum,pageSize);
    //2.緊跟的查詢就是一個分頁查詢-必須緊跟.後面的其他查詢不會被分頁,除非再次調用PageHelper.startPage
    try {
        List<DeptDomain> deptDomains = deptService.selectAll();
        System.out.println("分頁資料:"+deptDomains);
        //3.使用PageInfo包裝查詢後的結果,5是連續顯示的條數,結果list類型是Page<E>
        PageInfo<DeptDomain> pageInfo = new PageInfo<DeptDomain>(deptDomains,pageSize);
        //4.使用model/map/modelandview等帶回前端
        view.addObject("pageInfo", pageInfo);
        view.setViewName("/test/TstShowByPage.html");

        System.out.println("pageInfo.getPageSize()>>>" + pageInfo.getPageSize());
        System.out.println("pageInfo.isHasPreviousPage()>>>" + pageInfo.isHasPreviousPage());

    }finally {
        PageHelper.clearPage(); //清理 ThreadLocal 存儲的分頁參數,保證線程安全
    }
    //5.設定傳回的jsp/html等前端頁面

    return view;
}
           
  1. thymleaf渲染代碼: 由于pageInfo.getList() 可以直接傳回分頁的内容,故直接通過這個方法擷取。注意:由于CSDN的這個編輯器無法顯示全部代碼,故請從以下連結資源中下載下傳完整l的htm代碼。

    https://download.csdn.net/download/snetlogon20/11826716

    <table class="table table-hover"  width="200px;" style="test-align: center;">
        <caption>部門表格</caption>
        <thead>
        <tr>
            <th width="30%">部門号</th>
            <th>部門名稱</th>
            <th>部門描述</th>
            <th>部門電話</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="deptDomain : ${pageInfo.getList()}">
            <td th:text="${deptDomain.deptId}">0</td>
            <td th:text="${deptDomain.deptName}">0</td>
            <td th:text="${deptDomain.desp}">0</td>
            <td th:text="${deptDomain.phone}">0</td>
        </tr>
        </tbody>
    </table>
    
    <!--顯示分頁資訊-->
    <div class="modal-footer no-margin-top">
        <div class="col-md-6">
            目前第 [[${pageInfo.pageNum}]]頁,共 [[${pageInfo.pages}]] 頁.一共 [[${pageInfo.total}]] 條記錄
        </div>
    
        <ul class="pagination pull-right no-margin">
            <li th:if="${pageInfo.hasPreviousPage}">
                <a th:href="'/test/DeptDomainPaging?pageNum=1'" target="_blank" rel="external nofollow" >首頁</a>
            </li>
    
            <li class="prev" th:if="${pageInfo.hasPreviousPage}">
                <a th:href="'/test/usermanage?pageNum='+${pageInfo.prePage}" target="_blank" rel="external nofollow" >
                    <i class="fa fa-leaf"><<</i>
                </a>
            </li>
            <!--周遊條數-->
            <li th:each="nav:${pageInfo.navigatepageNums}">
                <a th:href="'/test/DeptDomainPaging?pageNum='+${nav}" target="_blank" rel="external nofollow"  th:text="${nav}" th:if="${nav != pageInfo.pageNum}"></a>
                <span style="font-weight: bold;background: #6faed9;" th:if="${nav == pageInfo.pageNum}" th:text="${nav}" ></span>
            </li>
    
            <li class="next" th:if="${pageInfo.hasNextPage}">
                <a th:href="'/test/DeptDomainPaging?pageNum='+${pageInfo.nextPage}" target="_blank" rel="external nofollow" >
                    <i class="fa fa-leaf">>></i>
                </a>
            </li>
    
            <li>
                <a th:href="'/test/DeptDomainPaging?pageNum='+${pageInfo.pages}+'&pageSize=5'" target="_blank" rel="external nofollow" >尾頁</a>
            </li>
        </ul>
    </div>
    
    <div>目前頁号:<span th:text="${pageInfo.pageNum}"></span></div>
    <div>每頁條數:<span th:text="${pageInfo.pageSize}"></span></div>
    <div>起始行号:<span th:text="${pageInfo.startRow}"></span></div>
    <div>終止行号:<span th:text="${pageInfo.endRow}"></span></div>
    <div>總結果數:<span th:text="${pageInfo.total}"></span></div>
    <div>總頁數:<span th:text="${pageInfo.pages}"></span></div>
    <hr />
    <div>是否為第一頁:<span th:text="${pageInfo.isFirstPage}"></span></div>
    <div>是否為最後一頁:<span th:text="${pageInfo.isLastPage}"></span></div>
    <div>是否有前一頁:<span th:text="${pageInfo.hasPreviousPage}"></span></div>
    <div>是否有下一頁:<span th:text="${pageInfo.hasNextPage}"></span></div>
               

繼續閱讀