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

Controller控制层

  1. package com.gxuwz.late.controller;
  2.  
  3. import com.gxuwz.late.bean.Record;
  4. import com.gxuwz.late.repository.RecordRepository;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.data.domain.PageRequest;
  10. import org.springframework.data.domain.Pageable;
  11. import org.springframework.data.domain.Sort;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15.  
  16. import javax.servlet.http.HttpServletResponse;
  17.  
  18. @Controller
  19. @RequestMapping("/manager")
  20. public class ManagerController {
  21. static Logger logger = LoggerFactory.getLogger(ManagerController.class);
  22.  
  23. @Autowired
  24. RecordRepository recordRepository;
  25.  
  26. @RequestMapping("/list")
  27. public String list(HttpServletResponse response, Model model, Integer pageNum){
  28.  
  29. if (pageNum == null){
  30. pageNum = 1;
  31. }
         // 排序方式,这里是以“recordNo”为标准进行降序
  32. Sort sort = new Sort(Sort.Direction.DESC, "recordNo"); // 这里的"recordNo"是实体类的主键,记住一定要是实体类的属性,而不能是数据库的字段
  33. Pageable pageable = new PageRequest(pageNum - 1, 6, sort); // (当前页, 每页记录数, 排序方式)
  34. Page<Record> list = recordRepository.findAll(pageable);
  35.  
  36. logger.info("pageNum==" + pageNum);
  37.  
  38. model.addAttribute("pageInfo", list);
  39.  
  40. response.addHeader("x-frame-options","SAMEORIGIN"); // 允许iframe
  41. return "record_list";
  42. }
  43. }

html页面

  1.       <table id = "table" class="table table-hover text-center">
  2. <tr>
  3. <th>晚归记录编号</th>
  4. <th>宿舍楼编号</th>
  5. <th>宿舍号</th>
  6. <th>学号</th>
  7. <th>班级</th>
  8. <th>辅导员</th>
  9. <th>晚归时间</th>
  10. <th>晚归原因</th>
  11. <th>操作</th>
  12. </tr>
  13.           
  14.      <!-- pageInfo.getContent() 返回的是一个list -->
  15. <tr th:each="record:${pageInfo.getContent()}">
  16. <td th:text="${record.recordNo }"></td>
  17. <td th:text="${record.buildingNo }"></td>
  18. <td th:text="${record.dorId }"></td>
  19. <td th:text="${record.studentNo }"></td>
  20. <td th:text="${record.className }"></td>
  21. <td th:text="${record.instName }"></td>
  22. <td th:text="${record.time }"></td>
  23. <td th:text="${record.reason }"></td>
  24. </tr>
  25. <tr>
  26. <td colspan="8">
  27. <div class="pagelist">
  28. <p>当前<span th:text="${pageInfo.getNumber()} + 1"></span>页,总<span th:text="${pageInfo.totalPages}"></span>
  29.                  共<span th:text="${pageInfo.totalElements}"></span>条记录
  30. <a th:href="@{/manager/list}">首页</a>
  31. <a th:href="@{/manager/list(pageNum = ${pageInfo.hasPrevious()} ? ${pageInfo.getNumber() } : 1)}">上一页</a>
  32. <a th:href="@{/manager/list(pageNum = ${pageInfo.hasNext()} ? ${pageInfo.getNumber()} + 2 : ${pageInfo.totalPages})}">下一页</a>
  33. <a th:href="@{/manager/list(pageNum = ${pageInfo.totalPages})}">尾页</a></p>
  34. </div>
  35.  
  36. </td>
  37. </tr>
  38. </table>

实现效果:

springboot+jpa分页(Pageable+Page)的更多相关文章

  1. SpringBoot Jpa 分页查询最新配置方式

    这是已经被废弃的接口 Sort sort = new Sort(Sort.Direction.DESC,"bean类中字段"); //创建时间降序排序 Pageable pagea ...

  2. SpringBoot JPA + 分页 + 单元测试SpringBoot JPA条件查询

    application.properties 新增数据库链接必须的参数 spring.jpa.properties.hibernate.hbm2ddl.auto=update 表示会自动更新表结构,所 ...

  3. springBoot jpa 分页

    1.jap中有自带的分页方法 在dao层中使用 Page<LinkUrl> findAll(Pageable pageable); 2.在controller层 public List&l ...

  4. SpringBoot JPA实现增删改查、分页、排序、事务操作等功能

    今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查.分页.排序.事务操作等功能.下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and ...

  5. SpringBoot JPA + H2增删改查示例

    下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...

  6. SpringBoot JPA 专题

    Error: Error starting ApplicationContext. To display the auto-configuration report re-run your appli ...

  7. SpringBoot Jpa入门案例

    版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons) 我们先来了解一下是什么是springboot jpa,springboo ...

  8. 补习系列(19)-springboot JPA + PostGreSQL

    目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...

  9. spring data jpa 分页查询

    https://www.cnblogs.com/hdwang/p/7843405.html spring data jpa 分页查询   法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特 ...

随机推荐

  1. jmeter的运行原理和测试计划要素

    jmeter运行原理 1.jmeter运行在JVM虚拟机上,jmeter是以线程的方式运行的. 2.jmeter通过线程组来驱动多个线程,运行测试脚本对被测试服务器发起负载,每一个负载机上够可以运行多 ...

  2. js表格拖拽

    html部分 <div id="chenkbox"> <div id="tableSort"> <ol> <li> ...

  3. pycharm 永久注册

    pycharm 使用又到期了,找到了破解版亲测(到期日期2099/12/31),绝对简单好用,直接使用步骤: 一,下载pycharm(windows版):  https://www.jetbrains ...

  4. python 解释器编码

  5. Libevent:0异步IO简介

    一:异步IO简介 大多数的初级编程者都是从阻塞IO调用开始网络编程的.阻塞(同步)IO调用指的是:调用会一直阻塞,不会返回,直到发生下面两种情况之一.要么操作完成,要么经历相当长的时间,网络协议栈自己 ...

  6. 前端基础☞CSS

    css的四种引入方式 1.行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用. <p style="background-color: ...

  7. selenium 自动化点击页面

    #!/usr/bin/env python# -*- coding:utf-8 -*-from selenium import webdriverfrom selenium.webdriver.com ...

  8. python-----堡垒机前戏paramiko模块及进阶

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...

  9. @atcoder - ARC066F@ Contest with Drinks Hard

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定序列 T1, T2, ... TN,你可以从中选择一些 Ti ...

  10. 宝塔linux

    宝塔linux linux 定时任务管理