Spring Data JPA(2)--利用PagingAndSortingRespository实现分页和排序
2017-12-22 15:54
671 查看
通过查看PagingAndSortingRepository接口源码,我们可以看到有两个方法来实现功能:
1.Student实体类
2.创建一个接口,继承PagingAndSortingRepository
我们回头看看实现的过程:
Pageable接口的实现方法,通过源码发现,它有两个子类PageRequest和QPageRequest,我们选用常用的PageRequest来实现它.
PageRequest继承了AbstractPageRequest类,它提供了
c224
一系列的方法,常用的有:
Sort类源码:
/** * Returns all entities sorted by the given options. * * @param sort * @return all entities sorted by the given options */ Iterable<T> findAll(Sort sort);//升序或者降序的查询所有 /** * Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object. * * @param pageable * @return a page of entities */ Page<T> findAll(Pageable pageable);//分页查询,传入Pageable对象,返回一个Page对象下面通过实例来说明如何实现分页和排序的功能,并了解Sort对象,Pageable对象,Page对象如何声明实现.
1.Student实体类
@Entity public class Student { private int sno; private String sname; private int sage; @Override public String toString() { return "Student [sno=" + sno + ", sname=" + sname + ", sage=" + sage + "]"; } @GeneratedValue @Id public int getSno() { return sno; }getter,setter...
2.创建一个接口,继承PagingAndSortingRepository
public interface StudentPagingAndSortingRepository extends PagingAndSortingRepository<Student, Integer>{ }3.测试类进行单元测试,代码
public class StudentPagingAndSortingRepositoryTest { private ApplicationContext ctx = null; private StudentPagingAndSortingRepository studentPagingAndSortingRepository = null; @Before public void setUp(){ ctx = new ClassPathXmlApplicationContext("beans.xml"); studentPagingAndSortingRepository = ctx.getBean(StudentPagingAndSortingRepository.class); System.out.println("setup"); } @After public void tearDown(){ ctx = null; System.out.println("teardown"); } //PagingAndSortingRepository //分页 @Test public void testPage(){ System.out.println("start"); //PageRequest(page,size) page从index0开始 Pageable pageable = new PageRequest(0,2); Page<Student> page = studentPagingAndSortingRepository.findAll(pageable); System.out.println("查询的总页数:"+page.getTotalPages()); System.out.println("查询的记录数:"+page.getTotalElements()); System.out.println("查询当前的第几页:"+(page.getNumber()+1)); System.out.println("查询当前页面的集合:"+page.getContent()); System.out.println("查询当前页面的记录数:"+page.getNumberOfElements()); System.out.println("started"); } //分页加排序 @Test public void testPageAndSort(){ System.out.println("start"); Order order = new Order(Direction.ASC,"sage"); Sort sort = new Sort(order); Pageable pageable = new PageRequest(0,2,sort); Page<Student> page = studentPagingAndSortingRepository.findAll(pageable); System.out.println("查询的总页数:"+page.getTotalPages()); System.out.println("查询的记录数:"+page.getTotalElements()); System.out.println("查询当前的第几页:"+(page.getNumber()+1)); System.out.println("查询当前页面的集合:"+page.getContent()); System.out.println("查询当前页面的记录数:"+page.getNumberOfElements()); System.out.println("started"); } }这样就实现了分页与排序的功能.
我们回头看看实现的过程:
Pageable接口的实现方法,通过源码发现,它有两个子类PageRequest和QPageRequest,我们选用常用的PageRequest来实现它.
PageRequest继承了AbstractPageRequest类,它提供了
c224
一系列的方法,常用的有:
/** * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return the first * page. * * @param page zero-based page index. * @param size the size of the page to be returned. */ public PageRequest(int page, int size) { this(page, size, null);//从哪一页开始(从0开始),页面容量 } /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param direction the direction of the {@link Sort} to be specified, can be {@literal null}. * @param properties the properties to sort by, must not be {@literal null} or empty. */ public PageRequest(int page, int size, Direction direction, String... properties) { this(page, size, new Sort(direction, properties));//开始页面,页面容量,排序方向,按照什么字段排序 } /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param sort can be {@literal null}. */ public PageRequest(int page, int size, Sort sort) { super(page, size); this.sort = sort;//开始页面,页面容量,排序方向 } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#getSort() */ public Sort getSort() { return sort; } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#next() */ public Pageable next() { return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());//下一页 } /* * (non-Javadoc) * @see org.springframework.data.domain.AbstractPageRequest#previous() */ public PageRequest previous() { return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());//是否存在页面 } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#first() */ public Pageable first() { return new PageRequest(0, getPageSize(), getSort());//第一页 }
Sort类源码:
/** * Creates a new {@link Sort} instance using the given {@link Order}s. * * @param orders must not be {@literal null}. */ public Sort(Order... orders) { this(Arrays.asList(orders));//Sort的构建需要先实例化Order对象,Order order = new Order(Direction.ASC,"sage");ASC升序,DESC降序 }
相关文章推荐
- Spring boot data JPA 自定义JPQL语句,以及PagingAndSortingRepository接口实现分页查询
- 结合SpringDataJPA中的PagingAndSortingRepository和 JpaSpecificationExecutor接口实现大数据量的数据分页查询
- SpringData JPA实现CRUD,分页与多参数排序
- 【SpringData】轻松愉快之玩转SpringData( 第5章 CrudRepository PagingAndSortingRespsitory JpaRepository接口)
- SpringBoot第二讲利用Spring Data JPA实现数据库的访问(二)_分页和JpaSpecificationExecutor接口介绍
- SpringBoot第二讲 利用Spring Data JPA实现数据库的访问(二)_分页和JpaSpecificationExecutor接口介绍
- SpringBoot第二讲 利用Spring Data JPA实现数据库的访问(二)_分页和JpaSpecificationExecutor接口介绍
- Spring Data JPA自定义查询,分页,排序
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- jpa之PagingAndSortingRepository带分页查询
- Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页
- spring-data-jpa 介绍 复杂查询,包括多表关联,分页,排序等
- 【java】spring-data-jpa 集成hibernate实现多条件分页查询
- Spring Data JPA+kkpager实现分页功能实例
- spring-data-jpa实现增删改查以及分页操作方法
- jpa之PagingAndSortingRepository带分页查询
- Spring Data JPA+kkpager实现分页功能
- Spring Data Jpa --分页、排序查询
- Spring Data Jpa - EasyUi Datagrid 分页简单实现(一)