Spring?JPA之find拓展方法示例詳解
前言
前兩篇我們詳細了解了 findById 和 findAll 以及 findAll 的分頁查詢,如果說JPA只有上面的兩種查詢功能,那就太low了,今天讓我們再深入的去探究一下其他查詢方法。
一、單條件查詢
類似 select * from * where 條件 的查詢
1、精確查詢(確定值,例如:=、is)
Dao 層(因為已經(jīng)不是自帶方法了,所以需要在Dao添加接口)
User findByName(String name);
控制臺打印如下:
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)運算符
Dao 層
/** * 小于age的數(shù)據(jù) * @param age * @return */ List<User> findByAgeLessThan(int age); /** * 小于等于age的數(shù)據(jù) * @param age * @return */ List<User> findByAgeLessThanEqual(int age);
控制臺打印如下:
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);
控制臺打印如下:
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);
控制臺打印如下:
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é)果是相對的,但是用法是一樣的,傳入的參數(shù)也是數(shù)組
3、模糊查詢
模糊查詢無非就是 like 語句,這里我們就不詳細討論 like 如何使用了,只介紹一下 JPA 中的 like 如何去實現(xiàn)。
以下是Dao 層的like實現(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個不同的接口,但是控制臺打印卻是一致的,如下:
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個具體有啥區(qū)別呢?不急,我們詳細看看:
a)findByNameLike
請求url findByNameLike?name=aa,控制臺打印入?yún)⑷缦拢?/p>
binding parameter [1] as [VARCHAR] - [aa]
從輸入值可以看出,單獨的like你傳什么就將什么放在like后面;如果傳的是純字符,則相當于精確查詢;如果你加上“%”,那就可以進行模糊查詢了:findByNameLike?name=%25aa(url中的“%”轉(zhuǎn)義為“%25”),控制臺打印入?yún)⑷缦拢?/p>
binding parameter [1] as [VARCHAR] - [%aa]
b)findByNameStartingWith
請求urlfindByNameStartingWith?name=aa,控制臺打印入?yún)⑷缦拢?/p>
binding parameter [1] as [VARCHAR] - [aa%]
從輸入值來看,這個就是獲取以“aa”開頭的所有數(shù)據(jù),其實從名字也可以看出來他的實際作用。后面的findByNameStartsWith跟這個的作用是一樣的。
以此類推: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);
控制臺打印如下:
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=?
多條件查詢其實就是多個單條件查詢所疊加的效果;主要使用 and 來表示同時滿足多個條件的結(jié)果,而 or 用于表示滿足其中一個條件的結(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ù)會綁定到%后面) |
| EndingWith | findByNameEndingWith | ... where x.name like ?1 (參數(shù)會綁定在%前面) |
| Containing | findByNameContaining | ... where x.name like ?1 (參數(shù)會綁定在兩個%中間) |
| 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)造對應(yīng)接口。其實從關(guān)鍵字的語法來看,基本就已經(jīng)介紹了相關(guān)功能了,所以用起來其實也很方便,更多關(guān)于Spring JPA find拓展方法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用@Order控制配置類/AOP/方法/字段的加載順序詳解
這篇文章主要介紹了使用@Order控制配置類/AOP/方法/字段的加載順序詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
詳解使用Spring AOP和自定義注解進行參數(shù)檢查
本篇文章主要介紹了詳解使用Spring AOP和自定義注解進行參數(shù)檢查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能
這篇文章主要為大家詳細介紹了java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
Springboot項目啟動成功后可通過五種方式繼續(xù)執(zhí)行
本文主要介紹了Springboot項目啟動成功后可通過五種方式繼續(xù)執(zhí)行,主要包括CommandLineRunner接口,ApplicationRunner接口,ApplicationListener接口,@PostConstruct注解,InitalizingBean接口,感興趣的可以了解一下2023-12-12
自定義JmsListenerContainerFactory時,containerFactory字段解讀
這篇文章主要介紹了自定義JmsListenerContainerFactory時,containerFactory字段解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
springboot中不能獲取post請求參數(shù)的解決方法
這篇文章主要介紹了springboot中不能獲取post請求參數(shù)的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Spring Boot 中使用 Mybatis Plus的操作方法
本文介紹了如何在 Spring Boot 項目中集成 Mybatis Plus,Spring Boot 與 MyBatis Plus 的集成非常簡單,通過自動配置和簡潔的 API,可以大大減少開發(fā)中常見的數(shù)據(jù)庫操作代碼,需要的朋友參考下吧2024-12-12

