日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發(fā)布時(shí)間:2024/1/17 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Data 分页和排序 PagingAndSortingRepository的使用(九) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

繼承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除了會通過命名規(guī)范幫助我們擴(kuò)展Sql語句外,還會幫助我們處理類型為Pageable的參數(shù),將pageable參數(shù)轉(zhuǎn)換成為sql'語句中的條件,同時(shí),還會幫助我們處理類型為Page的返回值,當(dāng)發(fā)現(xiàn)返回值類型為Page,Spring Data Jpa將會把數(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> { }

? 編寫測試類

  

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è)測試方法

@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的使用(九)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。