SpringBoot集成Jpa對(duì)數(shù)據(jù)進(jìn)行排序、分頁(yè)、條件查詢(xún)和過(guò)濾操作
之前介紹了SpringBoot集成Jpa的簡(jiǎn)單使用,接下來(lái)介紹一下使用Jpa連接數(shù)據(jù)庫(kù)對(duì)數(shù)據(jù)進(jìn)行排序、分頁(yè)、條件查詢(xún)和過(guò)濾操作。首先創(chuàng)建Springboot工程并已經(jīng)繼承JPA依賴(lài),如果不知道可以查看我的另一篇文進(jìn)行學(xué)習(xí),這里不做介紹。參考文章地址
1、排序查詢(xún)
通過(guò)findAll方法的Sort類(lèi)進(jìn)行排序,根據(jù)實(shí)體類(lèi)字段進(jìn)行排序。descending降序,ascending升序,默認(rèn)不填為ascending升序。
List<User> mapperAll = userMapper.findAll(Sort.by("id").descending());
mapperAll.forEach(System.out::println);查詢(xún)結(jié)果:

Sort.by() 里面是一個(gè)可變參數(shù),可以傳入一個(gè)或多個(gè)值。
//先根據(jù)狀態(tài)進(jìn)行排序,然后在根據(jù)ID進(jìn)行排序
List<User> statusAll = userMapper.findAll(Sort.by("status","id").descending());
statusAll.forEach(System.out::println);
設(shè)置第一個(gè)屬性降序,第二個(gè)屬性升序
Sort sort = Sort.by("status").descending().and(Sort.by("id").ascending());
List<User> diffOb= userMapper.findAll(sort);
diffOb.forEach(System.out::println);
如果傳入的字段信息,實(shí)體類(lèi)中沒(méi)有,代碼訪(fǎng)問(wèn)會(huì)報(bào)錯(cuò)。如:
List<User> exceptionAll = userMapper.findAll(Sort.by("en_name").descending());
exceptionAll.forEach(System.out::println);會(huì)報(bào)找不到該字段信息異常:
org.springframework.data.mapping.PropertyReferenceException: No property en found for type User! Did you mean 'id'?
2、分頁(yè)查詢(xún)
JPA為我們提供了分頁(yè)的方法,我們可以查看接口集成的JpaRepository接口的關(guān)系圖。發(fā)現(xiàn)有一個(gè)PagingAndSortingRepository接口。

點(diǎn)擊該接口進(jìn)行查看:

使用這個(gè)接口方法就可以實(shí)現(xiàn)分頁(yè)查詢(xún)了。我們發(fā)現(xiàn)這個(gè)方法需要傳入一個(gè)Pageable,點(diǎn)擊Pageable查看發(fā)現(xiàn)它也是一個(gè)接口我們點(diǎn)擊查看它的實(shí)現(xiàn)類(lèi)。

使用PageRequest類(lèi)就可以實(shí)現(xiàn)分頁(yè)了,PageRequest有個(gè)靜態(tài)的of方法,page參數(shù)為顯示當(dāng)前頁(yè)數(shù),size為顯示當(dāng)前顯示多少數(shù)據(jù).
//第一頁(yè),顯示4條數(shù)據(jù) 0為一頁(yè) 1為二頁(yè) 輸入頁(yè)數(shù)大于總頁(yè)數(shù)則輸出內(nèi)容為空
PageRequest request = PageRequest.of(0, 4);
Page<User> userPage = userMapper.findAll(request);
System.out.println();
System.out.println("總頁(yè)數(shù): "+userPage.getTotalPages()); //總頁(yè)數(shù)
System.out.println("總條數(shù): "+userPage.getTotalElements()); //總條數(shù)
System.out.println("查詢(xún)的數(shù)據(jù): "+userPage.getContent());//查詢(xún)的數(shù)據(jù)
System.out.println("顯示條數(shù): "+userPage.getSize()); //顯示條數(shù)
System.out.println("當(dāng)前頁(yè)數(shù): "+Integer.valueOf(Integer.valueOf(userPage.getNumber())+1)); //當(dāng)前頁(yè)數(shù)查詢(xún)結(jié)果為:

還可以給分頁(yè)查詢(xún)進(jìn)行排序,根據(jù)主鍵進(jìn)行降序排序
PageRequest pageRequest = PageRequest.of(0, 4, Sort.by("id").descending());
Page<User> orderPAge = userMapper.findAll(pageRequest);
System.out.println("查詢(xún)的數(shù)據(jù): "+orderPAge.getContent());//查詢(xún)的數(shù)據(jù)
orderPAge.getContent().forEach(System.out::println);分頁(yè)進(jìn)行多個(gè)字段的排序同一排序
PageRequest pageRequest1 = PageRequest.of(0, 4, Sort.Direction.DESC, "status","id");
Page<User> orde = userMapper.findAll(pageRequest1);
System.out.println("查詢(xún)的數(shù)據(jù): "+orde.getContent());//查詢(xún)的數(shù)據(jù)
orde.getContent().forEach(System.out::println);分頁(yè)進(jìn)行多個(gè)字段的不同排序 根據(jù)status 升序,id降序排序
PageRequest of = PageRequest.of(0, 4, Sort.by("status").ascending().and(Sort.by("id").descending()));
Page<User> ord = userMapper.findAll(of);
ord.getContent().forEach(System.out::println);查看查詢(xún)輸出sql結(jié)果:

3、條件查詢(xún)
下面我們來(lái)看使用JPA進(jìn)行條件查詢(xún)。在JPA中JPA使用findBy方法自定義查詢(xún)。也可以使用findAllBy。這兩個(gè)沒(méi)有區(qū)別實(shí)際上還是使用的finBy...進(jìn)行查詢(xún)的。
//根據(jù)賬號(hào)名稱(chēng)進(jìn)行查詢(xún),有信息放回該條數(shù)據(jù),沒(méi)有查詢(xún)到則放回null,如果查詢(xún)多條數(shù)據(jù)則會(huì)報(bào)錯(cuò)
User user=userMapper.findByAccount("hibernateTest");
//Dao層
User findByAccount(String account);如果查詢(xún)多條會(huì)報(bào)錯(cuò)javax.persistence.NonUniqueResultException: query did not return a unique result: 4,把接收參數(shù)改為L(zhǎng)ist,就可以查詢(xún)多條數(shù)據(jù)了。
List<User> findByAccount(String account);
findBy后面還可以支持多種關(guān)鍵詞進(jìn)行查詢(xún):
And:等價(jià)于SQL中的 and 關(guān)鍵字, findByAccountAndPassword(String account, String password);
And:等價(jià)于SQL中的 and 關(guān)鍵字,findByAccountAndPassword(String account, String password);
Or:等價(jià)于SQL中的 or 關(guān)鍵字, findByAccountOrName(String account, String name);
Between:等價(jià)于SQL中的 between 關(guān)鍵字,findByIdBetween(int min, int max);
LessThan:等價(jià)于 SQL 中的 "<",findByIdLessThan(int id);
GreaterThan:等價(jià)于 SQL 中的">",findByIdGreaterThan(int id);
IsNull:等價(jià)于 SQL 中的 "is null",findByNameIsNull();
IsNotNull:等價(jià)于 SQL 中的 "is not null",findByNameIsNotNull();NotNull:與 IsNotNull 等價(jià);
Like:等價(jià)于 SQL 中的 "like",findByNameLike(String name);這樣傳值需要加 %name%,可以使用Containing,這個(gè)方法會(huì)在字符串兩邊都加上%,
除此之外還有StartingWith 和EndingWith,分別為 ?%,%?
NotLike:等價(jià)于 SQL 中的 "not like",findByNameNotLike(String name);傳值需要加 %name%
Not:等價(jià)于 SQL 中的 "!=",findByUsernameNot(String user);
OrderBy:等價(jià)于 SQL 中的 "order by",findByNameOrderByStatusAsc(String name);
In:等價(jià)于 SQL 中的 "in",findByIdIn(Collection userIdList) ,方法的參數(shù)可以是 Collection 類(lèi)型,也可以是數(shù)組或者不定長(zhǎng)參數(shù);
NotIn:等價(jià)于 SQL 中的 "not in",findByNameNotIn(Collection userList) ,方法的參數(shù)可以是 Collection 類(lèi)型,也可以是數(shù)組或者不定長(zhǎng)參數(shù);
這里介紹一下模糊查詢(xún)使用方法:
List<User> userList2=userMapper.findByNameLike("%hibernateTest%");
userList2.forEach(System.out::println);
List<User> userList3=userMapper.findByNameContaining("hibernateTest");
userList3.forEach(System.out::println);
//dao層
List<User> findByNameLike(String name);
List<User> findByNameContaining(String name);
上面的findBy規(guī)則介紹完畢后,接下來(lái)我們結(jié)合上面的分頁(yè)和排序,查詢(xún)出過(guò)濾后的數(shù)據(jù)。條件+分頁(yè)+排序查詢(xún)使用:
Page<User> userPage = userMapper.findByNameLike("%hibernateJPa%", PageRequest.of(0, 2, Sort.by("id").descending()));
System.out.println("總頁(yè)數(shù): "+userPage.getTotalPages()); //總頁(yè)數(shù)
System.out.println("總條數(shù): "+userPage.getTotalElements()); //總條數(shù)
System.out.println("查詢(xún)的數(shù)據(jù): "+userPage.getContent());//查詢(xún)的數(shù)據(jù)
System.out.println("顯示條數(shù): "+userPage.getSize()); //顯示條數(shù)
System.out.println("當(dāng)前頁(yè)數(shù): "+Integer.valueOf(Integer.valueOf(userPage.getNumber())+1)); //當(dāng)前頁(yè)數(shù)
//dao層
Page<User> findByNameLike(String name, Pageable pageable);查詢(xún)結(jié)果:

使用JPA進(jìn)行查詢(xún)、排序和分頁(yè)我們就介紹完畢了。關(guān)于JPA后面還有復(fù)雜的sql條件查詢(xún),需要繼承JpaSpecificationExecutor接口。JpaSpecificationExecutor接口方法不多但是很好用,推薦大家有時(shí)間可以學(xué)習(xí)一下。

到此這篇關(guān)于SpringBoot集成Jpa對(duì)數(shù)據(jù)進(jìn)行排序、分頁(yè)、條件查詢(xún)和過(guò)濾操作的文章就介紹到這了,更多相關(guān)SpringBoot集成Jpa數(shù)據(jù)分頁(yè)查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 實(shí)現(xiàn)最小二叉樹(shù)堆排序的實(shí)例
這篇文章主要介紹了java 實(shí)現(xiàn)最小二叉樹(shù)堆排序的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
SWT(JFace)體驗(yàn)之模擬BorderLayout布局
SWT(JFace)體驗(yàn)之模擬BorderLayout布局代碼。2009-06-06
Java 正則表達(dá)式入門(mén)詳解(基礎(chǔ)進(jìn)階)
最近看到很多同學(xué)想要學(xué)習(xí)java正則表達(dá)式的一些知識(shí),那么腳本之家小編就為大家介紹一下,其實(shí)正則表達(dá)式實(shí)用性很強(qiáng),處理大幅文字的時(shí)候都需要用得到,語(yǔ)法也大同小異2017-10-10
Java Web開(kāi)發(fā)項(xiàng)目中中文亂碼解決方法匯總
這篇文章主要為大家詳細(xì)匯總了Java Web開(kāi)發(fā)項(xiàng)目中中文亂碼的解決方法,分析了5種Java Web中文亂碼情況,感興趣的小伙伴們可以參考一下2016-05-05
java實(shí)現(xiàn)Composite組合模式的實(shí)例代碼
這篇文章主要介紹了java實(shí)現(xiàn)Composite組合模式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

