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 條件2
Dao 層
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)文章
Springboot+ElementUi實(shí)現(xiàn)評(píng)論、回復(fù)、點(diǎn)贊功能
這篇文章主要介紹了通過Springboot ElementUi實(shí)現(xiàn)評(píng)論、回復(fù)、點(diǎn)贊功能。如果是自己評(píng)論的還可以刪除,刪除的規(guī)則是如果該評(píng)論下還有回復(fù),也一并刪除。需要的可以參考一下2022-01-01Java實(shí)現(xiàn)通過IP獲取IP歸屬地的方法(離線+在線)
我們都知道安全攻擊都是在某臺(tái)客戶機(jī)上執(zhí)行某些惡意操作致使服務(wù)端響應(yīng)異常崩潰亦或響應(yīng)數(shù)據(jù)被篡改,首先我想到的是對(duì)訪問的web端做一個(gè)IP的校驗(yàn),那么我們首先得知道客戶端的IP是多少,接下來此文重點(diǎn)介紹如何獲得,需要的朋友可以參考下2023-10-10SpringBoot使用Interceptor攔截器的實(shí)例
這篇文章主要介紹了SpringBoot使用Interceptor攔截器的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03IDEA配置JRebel實(shí)現(xiàn)熱部署的方法
這篇文章主要介紹了IDEA配置JRebel實(shí)現(xiàn)熱部署的方法,本文給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01淺析打開eclipse出現(xiàn)Incompatible JVM的解決方法
本篇文章是對(duì)打開eclipse出現(xiàn)Incompatible JVM的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07springboot注解Aspect實(shí)現(xiàn)方案
本文提供一種自定義注解,來實(shí)現(xiàn)業(yè)務(wù)審批操作的DEMO,不包含審批流程的配置功能。對(duì)springboot注解Aspect實(shí)現(xiàn)方案感興趣的朋友一起看看吧2022-01-01