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;
}
// 排序方式,这里是以“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)的更多相关文章
- SpringBoot Jpa 分页查询最新配置方式
这是已经被废弃的接口 Sort sort = new Sort(Sort.Direction.DESC,"bean类中字段"); //创建时间降序排序 Pageable pagea ...
- SpringBoot JPA + 分页 + 单元测试SpringBoot JPA条件查询
application.properties 新增数据库链接必须的参数 spring.jpa.properties.hibernate.hbm2ddl.auto=update 表示会自动更新表结构,所 ...
- springBoot jpa 分页
1.jap中有自带的分页方法 在dao层中使用 Page<LinkUrl> findAll(Pageable pageable); 2.在controller层 public List&l ...
- SpringBoot JPA实现增删改查、分页、排序、事务操作等功能
今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查.分页.排序.事务操作等功能.下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and ...
- SpringBoot JPA + H2增删改查示例
下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...
- SpringBoot JPA 专题
Error: Error starting ApplicationContext. To display the auto-configuration report re-run your appli ...
- SpringBoot Jpa入门案例
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons) 我们先来了解一下是什么是springboot jpa,springboo ...
- 补习系列(19)-springboot JPA + PostGreSQL
目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...
- spring data jpa 分页查询
https://www.cnblogs.com/hdwang/p/7843405.html spring data jpa 分页查询 法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特 ...
随机推荐
- Spring_自动组件扫描和 基于注解配置bean
自动组件扫描 启用Spring组件扫描功能. 使用@Component注释来表示这是类是一个自动扫描组件. package com.tanlei.dao; import org.springfram ...
- Java安全——密钥那些事
标签(空格分隔): Java 安全 概念 密钥是加密算法不可缺少的部分.密钥在安全体系中至关重要,正如其名,私密的钥匙,打开安全的大门.密钥分两种:对称密钥和非对称密钥.非对称密钥里又包含公开密钥和私 ...
- Spring boot通过JPA访问MySQL数据库
本文展示如何通过JPA访问MySQL数据库. JPA全称Java Persistence API,即Java持久化API,它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据 ...
- day39-Spring 01-上次课内容回顾
- cmakelists.txt中配置openg环境出现: undefined reference to symbol 'glLightfv'
cmakelists.txt中配置openg环境出现: undefined reference to symbol 'glLightfv' 解决方法: 在cmakelists.txt添加 find_p ...
- poj3532 Round Numbers
题意:给出l.r,求区间[l,r]内二进制中0的个数大于等于1的个数的数字有多少个. 简单的数位dp. //Serene #include<algorithm> #include< ...
- Android实战:手把手实现“捧腹网”APP(一)-----捧腹网网页分析、数据获取
Android实战:手把手实现"捧腹网"APP(一)-–捧腹网网页分析.数据获取 Android实战:手把手实现"捧腹网"APP(二)-–捧腹APP原型设计.实 ...
- 【JZOJ4848】【GDOI2017模拟11.3】永恒的契约
题目描述 宅邸迅速的燃烧着,必须带贝蒂走出禁书库!凭着感觉,又一次直接找到禁书库的门. "你,是那个人嘛?"400年了,当初圣域建立结界时没有进入圣域,被伤了心的人工精灵贝蒂,与强 ...
- Python学习之路2☞数据类型与变量
变量 变量作用:保存状态:说白了,程序运行的状态就是状态的变化,变量是用来保存状态的,变量值的不断变化就产生了运行程序的最终输出结果 一:声明变量 #!/usr/bin/env python # -* ...
- MyBatis动态SQL(一)
MyBatis 的强大特性之一便是它的动态 SQL.动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似.在 MyBatis 之前的版本中,有很多元素需要花时间了解.MyBatis 3 ...