springboot + jpa pagination plugin (Pageable + Page)

Pageable + Page tab need not be configured to achieve, does not require added to the jar package (Maven dependent)

Controller control layer

package com.gxuwz.late.controller;

import com.gxuwz.late.bean.Record;
import com.gxuwz.late.repository.RecordRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    static Logger logger = LoggerFactory.getLogger(ManagerController.class);

    @Autowired
    RecordRepository recordRepository;

    @RequestMapping("/list")
    public String list(HttpServletResponse response, Model model, Integer pageNum){
                       
        if (pageNum == null){
            pageNum = 1;
        }
        Sort sort= New new the Sort (Sort.Direction.DESC, "recordNo" ); // where "recordNO" is the primary key of the entity class, the entity class must remember if the field is not a field in the database 
        the Pageable Pageable = new new PageRequest (pageNum -. 1,. 6 , Sort); 
        Page <the Record> List = recordRepository.findAll (Pageable); 

        logger.info ( "pageNum ==" + pageNum); 

        model.addAttribute ( "PageInfo" , List); 

        logger.info ( " = PageInfo> "+ list.getContent () GET (0. ) .getClassName ()); 
        Response.AddHeader ( " X-Frame-Options "," SAMEORIGIN ");  // allow iframes. 
        Return "record_list" ;
    }
}

html page

< Table ID = "dormitory_table" class = "hover-text Table Table-Center" > 
              < TR > 
                < TH > coming home record number </ TH > 
                < TH > dormitory ID </ TH > 
                < TH > quarters No. </ TH > 
                < TH > Student ID </ TH > 
                < TH > class </ TH > 
                < TH > facilitator </th>
                <th>晚归时间</th>
                <th>晚归原因</th>
                <th>操作</th>
              </tr>
                <tr th:each="record:${pageInfo.getContent()}">
                    <td th:text="${record.recordNo }"></td>
                    <td th:text="${record.buildingNo }"></td>
                    <td th:text="${record.dorId }"></td>
                    <td th:text="${record.studentNo }"></td>
                    <td th:text="${record.className }"></td>
                    <td th:text="${record.instName }"></td>
                    <td th:text="${record.time }"></td>
                    <td th:text="${record.reason }"></td>
                </tr>
                  <tr>
                    <td colspan="8">
                        <div class="pagelist">
                            <p>当前<span th:text="${pageInfo.getNumber()} + 1"></span>页,总<span th:text="${pageInfo.totalPages}"></span>
                            <a th:href="@{/manager/list}">首页</a>
                            <a th:href="@{/manager/list(pageNum = ${pageInfo.hasPrevious()} ? ${pageInfo.getNumber() } : 1)}">上一页</a>
                            <a th:href="@{/manager/list(pageNum = ${pageInfo.hasNext()} ? ${pageInfo.getNumber()} + 2 : ${pageInfo.totalPages})}">下一页</a>
                            <a th:href="@{/manager/list(pageNum = ${pageInfo.totalPages})}">尾页</a></p>
                        </div>

                    </td>
              </tr>
            </table>

Achieve results:

jpa prone to problems with pagehelper words: If the pageSize invalid, paging invalid

Guess you like

Origin www.cnblogs.com/chuangqi/p/11261482.html