继承PagingAndSortingRepository

我们可以看到,BlogRepository定义了这样一个方法:Page<Blog> findByDeletedFalse(Pageable pageable);,我们主要关注它的参数以及返回值。

  • Pageable 是Spring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息(例如pageNumberpageSize等),这样,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的使用(九)的更多相关文章

  1. 整合Spring Data JPA与Spring MVC: 分页和排序

    之前我们学习了如何使用Jpa访问关系型数据库.比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门. 通过Jpa大大简化了我们对数据库的开发 ...

  2. 整合Spring Data JPA与Spring MVC: 分页和排序pageable

    https://www.tianmaying.com/tutorial/spring-jpa-page-sort Spring Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,我们来学 ...

  3. Spring data JPA先排序再分页。

    //工具类,增删改查等等package com.yunqing.service.impl; import java.util.Map; import org.springframework.beans ...

  4. 转:使用 Spring Data JPA 简化 JPA 开发

    从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...

  5. 了解 Spring Data JPA

    前言 自 JPA 伴随 Java EE 5 发布以来,受到了各大厂商及开源社区的追捧,各种商用的和开源的 JPA 框架如雨后春笋般出现,为开发者提供了丰富的选择.它一改之前 EJB 2.x 中实体 B ...

  6. Spring Data JPA

    转自: http://www.cnblogs.com/WangJinYang/p/4257383.html Spring 框架对 JPA 的支持 Spring 框架对 JPA 提供的支持主要体现在如下 ...

  7. 使用 Spring Data JPA 简化 JPA 开发

    从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...

  8. Spring Data JPA 梳理 - 使用方法

    1.下载需要的包. 需要先 下载Spring Data JPA 的发布包(需要同时下载 Spring Data Commons 和 Spring Data JPA 两个发布包,Commons 是 Sp ...

  9. Spring Data JPA基本了解

    前言 自 JPA 伴随 Java EE 5 发布以来,受到了各大厂商及开源社区的追捧,各种商用的和开源的 JPA 框架如雨后春笋般出现,为开发者提供了丰富的选择.它一改之前 EJB 2.x 中实体 B ...

随机推荐

  1. 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-

    bug: 今天做项目的时候遇到了这样一个崩溃信息: 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumera ...

  2. 解决The markup in the document following the root element must be well-formed.

    出现问题的代码: <security-constraint> <web-resource-collection> <web-resource-name>Regist ...

  3. Django教程:[33]从数据库生成模型

    在使用django做网站的时候,有时候我们的数据库来自一个已有的数据库,如何整合这个数据库呢? django提供了方便的方法来整合已有数据库,下面我们看看具体的方法: 1.先来设置数据库:在网站文件夹 ...

  4. /usr/bin/ld: cannot find -lc错误原因及解决方法

    问题解决 我在执行如下命令的时候,出现了错误. gcc -o main main.c -static -L. –lmylib Linux环境下gcc静态编译/usr/bin/ld: cannot fi ...

  5. go json解析

    JSON转换库为 encoding/json 把对象转换为JSON的方法(函数)为 json.Marshal(),其函数原型如下 func Marshal(v interface{}) ([]byte ...

  6. thinkphp 命名规范

    目录和文件命名 目录和文件名采用 小写+下划线,并且以小写字母开头: 类库.函数文件统一以.php为后缀: 类的文件名均以命名空间定义,并且命名空间的路径和类库文件所在路径一致(包括大小写): 类名和 ...

  7. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

  8. C++ 类中的const关键字

    //类中的const关键字 #include<iostream> using namespace std; class Point{ public: //这个const关键字本质上修饰的是 ...

  9. sql one

    查询的话 子查询什么的都很正常 添加的话 尽量把东西都添加在一个表单里 这是源头 有个这个方便的源头 查询和删除都会方便很多 组建一个网站,不可避免的要进行调试,有些功能需要添加或者删除,对于后台来讲 ...

  10. 游戏开发之coco2dx ---2d 游戏特效

    http://www.cnblogs.com/gamedes/p/4547722.html