- 在springdata jpa 中我們經常會對數據庫進行各種各樣的CURD操作。比如,查詢操作。當然對於一些簡單業務的CURD操作由jpa自帶的JpaRepository接口就行已經可以滿足了,但是往往在開發中需求的復雜程度是不能夠預測的,所以構建一些能自適應相對復雜業務的CURD操作是很有必要的。這時候我們也可以選擇采用@Query注解進行原生SQL語句的編寫、獲取采用@Query注解編寫SpEL語句進行着對數據庫的CURD操作。而現在我要說的是采用jpa 提供的方法名派生查詢的方式。比如在Person類中有lastName屬性,就可以構造一個這樣的查詢方法:findPersonByLastName(String lastName)。意思是根據lastName屬性值查詢響應的Person對象,而Person類映射着數據表,自然而然地形成了間接起到了查詢數據表的操作。當然前提是Person的持久化接口先繼承了jpa的持久化接口JpaRepository。下面是方法名派生查詢中能用到的關鍵字:
關鍵字 | 樣品方法 | SQPL片段 | 原生SQL | 備注 |
And | findPersonByLastNameAndFirstName(String lastName,String firstName) | ...where x.lastName = ?1 and x.firstName = ?2 | ...where lastName = 1? and firstName = 2? | & |
Or | findPersonByLastnameOrFirstname(String lastName,String firstName) | ...where x.lastName= ?1 or x.firstName = ?2 | ...where lastName = ?1 or firstName = ?2 | | |
Is、Equals | findPersonByLastName(String lastName) findPersonByLastNameIs(String lastName) findPersonByLastNameEquals(String lastName) |
...where x.lastName= ?1 | ...where lastName = ?1 | = |
Between | findPersonByAge(int minAge, int maxAge) | ...where x.age between ?1 and ?2 | ...where age between ?1 and ?2 | >= and < |
LessThan | findPersonByAgeLessThan(int age) | ...where x.age < ?1 | ...where age < ?1 | < |
LessThanEqual | findPersonByAgeLessThanEqual(int age) | ...where x.age <= ?1 | ...where age <= ?1 | <= |
GreaterThan | findPersonByAgeGreaterThanEqual(int age) | ...where x.age > ?1 | ...where age > ?1 | > |
GreaterThanEqual | findPersonByAgeGreaterThanEqual(int age) | ...where x.age >= ?1 | ...where age >= ?1 | >= |
After | findPersonByStartDateAfter(Date startDate) | ...where x.startDate > ?1 | ...where startDate > ?1 | > |
Before | findPersonByStartDateBefore(Date startDate) | ...where x.startDate < ?1 | ...where startDate < ?1 | < |
isNull、Null | findPersonByLastNameIsNull/findPersonByLastNameNull | ...where x.lastName is null | ...where lastName is null | is null |
isNotNull、NotNull | findPersonByLastNameisNotNull/findPersonByLastNameNotNull | ...where x.lastName is not null | ...where lastName is not null | is not null |
Like | findPersonByLastNameLike(String lastName) | ...where x.lastName like '%' + ?1 + '%' | ...where lastName like ?1 | like |
NotLike | findPersonByLastNameNotLike(String lastName) | ...where x.lastName not like ?1 | ...where lastName not like ?1 | not like |
StartingWith | findPersonByLastNameStartingWith(String lastName) | ...where x.lastName like '%' + ?1 | ...where lastName like '%' + ?1 | like |
EndingWith | findPersonByLastNameEndingWith(String lastName) | ...where x.lastName like ?1 + '%' | ...where lastName like ?1 + '%' | like |
Containing | findPersonByLastNameContaiting(String lastName) | ...where x.lastName like '%' + ?1 + '%' | ...where lastName like '%' + ?1 + '%' | like |
OrderBy | findPersonByOrderByLastNameDesc/Asc | ...order by x.lastName desc | ...order by lastName desc | order by |
Not | findPersonByLastNameNot(String lastName) | ...where x.lastName <> ?1 | ...where lastName <> ?1 | <> / != |
In | findPersonByAgeIn(List<Integer> ages) | ...where x.lastName in(?1) | ...where lastName in(?1) | in |
NotIn | findPersonByAgeNotIn(List<Integer> ages) | ...where x.lastName not in(?1) | ...where lastName not in(?1) | not in |
True | findPersonByFlagTrue(Boolean flag) | ...where x.flag = true | ...where flag = true | = |
False | findPersonByFlagFalse(Boolean flag) | ...where x.flag = false | ...where flag = false | = |
IgnoreCase | findPersonByLaseNameIgnoreCase(String lastName) | ...where upper(x.lastName) = upper(?1) | ...where upper(lastName) = upper(?1) | = |