springboot+jpa分页(Pageable+Page)

Pageable+Page实现分页无需配置,也不需要加入jar包(maven依赖)

Controller控制层

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 Sort(Sort.Direction.DESC, "recordNo");  // 这里的"recordNO"是实体类的主键,记住一定要是实体类的字段,而不能是数据库的字段
        Pageable pageable = new PageRequest(pageNum - 1, 6, sort);
        Page<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");  // 允许iframe
        return "record_list";
    }
}

html页面

      <table id = "table" class="table table-hover text-center">
              <tr>
                <th>晚归记录编号</th>
                <th>宿舍楼编号</th>
                <th>宿舍号</th>
                <th>学号</th>
                <th>班级</th>
                <th>辅导员</th>
                <th>晚归时间</th>
                <th>晚归原因</th>
                <th>操作</th>
              </tr>
          
     <!--  pageInfo.getContent() 返回的是一个list      -->
                <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>页
                 共<span th:text="${pageInfo.totalElements}"></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>

 

实现效果:

 

 

转载于:https://www.cnblogs.com/chuangqi/p/11261482.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值