欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于JPA的Repository使用詳解

 更新時間:2021年11月25日 16:04:44   作者:三毛村滴雪魚粉  
這篇文章主要介紹了JPA的Repository使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Spring Data JPA

Spring Data是Spring提供的操作數(shù)據(jù)的框架,Spring Data JPA是Spring Data的一個模塊,通過Spring data 基于jpa標準操作數(shù)據(jù)的模塊。

Spring Data的核心能力,就是基于JPA操作數(shù)據(jù),并且可以簡化操作持久層的代碼。

它使用一個叫作Repository的接口類為基礎,它被定義為訪問底層數(shù)據(jù)模型的超級接口。而對于某種具體的數(shù)據(jù)訪問操作,則在其子接口中定義。

Spring Data可以讓我們只定義接口,只要遵循spring data的規(guī)范,就無需寫實現(xiàn)類,不用寫sql語句直接查詢數(shù)據(jù)。

Repository

在這里插入圖片描述

  • Repository

提供了findBy + 屬性 方法

  • CrudRepository

繼承了Repository 提供了對數(shù)據(jù)的增刪改查

  • PagingAndSortRepository

繼承了CrudRepository 提供了對數(shù)據(jù)的分頁和排序,缺點是只能對所有的數(shù)據(jù)進行分頁或者排序,不能做條件判斷

  • JpaRepository: 繼承了PagingAndSortRepository

開發(fā)中經(jīng)常使用的接口,主要繼承了PagingAndSortRepository,對返回值類型做了適配

  • JpaSpecificationExecutor

提供多條件查詢

CrudRepository

CrudRepository繼承Repository,添加了一組對數(shù)據(jù)的增刪改查的方法

在這里插入圖片描述

PagingAndSortingRepository

PagingAndSortingRepository繼承CrudRepository,添加了一組分頁排序相關的方法

在這里插入圖片描述

JpaRepository

JpaRepository繼承PagingAndSortingRepository,添加了一組JPA規(guī)范相關的方法。對繼承父接口中方法的返回值進行了適配,因為在父類接口中通常都返回迭代器,需要我們自己進行強制類型轉化。而在JpaRepository中,直接返回了List。

開發(fā)中最常用JpaRepository

在這里插入圖片描述

JpaSpecificationExecutor

這個接口比較特殊,單獨存在,沒有繼承以上接口。主要提供了多條件查詢的支持,并且可以在查詢中添加分頁和排序。因為這個接口單獨存在,因此需要配合以上說的接口使用。

在這里插入圖片描述

JpaRepository查詢功能

Jpa方法命名規(guī)則

JpaRepository支持接口規(guī)范方法名查詢。意思是如果在接口中定義的查詢方法符合它的命名規(guī)則,就可以不用寫實現(xià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

使用方法

使用時自定義接口繼承JpaRepository,傳入泛型,第一個參數(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);
}

解析過程

1. Spring Data JPA框架在進行方法名解析時

會先把方法名多余的前綴截取掉,比如find,findBy,read,readBy,get,getBy,然后對剩下的部分進行解析。

2. 假設創(chuàng)建如下查詢findByCategoryId()

框架在解析該方法時,首先剔除findBy,然后對剩下的屬性進行解析,假設查詢實體為Spu

(1) 先判斷categoryId(根據(jù)POJO 規(guī)范,首字母變?yōu)樾懀┦欠駷椴樵儗嶓w的一個屬性,如果是,則表示根據(jù)該屬性進行查詢;如果沒有該屬性,繼續(xù)第二步;

(2) 從右往左截取第一個大寫字母開頭的字符串此處為Id),然后檢查剩下的字符串是否為查詢實體的一個屬性,如果是,則表示根據(jù)該屬性進行查詢;

如果沒有該屬性,則重復第二步,繼續(xù)從右往左截取;最后假設user為查詢實體的一個屬性;

(3) 接著處理剩下部分(CategoryId),先判斷用戶所對應的類型是否有categoryId屬性,如果有,則表示該方法最終是根據(jù)"Spu.categoryId"的取值進行查詢;

否則繼續(xù)按照步驟2的規(guī)則從右往左截取。

(4) 可能會存在一種特殊情況,比如Spu包含一個categoryId 的屬性,也有一個 rootCategoryId屬性,此時會存在混淆。可以明確在屬性之間加上"_" 以顯式表達意圖,比如 "findByRoot_CategoryId()"

3. 特殊參數(shù)

可以直接在方法的參數(shù)上加入分頁或排序的參數(shù),比如:

Page<Spu> findByCategoryId(Long cid, Pageable pageable);
List<Spu> findByCategoryId(Long cid, Sort sort);

4. JPA的@NamedQueries

(1) 在實體類上使用@NamedQuery

@NamedQuery(name = "Spu.findByRootCategoryId",query = "select s from Spu s where s.rootCategoryId >= ?1")

(2) 在自己實現(xiàn)的DAO的Repository接口里面定義一個同名的方法,示例如下:

public List<Spu> findByRootCategoryId(Long rootCategoryId);

(3) 然后Spring會先找是否有同名的NamedQuery,如果有,那么就不會按照接口定義的方法來解析。

5. 使用@Query

在方法上標注@Query來指定本地查詢

參數(shù)nativeQuery默認為false,nativeQuery=false時,value參數(shù)寫的是JPQL,JPQL是用來操作model對象的

@Query(value="select s from Spu s where s.title like %?1" )
public List<Spu> findByTitle(String title);

nativeQuery=true時,value參數(shù)寫的是原生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的方法,想要自己實現(xiàn),該怎么辦呢?

定義一個接口:聲明要添加的, 并自實現(xiàn)的方法

提供該接口的實現(xiàn)類:類名需在要聲明的 Repository 后添加 Impl, 并實現(xiàn)方法

聲明 Repository 接口, 并繼承 1) 聲明的接口

注意:默認情況下, Spring Data 會在 base-package 中查找 "接口名Impl" 作為實現(xiàn)類. 也可以通過 repository-impl-postfix 聲明后綴.

這張圖是類與接口之間的關系

下面是具體的實現(xià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() {
  //只是用來測試
  Person person = em.find(Person.class, 1);
  System.out.println(person);
 } 
}

測試代碼

@Test
 public void testCustomerRepositoryMethod() {
  personRepositoiry.test();
 }

經(jīng)過實踐發(fā)現(xiàn)

XXXRepositoryImpl 與XXXRepository前面的名字必須相同,后面的也需要按照規(guī)則寫

若將XXXRepositoryImpl與XXXRepository接口放在同意包下,XXXRepositoryImpl不需要添加@Repository注解,但是當XXXRepositoryImpl與XXXRepository接口不在同一包下,需要在在XXXRepositoryImpl類上加@Repository注解進行修飾

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論