Spring?JPA之find拓展方法示例詳解
前言
前兩篇我們?cè)敿?xì)了解了 findById 和 findAll 以及 findAll 的分頁查詢,如果說JPA只有上面的兩種查詢功能,那就太low了,今天讓我們?cè)偕钊氲娜ヌ骄恳幌缕渌樵兎椒ā?/p>
一、單條件查詢
類似 select * from * where 條件 的查詢
1、精確查詢(確定值,例如:=、is)
Dao 層(因?yàn)橐呀?jīng)不是自帶方法了,所以需要在Dao添加接口)
User findByName(String name);
控制臺(tái)打印如下:
Hibernate:
select
user0_.id as id1_0_,
user0_.age as age2_0_,
user0_.name as name3_0_
from
user user0_
where
user0_.name=?
2、范圍查詢(一定范圍,例如<、<=、>、>=、in、between)
a)運(yùn)算符
Dao 層
/** * 小于age的數(shù)據(jù) * @param age * @return */ List<User> findByAgeLessThan(int age); /** * 小于等于age的數(shù)據(jù) * @param age * @return */ List<User> findByAgeLessThanEqual(int age);
控制臺(tái)打印如下:
Hibernate:
select
user0_.id as id1_0_,
user0_.age as age2_0_,
user0_.name as name3_0_
from
user user0_
where
user0_.age<?
Hibernate:
select
user0_.id as id1_0_,
user0_.age as age2_0_,
user0_.name as name3_0_
from
user user0_
where
user0_.age<=?
其他的“>”就是findByAgeGreaterThan,“>=”就是findByAgeGreaterThanEqual 等等
b)between
Dao 層
/** * age在ageLeft和ageRight之間的數(shù)據(jù) * @param ageLeft * @param ageRight * @return */ List<User> findByAgeBetween(int ageLeft,int ageRight);
控制臺(tái)打印如下:
Hibernate:
select
user0_.id as id1_0_,
user0_.age as age2_0_,
user0_.name as name3_0_
from
user user0_
where
user0_.age between ? and ?
c)in
Dao 層
/**java * age在ints內(nèi)的數(shù)據(jù) * @param ages * @return */ List<User> findByAgeIn(int[] ages);
控制臺(tái)打印如下:
Hibernate:
select
user0_.id as id1_0_,
user0_.age as age2_0_,
user0_.name as name3_0_
from
user user0_
where
user0_.age in (
? , ?
)
findByAgeNotIn的查詢跟in結(jié)果是相對(duì)的,但是用法是一樣的,傳入的參數(shù)也是數(shù)組
3、模糊查詢
模糊查詢無非就是 like 語句,這里我們就不詳細(xì)討論 like 如何使用了,只介紹一下 JPA 中的 like 如何去實(shí)現(xiàn)。
以下是Dao 層的like實(shí)現(xiàn)的各接口:
public List<User> findByNameLike(String name){
return userDao.findByNameLike(name);
}
public List<User> findByNameStartingWith(String name){
return userDao.findByNameStartingWith(name);
}
public List<User> findByNameStartsWith(String name){
return userDao.findByNameStartsWith(name);
}
public List<User> findByNameEndingWith(String name){
return userDao.findByNameEndingWith(name);
}
public List<User> findByNameContaining(String name){
return userDao.findByNameContaining(name);
}
上面雖然有5個(gè)不同的接口,但是控制臺(tái)打印卻是一致的,如下:
Hibernate:
select
user0_.id as id1_0_,
user0_.age as age2_0_,
user0_.name as name3_0_
from
user user0_
where
user0_.name like ? escape ?
那他們5個(gè)具體有啥區(qū)別呢?不急,我們?cè)敿?xì)看看:
a)findByNameLike
請(qǐng)求url findByNameLike?name=aa,控制臺(tái)打印入?yún)⑷缦拢?/p>
binding parameter [1] as [VARCHAR] - [aa]
從輸入值可以看出,單獨(dú)的like你傳什么就將什么放在like后面;如果傳的是純字符,則相當(dāng)于精確查詢;如果你加上“%”,那就可以進(jìn)行模糊查詢了:findByNameLike?name=%25aa(url中的“%”轉(zhuǎn)義為“%25”),控制臺(tái)打印入?yún)⑷缦拢?/p>
binding parameter [1] as [VARCHAR] - [%aa]
b)findByNameStartingWith
請(qǐng)求urlfindByNameStartingWith?name=aa,控制臺(tái)打印入?yún)⑷缦拢?/p>
binding parameter [1] as [VARCHAR] - [aa%]
從輸入值來看,這個(gè)就是獲取以“aa”開頭的所有數(shù)據(jù),其實(shí)從名字也可以看出來他的實(shí)際作用。后面的findByNameStartsWith跟這個(gè)的作用是一樣的。
以此類推:findByNameEndingWith(aa)、findByNameEndsWith(aa): 輸入值應(yīng)為 [%aa],就是獲取以“aa”為結(jié)尾的所有數(shù)據(jù);findByNameContaining(aa):輸入值應(yīng)為 [%aa%]\,就是獲取任意位置包含“aa”的數(shù)據(jù)。
二、多條件查詢
類似select* from * where 條件1 and 條件2Dao 層
User findByNameAndAge(String name, int age);
控制臺(tái)打印如下:
Hibernate:
select
user0_.id as id1_0_,
user0_.age as age2_0_,
user0_.name as name3_0_
from
user user0_
where
user0_.name=?
and user0_.age=?
多條件查詢其實(shí)就是多個(gè)單條件查詢所疊加的效果;主要使用 and 來表示同時(shí)滿足多個(gè)條件的結(jié)果,而 or 用于表示滿足其中一個(gè)條件的結(jié)果。
三、關(guān)鍵字
以下是整理的JPA支持的關(guān)鍵字,大家可以自行取之。
| 關(guān)鍵字 | 示例 | JPQL片段 |
|---|---|---|
| And | findByNameAndAge | ... where x.name = ?1 and x.age = ?2 |
| Or | findByNameOrAge | ... where x.name = ?1 or x.age = ?2 |
| Is,Equals | findByName,findByNameIs,findByNameEquals | ... where x.name = ?1 |
| Between | findByAgeBetween | ... where x.age between ?1 and ?2 |
| LessThan | findByAgeLessThan | ... where x.age < ?1 |
| LessThanEqual | findByAgeLessThanEqual | ... where x.age <= ?1 |
| GreaterThan | findByAgeGreaterThan | ... where x.age > ?1 |
| GreaterThanEqual | findByAgeGreaterThanEqual | ... where x.age >= ?1 |
| After | findByAgeAfter | ... where x.age > ?1 |
| Before | findByAgeBefore | ... where x.age< ?1 |
| IsNull | findByAgeIsNull | ... where x.age is null |
| IsNotNull,NotNull | findByAge(Is)NotNull | ... where x.age not null |
| Like | findByNameLike | ... where x.name like ?1 |
| NotLike | findByNameNotLike | ... where x.name not like ?1 |
| StartingWith | findByNameStartingWith | ... where x.name like ?1 (參數(shù)會(huì)綁定到%后面) |
| EndingWith | findByNameEndingWith | ... where x.name like ?1 (參數(shù)會(huì)綁定在%前面) |
| Containing | findByNameContaining | ... where x.name like ?1 (參數(shù)會(huì)綁定在兩個(gè)%中間) |
| OrderBy | findByAgeOrderByNameDesc | ... where x.age = ?1 order by name desc |
| Not | findByNameNot | ... where x.name <> ?1 |
| In | findByAgeIn(Collection ages) | ... where x.age in ?1 |
| NotIn | findByAgeNotIn(Connection ages) | ... where x.age not in ?1 |
| True | findByActiveTrue() | ... where x.active = true |
| Flase | findByActiveFalse() | ... where x.active = false |
| IgnoreCase | findByNameIgnoreCase | ... where UPPER(x.name) = UPPER(?1) |
最后總結(jié):
以上總結(jié)的這些關(guān)鍵字,大家可以直接用來在Dao層構(gòu)造對(duì)應(yīng)接口。其實(shí)從關(guān)鍵字的語法來看,基本就已經(jīng)介紹了相關(guān)功能了,所以用起來其實(shí)也很方便,更多關(guān)于Spring JPA find拓展方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用@Order控制配置類/AOP/方法/字段的加載順序詳解
這篇文章主要介紹了使用@Order控制配置類/AOP/方法/字段的加載順序詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
詳解使用Spring AOP和自定義注解進(jìn)行參數(shù)檢查
本篇文章主要介紹了詳解使用Spring AOP和自定義注解進(jìn)行參數(shù)檢查,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Springboot項(xiàng)目啟動(dòng)成功后可通過五種方式繼續(xù)執(zhí)行
本文主要介紹了Springboot項(xiàng)目啟動(dòng)成功后可通過五種方式繼續(xù)執(zhí)行,主要包括CommandLineRunner接口,ApplicationRunner接口,ApplicationListener接口,@PostConstruct注解,InitalizingBean接口,感興趣的可以了解一下2023-12-12
IDEA導(dǎo)入geoserver項(xiàng)目的詳細(xì)步驟及注意事項(xiàng)
由于GeoServer是基于Java開發(fā)的。因此在安裝之前,必須確保安裝了Java。本文給大家分享IDEA導(dǎo)入geoserver項(xiàng)目的詳細(xì)步驟及注意事項(xiàng),感興趣的朋友一起看看吧2021-06-06
java設(shè)計(jì)模式學(xué)習(xí)之裝飾模式
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式學(xué)習(xí)之裝飾模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
自定義JmsListenerContainerFactory時(shí),containerFactory字段解讀
這篇文章主要介紹了自定義JmsListenerContainerFactory時(shí),containerFactory字段解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
springboot中不能獲取post請(qǐng)求參數(shù)的解決方法
這篇文章主要介紹了springboot中不能獲取post請(qǐng)求參數(shù)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Spring Boot 中使用 Mybatis Plus的操作方法
本文介紹了如何在 Spring Boot 項(xiàng)目中集成 Mybatis Plus,Spring Boot 與 MyBatis Plus 的集成非常簡(jiǎn)單,通過自動(dòng)配置和簡(jiǎn)潔的 API,可以大大減少開發(fā)中常見的數(shù)據(jù)庫操作代碼,需要的朋友參考下吧2024-12-12

