SpringData--簡介(一)

一、SpringData簡介

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
??Spring Data是一個用于簡化數據庫訪問,并支持云服務的開源框架。其主要目標是使得對數據的訪問變得方便快捷。
??可以極大的簡化JPA的寫法,可以在幾乎不用寫實現的情況下,實現對數據的訪問和操作。除了CRUD外,還包括如分頁、排序等一些常用的功能。

主要來看看Spring Data JPA提供的接口,也是Spring Data JPA的核心概念:

1:Repository:最頂層的接口,是一個空的接口,目的是為了統一所有Repository的類型,且能讓組件掃描的時候自動識別。
    2:CrudRepository :是Repository的子接口,提供CRUD的功能
    3:PagingAndSortingRepository:是CrudRepository的子接口,添加分頁和排序的功能
    4:JpaRepository:是PagingAndSortingRepository的子接口,增加了一些實用的功能,比如:批量操作等。
    5:JpaSpecificationExecutor:用來做負責查詢的接口
    6:Specification:是Spring Data JPA提供的一個查詢規范,要做復雜的查詢,只需圍繞這個規范來設置查詢條件即可

特征

1:強大的存儲庫和自定義對象映射抽象
2:從存儲庫方法名稱中進行動態查詢導出
3:實現域基類提供基本屬性
4:支持透明審核(創建,最后更改)
5:集成自定義存儲庫代碼的可能性
6:Easy Spring通過JavaConfig和自定義XML命名空間進行集成
7:與Spring MVC控制器進行高級集成
8:跨店存儲的實驗支持

二、Maven依賴

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

三、繼承接口

Repository
public interface GatewayRouteRepository extends JpaRepository<GatewayRoute, String> , JpaSpecificationExecutor<GatewayRoute> {

}

四、Spring Data Repository 查詢方法定義規范

簡單查詢條件 :查詢某一個實體類或是集合
在Repository 子接口中聲明方法:
①、不是隨便聲明的,而需要符合一定的規范
②、查詢方法以 find | read | get 開頭
③、涉及條件查詢時,條件的屬性用條件關鍵字連接
④、要注意的是:條件屬性以首字母大寫
⑤、支持屬性的級聯查詢。若當前類有符合條件的屬性,則優先使用,而不使用級聯屬性。若需要使用級聯屬性,則屬性之間使用_連接

spring data支持的關鍵字

關鍵字 方法命名 sql where字句
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(?)
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容