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. Java安全——密钥那些事

    标签(空格分隔): Java 安全 概念 密钥是加密算法不可缺少的部分.密钥在安全体系中至关重要,正如其名,私密的钥匙,打开安全的大门.密钥分两种:对称密钥和非对称密钥.非对称密钥里又包含公开密钥和私 ...

  2. @codeforces - 1161F@ Zigzag Game

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 2n 个结点的完全二分图,1~n 在左边,n+1~2n ...

  3. aspcms安装所遇到的问题

     aspcms标签:http://biaoqian.iasp.com.cn/ 1.报错:An error occurred on the server when processing the URL. ...

  4. Ubuntu修改root密码,ssh 允许root用户登录

    1,切换为root用户 2,passwd root(or others) 3,输两次密码 4,重启. ssh允许root用户登录: 1,vim /etc/ssh/sshd_config 2,修改Per ...

  5. 巨蟒python全栈开发-第11阶段 ansible3_3入门playbook剧本

    1.playbook剧本 2.playbook传参 3.setup模块介绍 4.playbook的tags 5.playbook的handlers&&templates模块 6.条件和 ...

  6. CAD专用卸载修复工具,一键完全彻底卸载删除CAD软件的专用卸载工具

    Autodesk CAD卸载工具(AUTO Uninstaller)是专门为了针对Autodesk CAD软件卸载不干净而导致CAD安装失败问题进行研发的Autodesk CAD一键卸载工具.现在虽然 ...

  7. iOS打包上传ipa文件时,报错<ERROR ITMS-90096: "Your binary is not optimized for iPhone 5 - New iPhone apps......>的解决方案

    很长一段时间习惯了用企业级证书发布,最近的新项目使用Xcode 9.1发布到AppStore时遇到了一个小问题(emm..其实问题跟Xcode版本没关系,我也不知道为什么要声明这个233),如下: E ...

  8. Java练习 SDUT-1253_进制转换

    进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制数N,将它转换成R进制数输出. Input 输入 ...

  9. Python基础:13装饰器

    装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的应用有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同 ...

  10. 树状数组(Binary Index Tree)

    一维BIT(单点更新,区间求和): Problem - 1166 #include <iostream> #include <algorithm> #include <c ...