SpringData JPA之方法名查询的关键字表

  1. 在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)  =
原文地址:https://www.cnblogs.com/chaoyou/p/11698482.html