2.6 查询
2.6.1 查询方法
2.6.1.1 推导查询
Spring Data JPA支持方法名来中的属性来推导出查询语句。
public interface PersonRepository extends JpaRepository<Person, Long> {
List<Person> findByNameAndAge(String name, Integer age);
}
根据方法名findByNameAndAge
翻译成查询JPQL语句:
select p from Person where p.name = ?1 and p.age = ?2
方法名中除了属性外的关键字包含:
关键字 | 示例 | JPQL片段 |
---|---|---|
And |
findByNameAndAge |
where p.name = ?1 and p.age = ?2 |
Or |
findByNameOrAge |
where p.name = ?1 or x.age = ?2 |
Is,Equals |
findByName findByNameIs findByNameEquals |
where p.name = ?1 |
Between |
findByCreateTimeBetween |
where p.createTime between ?1 and ?2 |
LessThan |
findByAgeLessThan |
where p.age < ?1 |
LessThanEqual |
findByAgeLessThanEqual |
where p.age <= ?1 |
GreaterThan |
findByAgeGreaterThan |
where p.age > ?1 |
GreaterThanEqual |
findByAgeGreaterThanEqual |
where p.age >= ?1 |
After |
findByCreateTimeAfter |
where p.createTime > ?1 |
Before |
findByCreateTimeBefore |
where p.createTime < ?1 |
IsNull |
findByAgeIsNull |
where p.age is null |
IsNotNull,NotNull |