芋道 Spring Boot JPA 入門(二)之基於方法名查詢
- 2019 年 12 月 13 日
- 筆記
摘要: 原創出處 http://www.iocoder.cn/Spring-Boot/JPA/ 「芋道源碼」歡迎轉載,保留摘要,謝謝!
- 1. 概述
- 2. 快速入門
- 3. 分頁操作
- 4. 基於方法名查詢
- 5. 基於註解查詢
- 666. 彩蛋
4. 基於方法名查詢
示例程式碼對應倉庫:lab-13-jpa 。
在 Spring Data 中,支援根據方法名作生成對應的查詢(WHERE
)條件,進一步進化我們使用 JPA ,具體是方法名以 findBy
、existsBy
、countBy
、deleteBy
開頭,後面跟具體的條件。具體的規則,在 《Spring Data JPA —— Query Creation》 文檔中,已經詳細提供。如下:
關鍵字 |
方法示例 |
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, Null |
findByAge(Is)Null |
… 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 ages) |
… where x.age in ?1 |
NotIn |
findByAgeNotIn(Collection ages) |
… 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) |
- 注意,如果我們有排序需求,可以使用
OrderBy
關鍵字。
下面,我們來編寫一個簡單的示例。
艿艿:IDEA 牛逼,提供的插件已經能夠自動提示上述關鍵字。太強了~
4.1 UserRepository03
在 cn.iocoder.springboot.lab13.mybatis.repository
包路徑下,創建 UserRepository03 介面。程式碼如下:
// UserRepository03.java public interface UserRepository03 extends PagingAndSortingRepository<UserDO, Integer> { UserDO findByUsername(String username); Page<UserDO> findByCreateTimeAfter(Date createTime, Pageable pageable); }
- 對於分頁操作,需要使用到 Pageable 參數,需要作為方法的最後一個參數。
4.2 簡單測試
創建 UserRepository03Test 測試類,我們來測試一下簡單的 UserRepository03 的每個操作。程式碼如下:
// UserRepository03.java @RunWith(SpringRunner.class) @SpringBootTest public class UserRepository03Test { @Autowired private UserRepository03 userRepository; @Test public void testFindByUsername() { UserDO user = userRepository.findByUsername("yunai"); System.out.println(user); } @Test public void testFindByCreateTimeAfter() { // 創建分頁條件 Pageable pageable = PageRequest.of(1, 10); // 執行分頁操作 Date createTime = new Date(2018 - 1990, Calendar.FEBRUARY, 24); // 臨時 Demo ,實際不建議這麼寫 Page<UserDO> page = userRepository.findByCreateTimeAfter(createTime, pageable); // 列印 System.out.println(page.getTotalElements()); System.out.println(page.getTotalPages()); } }
具體的,胖友可以自己跑跑,妥妥的。