Spring?Data?JPA查詢方式及方法名查詢規(guī)則介紹
Spring Data JPA查詢方式及方法名查詢規(guī)則
Spring Data JPA
通過解析方法名創(chuàng)建查詢
在執(zhí)行查詢時,Spring Data JPA框架會把方法名進行解析,解析到前綴比如 get、getBy、find、findBy、read、readBy時,會先把這些前綴截取掉,然后對剩下部分進行解析,剩下部分分為兩種:一是只有屬性名,二是屬性名+條件;條件很好解析,解析的關(guān)鍵在于屬性名,下面拿一個具體的例子來幫助大家更好的理解屬性名解析規(guī)則。
解析規(guī)則例子:比如實體為Product,方法為findByGoodsTypeDetail ();
1、首先截取掉 findBy,然后對剩下的屬性進行解析;
2、先判斷 goodsTypeDetail(根據(jù) POJO 規(guī)范,首字母變?yōu)樾?,下同)是否?Product的一個屬性,如果是,則表示根據(jù)該屬性進行查詢;如果沒有該屬性,繼續(xù)第三步;
3、從右往左截取第一個大寫字母開頭的字符串(本方法為 Detail),然后對比剩下的字符串(本方法為goodsType)是否為 Product的一個屬性,如果是,則表示根據(jù)該屬性進行查詢;如果沒有該屬性,則重復(fù)第三步,繼續(xù)從右往左截?。ù颂帪門ypeDetail,剩下goods),就這樣一直循環(huán)到最終;假設(shè) goods為 Product的一個屬性,則說明goods不是常量類型,而是一個對象類型;
4、此時剩下字符串 TypeDetail,先判斷goods對象中是否有 typeDetail屬性,如果有,則表示該方法最終是根據(jù) "Product.goods.typeDetail" 的值進行查詢;如果沒有該屬性,則繼續(xù)按照第三步的規(guī)則從右往左截取,最終表示根據(jù) "Product.goods.type.detail" 的值進行查詢。
不過這種解析規(guī)則不是完美的,也存在bug,不注意可能會掉到這個坑里,比如Product中有一個屬性叫g(shù)oods,同時還有一個屬性叫g(shù)oodsType,這時在解析時會出現(xiàn)混亂,不過可以在屬性之間加上 "_"來解決這個問題,注意:"_"是加在查詢方法上的,不是加在屬性名上的;比如 "findByGoods_TypeDetail()" (當Product中不存在goods_TypeDetail時,是給解析器說明Goods為一個對象)或"findByGoodsType_Detail()"(當Product中不存在goodsType_Detail時,是給解析器說明GoodsType為一個對象)。
查詢時,很多時候需要同時使用多個屬性進行查詢,而且查詢的條件也各不相同,Spring Data JPA 為此提供了一些條件查詢的關(guān)鍵字,我把常用的都整理了一下,如下表:
|
關(guān)鍵字 |
對應(yīng)SQL關(guān)鍵字 |
示例 |
|
列名 |
根據(jù)列名查詢 |
findByName(String name);自動解析findBy后面的列名,然后根據(jù)列名查詢。 |
|
In |
等價于SQL 中的 in |
findByNameIn(Collection<String> nameList) ;參數(shù)可以是集合、數(shù)組、不定長參數(shù); |
|
Like |
等價于SQL 中的 like |
findByNameLike(String name); |
| NotLike | 等價于SQL 中的 not like | findByNameNotLike(String name); |
|
And |
等價于SQL 中的 and |
findByNameAndPwd(String name, String pwd); |
|
Or |
等價于SQL 中的 or |
findByIdOrCode(String id, String code); |
|
Between |
等價于SQL 中的 between |
findByNumBetween(int max, int min); |
|
OrderBy |
等價于SQL 中的 order by |
findByNameOrderByNumAsc(String name); |
|
IsNull |
等價于SQL 中的 is null |
findByNameIsNull(); |
| IsNotNull | 等價于SQL 中的 is not null | findByNameIsNotNull(); |
| NotNull | 等價于SQL 中的 is not null | findByNameNotNull();--和IsNotNull 一樣,建議使用IsNotNull |
| Not | 等價于SQL 中的 ! = | findByNameNot(String name); |
| NotIn | 等價于SQL 中的 not in | findByNameNotIn(Collection<String> nameList) ;參數(shù)可以是集合、數(shù)組、不定長參數(shù); |
| LessThan | 等價于SQL 中的 < | findByNumLessThan(int num); |
|
GreaterThan |
等價于SQL 中的 > |
findByNumGreaterThan(int num); |
使用 @Query 創(chuàng)建查詢
1、使用 @Query 提供的位置編號查詢:格式為":位置編號",然后方法中的參數(shù)按 JPQL 查詢語句的位置編號順序書寫。 如下:
public interface ProductDao extends Repository<Product , Long> {
@Query("select * from Product p where p.id= ?1")
public Product findById(Long id);
@Query("select * from Product p where p.type = ?1 and p.name =?2")
public Page<Product> findByTypeAndName(
Integer type,String name,Pageable pageable);
}
2、使用@Query 命名參數(shù)查詢:格式為": 變量",同時在方法的參數(shù)前面使用 @Param 將方法參數(shù)與JPQL中的命名參數(shù)對應(yīng)。如下:
public interface ProductDao extends Repository<Product , Long> {
@Query("from Product p where p.goodsName= :name")
public Product findByGoodsName(@Param("name")String name);
@Query("from Product p where p.num < :num")
public Page<Product> findByNumLessThan(
@Param("num")Integer num,Pageable pageable);
}
3、 使用 @Modifying 將查詢操作標識為更新操作:在使用 @Query 的同時使用 @Modifying ,這樣會生成一個更新的操作,而非查詢。如下:
@Query("update Product p set p.name = ?1 where p.id = ?2")
@Modifying
public int updateName(String name, int id);
JPA 常用查詢方法記錄
以這張表為例:
+-------------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+-------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | role | varchar(45) | NO | | NULL | | | permissions | varchar(512) | NO | | NULL | | | create_time | datetime | NO | | CURRENT_TIMESTAMP | | | status | varchar(45) | NO | | NULL | | | role_name | varchar(45) | NO | | NULL | | +-------------+--------------+------+-----+-------------------+----------------+
CrudRepository 默認帶的查詢方法

@Repository
public interface RoleRepository extends CrudRepository<RoleData, Integer> {
}
@Entity
@Table(name = "role", catalog = "message_push")
public class RoleData implements java.io.Serializable {
@Id
@GeneratedValue
private Integer id;
private String role;
private String permissions;
private Long create_time;
private Integer status;
// getter setter 構(gòu)造函數(shù)從略
}
簡單的擴展-以字段為關(guān)鍵字進行查詢
list<RoleData> findByXXX(xxx) 其中 XXX 對應(yīng)數(shù)據(jù)庫中的字段,例如:
@Repository
public interface RoleRepository extends CrudRepository<RoleData, Integer> {
List<RoleData> findByRole(String role);
List<RoleData> findByStatus(String status);
}
還可以多字段AND 查詢:
@Repository
public interface RoleRepository extends CrudRepository<RoleData, Integer> {
List<RoleData> findByRoleAndStatus(String role, String status);
}
在 application.properties 中加入以下配置 spring.jpa.show-sql=true 可以看到SQL語句:
Hibernate: select roledata0_.id as id1_0_, roledata0_.create_time as create_t2_0_, roledata0_.permissions as permissi3_0_, roledata0_.role as role4_0_, roledata0_.status as status5_0_ from message_push.role roledata0_ where roledata0_.role=? and roledata0_.status=?
當然 or 也是可以:
List<RoleData> findByRoleOrStatus(String role, String status);
Hibernate: select roledata0_.id as id1_0_, roledata0_.create_time as create_t2_0_, roledata0_.permissions as permissi3_0_, roledata0_.role as role4_0_, roledata0_.status as status5_0_ from message_push.role roledata0_ where roledata0_.role=? or roledata0_.status=?
使用@Query 進行復(fù)雜查詢
例如:
@Query(value = "select * from role where role = ?1", nativeQuery = true) List<RoleData> searchByRole(String role);
或 sql in 用法
@Query(value = "select * from role where role in (?1) and status = 'valid'", nativeQuery = true) List<RoleData> searchByRoleList(List<String> targetList);
又或 sql like 用法:
@Query(value = "select * from role where role like %?1%", nativeQuery = true) List<RoleData> searchByRole(String keyWord);
使用 Specification 進行復(fù)雜查詢
先來看一下 JpaSpecificationExecutor 接口

以 findAll(Specification<T>) 為例進行說明:
Specification<T> 可以理解為一個查詢條件。findAll 以這個條件為基準進行查詢,也就是我們在sql 里寫的 whre xxx 轉(zhuǎn)為 Specification 來寫。
首先要讓我們的 repository 繼承 JpaSpecificationExecutor
@Repository
public interface RoleRepository extends CrudRepository<RoleData, Integer>, JpaSpecificationExecutor<RoleData> {
接下來,將這個查詢 [ select * from role where role like '%a%' ] 轉(zhuǎn)為一個簡單的 Specification。
final Specification<RoleData> spec = new Specification<RoleData> () {
@Override
public Predicate toPredicate(Root<RoleData> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
Predicate predicate = criteriaBuilder.like(root.get("role"), "%a%");
return predicate;
}
};
然后直接按如下方式調(diào)用即可:
roleRepository.findAll(spec);
Specification 里又衍生出了好幾個類,分別介紹一下:
Predicate
因為我們實現(xiàn) Specification 接口時,只需要實現(xiàn) Predicate toPredicate() 方法。而 Specification 上文中我們當做搜索條件來理解了,那么也可以簡單的把 Predicate 視為搜索條件。
CriteriaBuilder
用于構(gòu)建搜索條件 Predicater 的。
回想一下SQL搜索條件怎么寫
where attribute = xx where attribute > xx where attribute < xx where attribute like %xx%
注意這里有三要素:
- attribute 搜索指定的數(shù)據(jù)庫字段
- 操作符 大于 小于 等于
- 具體數(shù)據(jù)
CriteriaBuilder提供了一系列靜態(tài)方法構(gòu)建這三要素。
比如
- CriteriaBuilder.like(數(shù)據(jù)庫字段, 具體數(shù)據(jù))
- CriteriaBuilder.equal(數(shù)據(jù)庫字段, 具體數(shù)據(jù))
其中 數(shù)據(jù)庫字段 不能直接寫字符串,需要下一個工具類 Root 的 get 方法獲取。
Root
root.get( String attributeName ) 參數(shù) attributeName 就是數(shù)據(jù)庫里的字段名
現(xiàn)在相信讀者可以理解 我們剛才寫的 那個完整的 Specification了。
再下來再上一個稍微復(fù)雜點的例子:
[ select * from role where role like '%a%' and (id > 11 or id < 8) ]
final Specification<RoleData> spec = new Specification<RoleData> () {
@Override
public Predicate toPredicate(Root<RoleData> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
Predicate roleLikeaPredicate = criteriaBuilder.like(root.get("role"), "%a%");
Predicate idLessThan8Predicate = criteriaBuilder.lessThan(root.get("id"), 8);
Predicate idGreaterThan12Predicate = criteriaBuilder.greaterThan(root.get("id"), 11);
Predicate idCombindedPredicate = criteriaBuilder.or(idLessThan8Predicate, idGreaterThan12Predicate);
Predicate predicate = criteriaBuilder.and(idCombindedPredicate, roleLikeaPredicate);
return predicate;
}
};
其實也很簡單,就是多了 criteriaBuilder.or criteriaBuilder.and 來把多個 Predicate 合成一個新的 Predicate
最后一個例子:
可以通過root.get(xx).in(List<> list) 也是可以直接返回 Predicate 的
final Specification<RoleData> spec2 = new Specification<RoleData> () {
@Override
public Predicate toPredicate(Root<RoleData> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<String> alist = new ArrayList<String>();
alist.add("admin");
Predicate predicate = root.get("role").in(alist);
return predicate;
}
};
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項
這篇文章主要給大家介紹了關(guān)于Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring Cloud具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能
這篇文章主要介紹了Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能,使用步驟是先創(chuàng)建一個線程池的配置,讓Spring Boot加載,用來定義如何創(chuàng)建一個ThreadPoolTaskExecutor,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2022-08-08
java?stream實現(xiàn)分組BigDecimal求和以及自定義分組求和
這篇文章主要給大家介紹了關(guān)于java?stream實現(xiàn)分組BigDecimal求和以及自定義分組求和的相關(guān)資料,Stream是Java8的一大亮點,是對容器對象功能的增強,它專注于對容器對象進行各種非常便利、高效的聚合操作或者大批量數(shù)據(jù)操作,需要的朋友可以參考下2023-12-12
java基于C/S結(jié)構(gòu)實現(xiàn)多線程聊天室
這篇文章主要為大家詳細介紹了java基于C/S結(jié)構(gòu)實現(xiàn)多線程聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
解決springboot項目啟動報錯Field xxxMapper in com...xx
這篇文章主要介紹了解決springboot項目啟動報錯Field xxxMapper in com...xxxContr問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

