關(guān)于Spring?Data?Jpa?自定義方法實(shí)現(xiàn)問題
Spring Data Jpa 自定義方法的實(shí)現(xiàn)
最近項(xiàng)目中用到了Spring Data JPA,在里面我繼承了一個(gè)PagingAndSortingRepository的接口,期望的是利用Spring Data JPA提供的便利。
同時(shí)我也希望自己有一個(gè)能定義自己方法的接口,因?yàn)閱渭兛縎pring Data JPA中提供的功能還是有很多業(yè)務(wù)邏輯實(shí)現(xiàn)不了,我必須自己實(shí)現(xiàn)。
那么問題來了:Spring Data JPA好處就是讓我們省去了實(shí)現(xiàn)接口的過程,按照他們給的命名規(guī)范他們會(huì)自動(dòng)實(shí)現(xiàn)我們的業(yè)務(wù)邏輯,那我們自己實(shí)現(xiàn)的接口要怎么注入到其中呢?
上網(wǎng)查找了好多資料,都沒有說的太詳細(xì),更多的是照搬胡抄,這里是我親自寫的,可能很多人會(huì)用到,不多說上代碼:
自己的接口
package com.mhc.dao; import org.springframework.stereotype.Repository; import com.mhc.entity.Person; @Repository public interface DeviceCategoryDaoCustom { public Person getsFather(Person person); }
主接口
public interface DeviceCategoryDao extends PagingAndSortingRepository<Person, String>, DeviceCategoryDaoCustom { }
上面是我的接口繼承PagingAndSortingRepository、DeviceCategoryDaoCustom(我自己方法的接口)。
我新建一個(gè)類來實(shí)現(xiàn)我自己的接口
package com.mhc.dao; import javax.persistence.PersistenceContext; import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.NoRepositoryBean; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import com.mhc.entity.Person; @Repository("crudRepositoryDaoCustom") class DeviceCategoryDaoImpl implements DeviceCategoryDaoCustom { @Transactional public Person getsFather(Person person) { // TODO Auto-generated method stub Person father = new Person(); father = person.getParentPerson(); return father; } }
在這里有個(gè)需要注意的地方,就是用不用implements的問題,如果用的話,他就會(huì)調(diào)用編譯器的實(shí)現(xiàn)功能去實(shí)現(xiàn)我們自定義的接口也就是:DevicecategoryCustom。
如果去掉的話,他會(huì)去實(shí)現(xiàn)DeviceCategoryDao,那么會(huì)有人問,他怎么去自己找的呢。
事實(shí)上他是根據(jù)后面的Impl來尋找的。他不會(huì)提示@override,不過你寫相同的方法他還是會(huì)覆蓋(覆蓋主接口中的同名方法,如果有的話)DeviceCategoryDao中的同名方法。你可以去嘗試一下。
同時(shí)加上@Repository把他加入到Bean里面,這樣下次用這個(gè)方法的時(shí)候Repository會(huì)自動(dòng)找到他的(話說Spring團(tuán)隊(duì)真心NB)。然后我們交給spring托管、測(cè)試。。。。。Ok 真心贊
Spring Data Jpa自定義方法關(guān)鍵字
關(guān)鍵字 | 方法名舉例 | 對(duì)應(yīng)的SQL |
---|---|---|
And | findByNameAndAge | where name = ? and age = ? |
Or | findByNameOrAge | where name = ? or age = ? |
Is | findByNameIs | where name = ? |
Equals | findByNameEquals | where name = ? |
Between | findByAgeBetween | where age between ? and ? |
LessThan | findByAgeLessThan | where age < ? |
LessThanEquals | findByAgeLessThanEqual | where age <= ? |
GreatorThan | findByAgeGreaterThan | where age > ? |
GreatorThanEquals | findByAgeGreaterThanEqual | where age >= ? |
After | findByAgeAfter | where age > ? |
Before | findByAgeBefore | where age < ? |
IsNull | findByNameIsNull | where name is null |
IsNotNull,NotNull | findByNameIsNotNull,findByNameNotNull | where name is not null |
Not | findByNameNot | where name <>? |
In | findByAgeIn | where age in (?) |
NotIn | findByAgeNotIn | where age not in (?) |
NotLike | findByNameNotLike | where name not like ? |
Like | findByNameLike | where name like ? |
StartingWith | findByNameStartingWith | where name like ‘?%' |
EndingWith | findByNameEndingWith | where name like ‘%?' |
Containing,Contains | findByNameContaining,findByNameContains | where name like ‘%?%' |
OrderBy | findByOrderByAgeDesc | order by age desc |
True | findByBossTrue | where boss = true |
False | findByBossFalse | where boss = false |
IgnoreCase | findByNameIgnoreCase | where UPPER(name) = UPPER(?) |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):棧
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛好者有所幫助,同時(shí)祝大家有一個(gè)好成績,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07關(guān)于java的包Package中同名類的沖突及其理解
這篇文章主要介紹了關(guān)于java的包Package中同名類的沖突及其理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08IDEA引MAVEN項(xiàng)目jar包依賴導(dǎo)入問題解決方法
這篇文章主要介紹了IDEA引MAVEN項(xiàng)目jar包依賴導(dǎo)入問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Java實(shí)現(xiàn)系統(tǒng)限流的示例代碼
限流是保障系統(tǒng)高可用的方式之一,也是大廠高頻面試題,它在微服務(wù)系統(tǒng)中,緩存、限流、熔斷是保證系統(tǒng)高可用的三板斧,所以本文我們就來聊聊如何實(shí)現(xiàn)系統(tǒng)限流吧2023-09-09MybatisPlusException:Failed?to?process,Error?SQL異常報(bào)錯(cuò)的解決辦法
這篇文章主要給大家介紹了關(guān)于MybatisPlusException:Failed?to?process,Error?SQL異常報(bào)錯(cuò)的解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03Spring Cloud重試機(jī)制與各組件的重試總結(jié)
這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機(jī)制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11SpringBoot-JPA刪除不成功,只執(zhí)行了查詢語句問題
這篇文章主要介紹了SpringBoot-JPA刪除不成功,只執(zhí)行了查詢語句問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12