Spring Data 分页和排序 PagingAndSortingRepository的使用(九)

继承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 排前面了

猜你喜欢

转载自blog.csdn.net/weixin_42722953/article/details/89518675