javascript
Spring Data 分页和排序 PagingAndSortingRepository的使用(九)
繼承PagingAndSortingRepository
我們可以看到,BlogRepository定義了這樣一個(gè)方法:Page<Blog> findByDeletedFalse(Pageable pageable);,我們主要關(guān)注它的參數(shù)以及返回值。
- Pageable 是Spring Data庫中定義的一個(gè)接口,該接口是所有分頁相關(guān)信息的一個(gè)抽象,通過該接口,我們可以得到和分頁相關(guān)所有信息(例如pageNumber、pageSize等),這樣,Jpa就能夠通過pageable參數(shù)來得到一個(gè)帶分頁信息的Sql語句。
- Page類也是Spring Data提供的一個(gè)接口,該接口表示一部分?jǐn)?shù)據(jù)的集合以及其相關(guān)的下一部分?jǐn)?shù)據(jù)、數(shù)據(jù)總數(shù)等相關(guān)信息,通過該接口,我們可以得到數(shù)據(jù)的總體信息(數(shù)據(jù)總數(shù)、總頁數(shù)...)以及當(dāng)前數(shù)據(jù)的信息(當(dāng)前數(shù)據(jù)的集合、當(dāng)前頁數(shù)等)
Spring Data Jpa除了會(huì)通過命名規(guī)范幫助我們擴(kuò)展Sql語句外,還會(huì)幫助我們處理類型為Pageable的參數(shù),將pageable參數(shù)轉(zhuǎn)換成為sql'語句中的條件,同時(shí),還會(huì)幫助我們處理類型為Page的返回值,當(dāng)發(fā)現(xiàn)返回值類型為Page,Spring Data Jpa將會(huì)把數(shù)據(jù)的整體信息、當(dāng)前數(shù)據(jù)的信息,分頁的信息都放入到返回值中。這樣,我們就能夠方便的進(jìn)行個(gè)性化的分頁查詢。
分頁:
package org.springdata.repository;import org.springdata.domain.Employee; import org.springframework.data.repository.PagingAndSortingRepository;/***/ public interface EmployeePadingAndSortingResponstory extends PagingAndSortingRepository<Employee,Integer> { }? 編寫測(cè)試類
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("查詢總頁數(shù):"+employeeList.getTotalPages()); System.out.println("查詢總記錄數(shù):"+employeeList.getTotalElements()); System.out.println("查詢當(dāng)前第幾頁:"+employeeList.getNumber()+1); System.out.println("查詢當(dāng)前頁面的集合:"+employeeList.getContent()); System.out.println("查詢當(dāng)前頁面的記錄數(shù):"+employeeList.getNumberOfElements()); } }
查詢結(jié)果 ? 咱們先在Employee 實(shí)體類 重寫下toString()方法 ?才能打印集合數(shù)據(jù)
?
排序:
基于上面的查詢集合 ?我們新建一個(gè)測(cè)試方法
@Testpublic void testPageAndSord(){//根據(jù)id 進(jìn)行降序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("查詢總頁數(shù):"+employeeList.getTotalPages()); System.out.println("查詢總記錄數(shù):"+employeeList.getTotalElements()); System.out.println("查詢當(dāng)前第幾頁:"+employeeList.getNumber()+1); System.out.println("查詢當(dāng)前頁面的集合:"+employeeList.getContent()); System.out.println("查詢當(dāng)前頁面的記錄數(shù):"+employeeList.getNumberOfElements()); }我們可以看到 ?最大id 排前面了
?
轉(zhuǎn)載于:https://www.cnblogs.com/fzng/p/7256896.html
總結(jié)
以上是生活随笔為你收集整理的Spring Data 分页和排序 PagingAndSortingRepository的使用(九)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Concurrent实现原理
- 下一篇: HDU 6047 Maximum Seq