目錄
Spring Data 概述
JPA Spring Data 概述
Spring Data JPA HelloWorld
Repository 接口
Repository 子接口
Spring Data Repository 查詢方法定義規範
spring data 支援的關鍵字
級聯查詢
Spring Data 概述
Spring Data: Spring 的一個子項目。用于簡化資料庫通路,支援NoSQL和關系資料庫存儲。其主要目标是使資料庫的通路變得友善快捷。
Spring Data 項目所支援NoSQL存儲:
- - MongoDB(文檔資料庫)
- - Neo4j (圖形資料庫)
- - Redis(鍵/值存儲)
- - Hbase(列族資料庫)
Spring Data 項目所支援的關系資料存儲技術:
- - JDBC
- - JPA
JPA Spring Data 概述
JPA Spring Data: 緻力于減少資料通路層(DAO)的開發量。開發者唯一要做的,就隻是聲明持久層的接口,其他都交給Spring Data JPA 來幫你完成!
架構怎麼可能代替開發者實作業務邏輯呢?比如:當有一個UserDao.findUserById()這樣一個方法聲明,大緻應該能判斷出這是根據給定條件的ID查詢出滿足條件的User對象。Spring Data JPA 做的便是規範方法的名字,根據符合規範的名字來确定方法需要實作什麼樣的邏輯。
Spring Data JPA HelloWorld
使用Spring Data JPA進行持久層開發需要的四個步驟:
- 配置Spring 整合 JPA
- 在Spring 配置檔案中配置Spring Data ,讓Spring 為聲明的接口建立代理對象。配置了<jpa:repositories>後,Spring 初始化容器将會掃描base-package 指定的包目錄及其子目錄,為繼承Repository 或其子接口的接口建立代理對象,并将代理對象注冊為Spring Bean ,業務層便可以通過Spring自動封裝的特性來直接使用該對象。
- 聲明持久層的接口,該接口繼承 Repository ,Repository是一個标記型接口,它不包含任何方法,如必要,Spring Data 可實作Repository其他子接口,其中定義了一些常用的增删改查,以及分頁相關的方法。
- 在接口中聲明需要的方法。Spring Data 将根據給定的政策(具體政策稍後講解)來為其生成實作代碼。
Repository 接口
1、Repository是一個空接口,即是一個标記接口
2、若我們定義的接口繼承了Repository,則該接口會被IOC容器識别為一個Repository Bean 納入到IOC容器中,進而可以在該接口中定義滿足一定規範的方法
3、實際上,也可以通@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class) 注解來代替 extends Repository<Person, Integer>
Repository 子接口
在eclipse 中選中Repository ,Ctrl + T ,有多個子接口和實作類
繼承關系如下:
①、Repository :僅僅是一個辨別,表名任何繼承它的均為倉庫接口類
②、CurdRepository : 繼承Repository ,實作了一組CURD相關的方法
③、PagingAndSortingRepository :繼承CurdRepository ,實作了一組分頁排序相關的方法
④、JpaRepository : 繼承PagingAndSortingRepository ,實作一組Jpa 規範相關的方法
⑤、自定義 的 XXXRepository :需要繼承JpaRepository ,這樣的XxxRepository 接口就具備了通用的資料通路控制層的能力。
⑥、JpaSpecificationExecutor :不屬于Repository 體系,實作一組JpaCriteria 查詢相關的方法
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(?) |
級聯查詢
1、建立address 表
2、在person實體類中 添加屬性
3、PersonRepository 中
//where address.id >?
List<Person> getByAddressIdGreaterThan(Integer id);
4、測試類裡面進行測試
①、先要測試是否生成資料表
//生成資料表
@Test
public void testJpa(){
}
②、測試查詢
//級聯查詢
@Test
public void testWords2(){
List<Person> persons = personRepository.getByAddressIdGreaterThan(1);
System.out.println(persons);
}
③、結果 為[ ] 是因為 資料表裡沒有資料,隻有字段
如若:
person實體類中本來就含有一個address_id
在執行級聯查詢的方法,會報錯如下:
在映射的時候列名重複
更改如下:
運作結果:
含義:支援屬性的級聯查詢。若目前類有符合條件的屬性,則優先使用,而不使用級聯屬性。若需要使用級聯屬性,
則屬性之間使用_連接配接
//where address.id >?
List<Person> getByAddress_IdGreaterThan(Integer id);
查詢結果: 連表查詢
以上是從某視訊處所學!