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查询,注意表名啥的都用数据库中的名称,适用于特 ...
随机推荐
- jmeter的运行原理和测试计划要素
jmeter运行原理 1.jmeter运行在JVM虚拟机上,jmeter是以线程的方式运行的. 2.jmeter通过线程组来驱动多个线程,运行测试脚本对被测试服务器发起负载,每一个负载机上够可以运行多 ...
- js表格拖拽
html部分 <div id="chenkbox"> <div id="tableSort"> <ol> <li> ...
- pycharm 永久注册
pycharm 使用又到期了,找到了破解版亲测(到期日期2099/12/31),绝对简单好用,直接使用步骤: 一,下载pycharm(windows版): https://www.jetbrains ...
- python 解释器编码
- Libevent:0异步IO简介
一:异步IO简介 大多数的初级编程者都是从阻塞IO调用开始网络编程的.阻塞(同步)IO调用指的是:调用会一直阻塞,不会返回,直到发生下面两种情况之一.要么操作完成,要么经历相当长的时间,网络协议栈自己 ...
- 前端基础☞CSS
css的四种引入方式 1.行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用. <p style="background-color: ...
- selenium 自动化点击页面
#!/usr/bin/env python# -*- coding:utf-8 -*-from selenium import webdriverfrom selenium.webdriver.com ...
- python-----堡垒机前戏paramiko模块及进阶
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...
- @atcoder - ARC066F@ Contest with Drinks Hard
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定序列 T1, T2, ... TN,你可以从中选择一些 Ti ...
- 宝塔linux
宝塔linux linux 定时任务管理