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;
}
     // 排序方式,这里是以“recordNo”为标准进行降序
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); 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>

实现效果:

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. LintCode_420 报数

    题目 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> 11. ...

  2. 【JZOJ3887】【长郡NOIP2014模拟10.22】字符串查询

    haf 给定n个字符串和q个询问 每次询问在这n个字符串中,有多少个字符串同时满足 1. 字符串a是它的前缀 2. 字符串b是它的后缀 100%数据满足n,q≤50000,字符串长度丌超过100,任意 ...

  3. 大数据技术之Hive

    第1章 Hive入门 1.1 什么是Hive Hive:由Facebook开源用于解决海量结构化日志的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提 ...

  4. linux下安装composer以及使用composer安装laravel

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/nianzhi1202/article/details/72770099 一.安装composer之前 ...

  5. 域名拆分 tld

    概念 URL Universal Resource Locator ,统一资源定位符. 用处:用来标识互联网资源的唯一地址. 本质:提供了互联网上任一资源地址的通用表示方法. protocol://h ...

  6. sql —— having

    在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用.HAVING 子句可以让我们筛选分组后的各组数据. 原表: 我们可以对上面数据根据性别这个字段进行分组查询,分别 ...

  7. mysql带有子查询的like查询

    SELECT * FROM by_app_categories WHERE c_name LIKE CONCAT('%', (SELECT `name` FROM b_catelist WHERE t ...

  8. QT开发ROS遇到问题:execute_process(/usr/bin/python"/home/fu/catkin_ws/build/catkin_genetated/generate_cached_setup.py)..........

    具体如上图显示,网上给的方法是重装ros和重新创建ROS工作空间.经过这样做以后发现问题依然存在. 最后考虑可能是没有加载ROS的环境变量.(我的qtcreator中已经有了创建ros工程的项目,但是 ...

  9. @codeforces - 1221G@ Graph And Numbers

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的无向图. 现在要求给每个点写上 0 或 ...

  10. python 检测文件夹的数据变动

    from watchdog.observers import Observerfrom watchdog.events import *import time class FileEventHandl ...