Spring Data JPA 關(guān)鍵字Exists的用法說(shuō)明
Spring Data JPA 關(guān)鍵字Exists
查詢(xún)數(shù)據(jù)庫(kù)中的此數(shù)據(jù)是否已存在:
例子:
查詢(xún)sys_user表中的一個(gè)user是否存在,類(lèi)SysUser對(duì)應(yīng)的是數(shù)據(jù)庫(kù)中的sys_user表,SysUserId是表sys_user的主鍵類(lèi)(ID類(lèi))。
如果查詢(xún)一個(gè)user,user的accountNo為demo。
userID為demo1,表sys_user的主鍵是accountNo和userID,下面代碼中的方法是查詢(xún)這個(gè)user是否存在,如果存在則返回true,不存在則返回false。
@Repository
public interface SysUserRepository extends JpaRepository<SysUser, SysUserId> {
@Override
boolean exists(SysUserId sysUserId);
}
Spring data jpa支持的關(guān)鍵字介紹
| Sample | JPQL snippet | |
|---|---|---|
|
And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
|
Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
|
Is,Equals |
findByFirstname,findByFirstnameIs,findByFirstnameEquals |
… where x.firstname = ?1 |
|
Between |
findByStartDateBetween |
… where x.startDate 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 |
findByStartDateAfter |
… where x.startDate > ?1 |
|
Before |
findByStartDateBefore |
… where x.startDate < ?1 |
|
IsNull |
findByAgeIsNull |
… where x.age is null |
|
IsNotNull,NotNull |
findByAge(Is)NotNull |
… where x.age not null |
|
Like |
findByFirstnameLike |
… where x.firstname like ?1 |
|
NotLike |
findByFirstnameNotLike |
… where x.firstname not like ?1 |
|
StartingWith |
findByFirstnameStartingWith |
… where x.firstname like ?1(parameter bound with appended %) |
|
EndingWith |
findByFirstnameEndingWith |
… where x.firstname like ?1(parameter bound with prepended %) |
|
Containing |
findByFirstnameContaining |
… where x.firstname like ?1(parameter bound wrapped in %) |
|
OrderBy |
findByAgeOrderByLastnameDesc |
… where x.age = ?1 order by x.lastname desc |
|
Not |
findByLastnameNot |
… where x.lastname <> ?1 |
|
In |
findByAgeIn(Collection<Age> ages) |
… where x.age in ?1 |
|
NotIn |
findByAgeNotIn(Collection<Age> age) |
… where x.age not in ?1 |
|
True |
findByActiveTrue() |
… where x.active = true |
|
False |
findByActiveFalse() |
… where x.active = false |
|
IgnoreCase |
findByFirstnameIgnoreCase |
… where UPPER(x.firstame) = UPPER(?1) |
| Keyword | Sample | JPQL snippet |
|---|---|---|
|
And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
|
Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
|
Is,Equals |
findByFirstname,findByFirstnameIs,findByFirstnameEquals |
… where x.firstname = ?1 |
|
Between |
findByStartDateBetween |
… where x.startDate 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 |
findByStartDateAfter |
… where x.startDate > ?1 |
|
Before |
findByStartDateBefore |
… where x.startDate < ?1 |
|
IsNull |
findByAgeIsNull |
… where x.age is null |
|
IsNotNull,NotNull |
findByAge(Is)NotNull |
… where x.age not null |
|
Like |
findByFirstnameLike |
… where x.firstname like ?1 |
|
NotLike |
findByFirstnameNotLike |
… where x.firstname not like ?1 |
|
StartingWith |
findByFirstnameStartingWith |
… where x.firstname like ?1(parameter bound with appended %) |
|
EndingWith |
findByFirstnameEndingWith |
… where x.firstname like ?1(parameter bound with prepended %) |
|
Containing |
findByFirstnameContaining |
… where x.firstname like ?1(parameter bound wrapped in %) |
|
OrderBy |
findByAgeOrderByLastnameDesc |
… where x.age = ?1 order by x.lastname desc |
|
Not |
findByLastnameNot |
… where x.lastname <> ?1 |
|
In |
findByAgeIn(Collection<Age> ages) |
… where x.age in ?1 |
|
NotIn |
findByAgeNotIn(Collection<Age> age) |
… where x.age not in ?1 |
|
True |
findByActiveTrue() |
… where x.active = true |
|
False |
findByActiveFalse() |
… where x.active = false |
|
IgnoreCase |
findByFirstnameIgnoreCase |
… where UPPER(x.firstame) = UPPER(?1) |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中自定義數(shù)據(jù)類(lèi)型轉(zhuǎn)換的方法詳解
Spring3引入了一個(gè)core.onvert包,提供一個(gè)通用類(lèi)型轉(zhuǎn)換系統(tǒng)。在Spring容器中,可以使用這個(gè)系統(tǒng)作為PropertyEditor實(shí)現(xiàn)的替代,將外部化的bean屬性值字符串轉(zhuǎn)換為所需的屬性類(lèi)型。本文將詳解這一系統(tǒng)的使用方法,需要的可以參考一下2022-06-06
IntelliJ IDEA中使用mybatis-generator的示例
這篇文章主要介紹了IntelliJ IDEA中使用mybatis-generator,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Java文件字符輸入流FileReader讀取txt文件亂碼的解決
這篇文章主要介紹了Java文件字符輸入流FileReader讀取txt文件亂碼的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java編寫(xiě)實(shí)現(xiàn)窗體程序顯示日歷
這篇文章主要為大家詳細(xì)介紹了Java編寫(xiě)實(shí)現(xiàn)窗體程序顯示日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
簡(jiǎn)單講解在Java編程中實(shí)現(xiàn)設(shè)計(jì)模式中的單例模式結(jié)構(gòu)
這篇文章主要介紹了簡(jiǎn)單講解在Java編程中實(shí)現(xiàn)設(shè)計(jì)模式中的單例模式結(jié)構(gòu),設(shè)計(jì)模式是最基本直白簡(jiǎn)單的一種設(shè)計(jì)模式,需要的朋友可以參考下2016-04-04

