Spring Data JPA 查询 -- 方法名查询

FFish 2021年09月08日 18次浏览

例子

Spring Data JPA 支持完全根据Repository方法名查询,不需要写任何SQL语句,参考如下写法:

@Entity
public class Person {

    @Id
    private int id;
    private String firstname;
    private String lastname;
    private int age;
    private String email;
    private boolean active;
    private Date updatedAt;
    private Date createdAt;
}
public interface PersonRepository extends Repository<Person, Long> {

    List<Person> findByEmailAndLastname(String email, String lastname);
}

可以看到findByEmailAndLastname()函数中,Email对应Person.emailLastname对应Person.lastname,非常简洁。

方法命名参考表

关键字 Distinct

例子:findDistinctByLastnameAndFirstname
JPQL:select distinct …​ where person.lastname = ?1 and person.firstname = ?2

SQLdistinct查询方式的支持,实际上,由于JPA的机制所限,Distinct几乎很难在实际编码中使用到。

关键字 And

例子:findByLastnameAndFirstname
JPQL:… where person.lastname = ?1 and person.firstname = ?2

关键字 Or

例子:findByLastnameOrFirstname
JPQL:... where person.lastname = ?1 or person.firstname = ?2

关键字 Is, Equals

例子:findByFirstname, findByFirstnameIs, findByFirstnameEquals
JPQL:... where person.firstname = ?1

关键字 Between

例子:findByAgeBetween
JPQL:... where person.age between ?1 and ?2

关键字 LessThan

例子:findByAgeLessThan
JPQL:... where person.age < ?1

关键字 LessThanEqual

例子:findByAgeLessThanEqual
JPQL:... where person.age <= ?1

关键字 GreaterThan

例子:findByAgeGreaterThan
JPQL:... where person.age > ?1

关键字 GreaterThanEqual

例子:findByAgeGreaterThanEqual
JPQL:... where person.age >= ?1

关键字 After

例子:findByCreatedAtAfter
JPQL:... where person.createdAt > ?1

关键字 Before

例子:findByCreatedAtBefore
JPQL:... where person.createdAt < ?1

关键字 IsNull, Null

例子:findByAgeIsNull, findByAgeNull
JPQL:... where person.age is null

关键字 IsNotNull, NotNull

例子:findByAgeIsNotNull, findByAgeNotNull
JPQL:... where person.age not null

关键字 Like

例子:findByFirstnameLike
JPQL:... where person.firstname like ?1

关键字 NotLike

例子:findByFirstnameNotLike
JPQL:... where person.firstname not like ?1

关键字 StartingWith

例子:findByFirstnameStartingWith
JPQL:... where person.firstname like ?1(参数后紧跟%

关键字 EndingWith

例子:findByFirstnameEndingWith
JPQL:... where person.firstname like ?1(参数前为%

关键字 Containing

例子:findByFirstnameContaining
JPQL:... where person.firstname like ?1(参数前后都有%

关键字 OrderBy

例子:findByAgeOrderByLastnameDesc
JPQL:... where person.age = ?1 order by person.lastname desc

升序写法findByAgeOrderByLastnameAsc

关键字 Not

例子:findByLastnameNot
JPQL:... where person.lastname <> ?1

关键字 In

例子:findByAgeIn(Collection<Age> ages)
JPQL:... where person.age in ?1

关键字 NotIn

例子:findByAgeNotIn(Collection<Age> ages)
JPQL:... where person.age not in ?1

关键字 True

例子:findByActiveTrue()
JPQL:... where person.active = true

关键字 False

例子:findByActiveFalse()
JPQL:... where person.active = false

关键字 IgnoreCase

例子:findByFirstnameIgnoreCase
JPQL:... where UPPER(person.firstname) = UPPER(?1)

关键字混合

JPA 查询关键子可以自由组合,例如:

  • findByFirstnameAndLastnameAndAge
  • findByFirstnameOrLastnameOrAge
  • findByLastnameOrAgeAndActiveTrue
  • findByLastnameAndAgeNot
  • findByLastNameAndActiveTrueOrderByLastnameAsc

飞鱼原创 https://ffish.net,未经允许,严禁转载!