Spring Data 分页和排序 PagingAndSortingRepository的使用(九)
继承PagingAndSortingRepository
我们可以看到,BlogRepository定义了这样一个方法:Page<Blog> findByDeletedFalse(Pageable pageable);,我们主要关注它的参数以及返回值。
- Pageable 是Spring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息(例如
pageNumber、pageSize等),这样,Jpa就能够通过pageable参数来得到一个带分页信息的Sql语句。 - Page类也是Spring Data提供的一个接口,该接口表示一部分数据的集合以及其相关的下一部分数据、数据总数等相关信息,通过该接口,我们可以得到数据的总体信息(数据总数、总页数...)以及当前数据的信息(当前数据的集合、当前页数等)
Spring Data Jpa除了会通过命名规范帮助我们扩展Sql语句外,还会帮助我们处理类型为Pageable的参数,将pageable参数转换成为sql'语句中的条件,同时,还会帮助我们处理类型为Page的返回值,当发现返回值类型为Page,Spring Data Jpa将会把数据的整体信息、当前数据的信息,分页的信息都放入到返回值中。这样,我们就能够方便的进行个性化的分页查询。
分页:
package org.springdata.repository;
import org.springdata.domain.Employee;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
*/
public interface EmployeePadingAndSortingResponstory extends PagingAndSortingRepository<Employee,Integer> {
}
编写测试类
package org.springdata.crudservice;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springdata.domain.Employee;
import org.springdata.repository.EmployeePadingAndSortingResponstory;
import org.springdata.service.CrudEmployeeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import java.util.List;
/**
*/
public class PagingAndSortingRespositoryTest {
private ApplicationContext ctx = null;
private EmployeePadingAndSortingResponstory employeePadingAndSortingResponstory = null;
@Before
public void setup(){
ctx = new ClassPathXmlApplicationContext("beans_news.xml");
employeePadingAndSortingResponstory = ctx.getBean(EmployeePadingAndSortingResponstory.class);
System.out.println("setup");
}
@After
public void tearDown(){
ctx = null;
System.out.println("tearDown");
}
@Test
public void testPage(){
//index 1 从0开始 不是从1开始的
Pageable page = new PageRequest(0,10);
Page<Employee> employeeList = employeePadingAndSortingResponstory.findAll(page);
System.out.println("查询总页数:"+employeeList.getTotalPages());
System.out.println("查询总记录数:"+employeeList.getTotalElements());
System.out.println("查询当前第几页:"+employeeList.getNumber()+1);
System.out.println("查询当前页面的集合:"+employeeList.getContent());
System.out.println("查询当前页面的记录数:"+employeeList.getNumberOfElements());
}
}
查询结果 咱们先在Employee 实体类 重写下toString()方法 才能打印集合数据

排序:
基于上面的查询集合 我们新建一个测试方法
@Test
public void testPageAndSord(){
//根据id 进行降序
Sort.Order order = new Sort.Order(Sort.Direction.DESC,"id");
Sort sort = new Sort(order);
//index 1 从0开始 不是从1开始的
Pageable page = new PageRequest(0,10,sort);
Page<Employee> employeeList = employeePadingAndSortingResponstory.findAll(page);
System.out.println("查询总页数:"+employeeList.getTotalPages());
System.out.println("查询总记录数:"+employeeList.getTotalElements());
System.out.println("查询当前第几页:"+employeeList.getNumber()+1);
System.out.println("查询当前页面的集合:"+employeeList.getContent());
System.out.println("查询当前页面的记录数:"+employeeList.getNumberOfElements());
}
我们可以看到 最大id 排前面了
Spring Data 分页和排序 PagingAndSortingRepository的使用(九)的更多相关文章
- 整合Spring Data JPA与Spring MVC: 分页和排序
之前我们学习了如何使用Jpa访问关系型数据库.比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门. 通过Jpa大大简化了我们对数据库的开发 ...
- 整合Spring Data JPA与Spring MVC: 分页和排序pageable
https://www.tianmaying.com/tutorial/spring-jpa-page-sort Spring Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,我们来学 ...
- Spring data JPA先排序再分页。
//工具类,增删改查等等package com.yunqing.service.impl; import java.util.Map; import org.springframework.beans ...
- 转:使用 Spring Data JPA 简化 JPA 开发
从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...
- 了解 Spring Data JPA
前言 自 JPA 伴随 Java EE 5 发布以来,受到了各大厂商及开源社区的追捧,各种商用的和开源的 JPA 框架如雨后春笋般出现,为开发者提供了丰富的选择.它一改之前 EJB 2.x 中实体 B ...
- Spring Data JPA
转自: http://www.cnblogs.com/WangJinYang/p/4257383.html Spring 框架对 JPA 的支持 Spring 框架对 JPA 提供的支持主要体现在如下 ...
- 使用 Spring Data JPA 简化 JPA 开发
从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...
- Spring Data JPA 梳理 - 使用方法
1.下载需要的包. 需要先 下载Spring Data JPA 的发布包(需要同时下载 Spring Data Commons 和 Spring Data JPA 两个发布包,Commons 是 Sp ...
- Spring Data JPA基本了解
前言 自 JPA 伴随 Java EE 5 发布以来,受到了各大厂商及开源社区的追捧,各种商用的和开源的 JPA 框架如雨后春笋般出现,为开发者提供了丰富的选择.它一改之前 EJB 2.x 中实体 B ...
随机推荐
- forward declaration of class 错误
在使用Qt的时候遇到这个错误,查了一下发现,是因为我没有正确的使用前置声明. #ifndef FIRSTPAGE_H #define FIRSTPAGE_H #include "ui_dia ...
- Windows Phone 提升开发效率(一)使用d:DataContext添加设计时Binding
[问题的提出] 在开发过程中我们经常会遇到将UI同学提供的效果图转化成实际的页面,而在这过程中,多数时候Blend等设计工具默认情况下并不能提供很好的可视化支持. 举个简单的例子来说下吧: ...
- 我们通过一个服务器程序,以研究backlog参数对listen系统调用的影响,运行截图如下
启动服务器程序,服务器程序正在等待客户端的连接 我们使用一次telnet命令就建立一个连接 打开多个终端窗口执行下列命名 #telnet 192.168.255.128 4444多次执行 然后我们执行 ...
- fontDialog-字体对话框和colorDialog-颜色对话框
private void button1_Click(object sender, EventArgs e) { DialogResult dr = fontDialog1.ShowDialog(); ...
- C语言中对输入输出格式的控制
格式化输出的控制 #include<stdio.h> int main(void){ float a=111123.681111f; printf("%1.3f",a) ...
- LRU算法 - LRU Cache
这个是比较经典的LRU(Least recently used,最近最少使用)算法,算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. 一般应 ...
- js 复制 功能
<html> <head> <title>Zero Clipboard Test</title> <meta charset="utf- ...
- 关于Nvelocity的主要语法和一些代码示例
context.Response.ContentType = "text/html"; VelocityEngine vltEngine = new VelocityEngine( ...
- MapReduce 中的两表 join 几种方案简介
转自:http://my.oschina.net/leejun2005/blog/95186 MapSideJoin例子:http://my.oschina.net/leejun2005/blog/1 ...
- malloc 函数本身并不识别要申请的内存是什么类型
malloc 函数本身并不识别要申请的内存是什么类型,它只关心内存的总字节数.我 们通常记不住 int, float 等数据类型的变量的确切字节数. 例如 int 变量在 16 位系统 下是 2 个字 ...