基于JPA的Repository使用詳解
Spring Data JPA
Spring Data是Spring提供的操作數(shù)據(jù)的框架,Spring Data JPA是Spring Data的一個(gè)模塊,通過(guò)Spring data 基于jpa標(biāo)準(zhǔn)操作數(shù)據(jù)的模塊。
Spring Data的核心能力,就是基于JPA操作數(shù)據(jù),并且可以簡(jiǎn)化操作持久層的代碼。
它使用一個(gè)叫作Repository的接口類為基礎(chǔ),它被定義為訪問(wèn)底層數(shù)據(jù)模型的超級(jí)接口。而對(duì)于某種具體的數(shù)據(jù)訪問(wèn)操作,則在其子接口中定義。
Spring Data可以讓我們只定義接口,只要遵循spring data的規(guī)范,就無(wú)需寫(xiě)實(shí)現(xiàn)類,不用寫(xiě)sql語(yǔ)句直接查詢數(shù)據(jù)。
Repository

Repository
提供了findBy + 屬性 方法
CrudRepository
繼承了Repository 提供了對(duì)數(shù)據(jù)的增刪改查
PagingAndSortRepository
繼承了CrudRepository 提供了對(duì)數(shù)據(jù)的分頁(yè)和排序,缺點(diǎn)是只能對(duì)所有的數(shù)據(jù)進(jìn)行分頁(yè)或者排序,不能做條件判斷
JpaRepository: 繼承了PagingAndSortRepository
開(kāi)發(fā)中經(jīng)常使用的接口,主要繼承了PagingAndSortRepository,對(duì)返回值類型做了適配
JpaSpecificationExecutor
提供多條件查詢
CrudRepository
CrudRepository繼承Repository,添加了一組對(duì)數(shù)據(jù)的增刪改查的方法

PagingAndSortingRepository
PagingAndSortingRepository繼承CrudRepository,添加了一組分頁(yè)排序相關(guān)的方法

JpaRepository
JpaRepository繼承PagingAndSortingRepository,添加了一組JPA規(guī)范相關(guān)的方法。對(duì)繼承父接口中方法的返回值進(jìn)行了適配,因?yàn)樵诟割惤涌谥型ǔ6挤祷氐?,需要我們自己進(jìn)行強(qiáng)制類型轉(zhuǎn)化。而在JpaRepository中,直接返回了List。
開(kāi)發(fā)中最常用JpaRepository

JpaSpecificationExecutor
這個(gè)接口比較特殊,單獨(dú)存在,沒(méi)有繼承以上接口。主要提供了多條件查詢的支持,并且可以在查詢中添加分頁(yè)和排序。因?yàn)檫@個(gè)接口單獨(dú)存在,因此需要配合以上說(shuō)的接口使用。

JpaRepository查詢功能
Jpa方法命名規(guī)則
JpaRepository支持接口規(guī)范方法名查詢。意思是如果在接口中定義的查詢方法符合它的命名規(guī)則,就可以不用寫(xiě)實(shí)現(xiàn),目前支持的關(guān)鍵字如下。
| Keyword | Sample | JPQL snippet |
|---|---|---|
| And | findByNameAndPwd | where name= ? and pwd =? |
| Or | findByNameOrSex | where name= ? or sex=? |
| Is,Equals | findById,findByIdEquals | where id= ? |
| Between | findByIdBetween | where id between ? and ? |
| LessThan | findByIdLessThan | where id < ? |
| LessThanEquals | findByIdLessThanEquals | where id <= ? |
| GreaterThan | findByIdGreaterThan | where id > ? |
| GreaterThanEquals | findByIdGreaterThanEquals | where id > = ? |
| After | findByIdAfter | where id > ? |
| Before | findByIdBefore | where id < ? |
| IsNull | findByNameIsNull | where name is null |
| isNotNull,NotNull | findByNameNotNull | where name is not null |
| Like | findByNameLike | where name like ? |
| NotLike | findByNameNotLike | where name not like ? |
| StartingWith | findByNameStartingWith | where name like ‘?%' |
| EndingWith | findByNameEndingWith | where name like ‘%?' |
| Containing | findByNameContaining | where name like ‘%?%' |
| OrderBy | findByIdOrderByXDesc | where id=? order by x desc |
| Not | findByNameNot | where name <> ? |
| In | findByIdIn(Collection<?> c) | where id in (?) |
| NotIn | findByIdNotIn(Collection<?> c) | where id not in (?) |
| True | findByAaaTue | where aaa = true |
| False | findByAaaFalse | where aaa = false |
| IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |
| top | findTop10 | top 10/where ROWNUM <=10 |
使用方法
使用時(shí)自定義接口繼承JpaRepository,傳入泛型,第一個(gè)參數(shù)為要操作的實(shí)體類,第二個(gè)參數(shù)為該實(shí)體類的主鍵類型
public interface SpuRepository extends JpaRepository<Spu, Long> {
Spu findOneById(Long id);
Page<Spu> findByCategoryIdOrderByCreateTimeDesc(Long cid, Pageable pageable);
Page<Spu> findByRootCategoryIdOrderByCreateTime(Long cid, Pageable pageable);
}
解析過(guò)程
1. Spring Data JPA框架在進(jìn)行方法名解析時(shí)
會(huì)先把方法名多余的前綴截取掉,比如find,findBy,read,readBy,get,getBy,然后對(duì)剩下的部分進(jìn)行解析。
2. 假設(shè)創(chuàng)建如下查詢findByCategoryId()
框架在解析該方法時(shí),首先剔除findBy,然后對(duì)剩下的屬性進(jìn)行解析,假設(shè)查詢實(shí)體為Spu
(1) 先判斷categoryId(根據(jù)POJO 規(guī)范,首字母變?yōu)樾?xiě))是否為查詢實(shí)體的一個(gè)屬性,如果是,則表示根據(jù)該屬性進(jìn)行查詢;如果沒(méi)有該屬性,繼續(xù)第二步;
(2) 從右往左截取第一個(gè)大寫(xiě)字母開(kāi)頭的字符串此處為Id),然后檢查剩下的字符串是否為查詢實(shí)體的一個(gè)屬性,如果是,則表示根據(jù)該屬性進(jìn)行查詢;
如果沒(méi)有該屬性,則重復(fù)第二步,繼續(xù)從右往左截??;最后假設(shè)user為查詢實(shí)體的一個(gè)屬性;
(3) 接著處理剩下部分(CategoryId),先判斷用戶所對(duì)應(yīng)的類型是否有categoryId屬性,如果有,則表示該方法最終是根據(jù)"Spu.categoryId"的取值進(jìn)行查詢;
否則繼續(xù)按照步驟2的規(guī)則從右往左截取。
(4) 可能會(huì)存在一種特殊情況,比如Spu包含一個(gè)categoryId 的屬性,也有一個(gè) rootCategoryId屬性,此時(shí)會(huì)存在混淆??梢悦鞔_在屬性之間加上"_" 以顯式表達(dá)意圖,比如 "findByRoot_CategoryId()"
3. 特殊參數(shù)
可以直接在方法的參數(shù)上加入分頁(yè)或排序的參數(shù),比如:
Page<Spu> findByCategoryId(Long cid, Pageable pageable); List<Spu> findByCategoryId(Long cid, Sort sort);
4. JPA的@NamedQueries
(1) 在實(shí)體類上使用@NamedQuery
@NamedQuery(name = "Spu.findByRootCategoryId",query = "select s from Spu s where s.rootCategoryId >= ?1")
(2) 在自己實(shí)現(xiàn)的DAO的Repository接口里面定義一個(gè)同名的方法,示例如下:
public List<Spu> findByRootCategoryId(Long rootCategoryId);
(3) 然后Spring會(huì)先找是否有同名的NamedQuery,如果有,那么就不會(huì)按照接口定義的方法來(lái)解析。
5. 使用@Query
在方法上標(biāo)注@Query來(lái)指定本地查詢
參數(shù)nativeQuery默認(rèn)為false,nativeQuery=false時(shí),value參數(shù)寫(xiě)的是JPQL,JPQL是用來(lái)操作model對(duì)象的
@Query(value="select s from Spu s where s.title like %?1" ) public List<Spu> findByTitle(String title);
nativeQuery=true時(shí),value參數(shù)寫(xiě)的是原生sql
@Query(value="select * from spu s where s.title like %?1",nativeQuery=true ) public List<Spu> findByTitle(String title);
6. 使用@Param命名化參數(shù)
@Query(value = "select s from Spu s where s.title in (:titles)")
List<Spu> findByTitle(@Param("titles") List<String> titles);
JPA自定義Repository方法
如果不使用SpringData的方法,想要自己實(shí)現(xiàn),該怎么辦呢?
定義一個(gè)接口:聲明要添加的, 并自實(shí)現(xiàn)的方法
提供該接口的實(shí)現(xiàn)類:類名需在要聲明的 Repository 后添加 Impl, 并實(shí)現(xiàn)方法
聲明 Repository 接口, 并繼承 1) 聲明的接口
注意:默認(rèn)情況下, Spring Data 會(huì)在 base-package 中查找 "接口名Impl" 作為實(shí)現(xiàn)類. 也可以通過(guò) repository-impl-postfix 聲明后綴.
這張圖是類與接口之間的關(guān)系

下面是具體的實(shí)現(xiàn)
包結(jié)構(gòu)

類與接口之間的關(guān)系代碼
public interface PersonRepositoiry extends JpaRepository<Person, Integer> ,PersonDao{
public interface PersonDao {
void test();
}
@Repository
public class PersonRepositoiryImpl implements PersonDao{
@PersistenceContext
private EntityManager em;
@Override
public void test() {
//只是用來(lái)測(cè)試
Person person = em.find(Person.class, 1);
System.out.println(person);
}
}
測(cè)試代碼
@Test
public void testCustomerRepositoryMethod() {
personRepositoiry.test();
}
經(jīng)過(guò)實(shí)踐發(fā)現(xiàn)
XXXRepositoryImpl 與XXXRepository前面的名字必須相同,后面的也需要按照規(guī)則寫(xiě)
若將XXXRepositoryImpl與XXXRepository接口放在同意包下,XXXRepositoryImpl不需要添加@Repository注解,但是當(dāng)XXXRepositoryImpl與XXXRepository接口不在同一包下,需要在在XXXRepositoryImpl類上加@Repository注解進(jìn)行修飾
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(44)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07
SpringBoot敏感數(shù)據(jù)脫敏的處理方式
在Spring Boot中處理敏感數(shù)據(jù)脫敏,可以通過(guò)以下幾種方式實(shí)現(xiàn),確保敏感信息在接口返回、日志輸出、數(shù)據(jù)庫(kù)存儲(chǔ)等環(huán)節(jié)得到保護(hù),文中通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2025-03-03
spring 定時(shí)任務(wù)@Scheduled詳解
這篇文章主要介紹了spring 定時(shí)任務(wù)@Scheduled的相關(guān)資料,文中通過(guò)示例代碼介紹的很詳細(xì),相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2017-01-01
springMVC如何將controller中數(shù)據(jù)傳遞到j(luò)sp頁(yè)面
這篇文章主要介紹了springMVC如何將controller中數(shù)據(jù)傳遞到j(luò)sp頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
springboot整合mybatis-plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼
這篇文章主要介紹了springboot整合mybatis-plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
SpringBoot?使用定時(shí)任務(wù)(SpringTask)的詳細(xì)步驟
Cron?表達(dá)式非常靈活,可以滿足各種定時(shí)任務(wù)的需求,但需要注意的是,Cron?表達(dá)式只能表示固定的時(shí)間點(diǎn),無(wú)法處理復(fù)雜的時(shí)間邏輯,本文給大家介紹SpringBoot?使用定時(shí)任務(wù)(SpringTask)的詳細(xì)步驟,感興趣的朋友一起看看吧2024-02-02
SpringCloud Gateway鑒權(quán)和跨域解決方案
網(wǎng)關(guān)是介于客戶端和服務(wù)器端之間的中間層,所有的外部請(qǐng)求都會(huì)先經(jīng)過(guò) 網(wǎng)關(guān)這一層,也就是說(shuō),API 的實(shí)現(xiàn)方面更多的考慮業(yè)務(wù)邏輯,而安全、性能、監(jiān)控可以交由 網(wǎng)關(guān)來(lái)做,這樣既提高業(yè)務(wù)靈活性又不缺安全性,本文給大家介紹SpringCloud Gateway鑒權(quán)和跨域解決方案,一起看看吧2023-11-11
SpringCloud Feign多參數(shù)傳遞及需要注意的問(wèn)題
這篇文章主要介紹了SpringCloud Feign多參數(shù)傳遞及需要注意的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

