關(guān)于Spring?Data?Jpa?自定義方法實現(xiàn)問題
Spring Data Jpa 自定義方法的實現(xiàn)
最近項目中用到了Spring Data JPA,在里面我繼承了一個PagingAndSortingRepository的接口,期望的是利用Spring Data JPA提供的便利。
同時我也希望自己有一個能定義自己方法的接口,因為單純靠Spring Data JPA中提供的功能還是有很多業(yè)務(wù)邏輯實現(xiàn)不了,我必須自己實現(xiàn)。
那么問題來了:Spring Data JPA好處就是讓我們省去了實現(xiàn)接口的過程,按照他們給的命名規(guī)范他們會自動實現(xiàn)我們的業(yè)務(wù)邏輯,那我們自己實現(xiàn)的接口要怎么注入到其中呢?
上網(wǎng)查找了好多資料,都沒有說的太詳細,更多的是照搬胡抄,這里是我親自寫的,可能很多人會用到,不多說上代碼:
自己的接口
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(我自己方法的接口)。
我新建一個類來實現(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; } }
在這里有個需要注意的地方,就是用不用implements的問題,如果用的話,他就會調(diào)用編譯器的實現(xiàn)功能去實現(xiàn)我們自定義的接口也就是:DevicecategoryCustom。
如果去掉的話,他會去實現(xiàn)DeviceCategoryDao,那么會有人問,他怎么去自己找的呢。
事實上他是根據(jù)后面的Impl來尋找的。他不會提示@override,不過你寫相同的方法他還是會覆蓋(覆蓋主接口中的同名方法,如果有的話)DeviceCategoryDao中的同名方法。你可以去嘗試一下。
同時加上@Repository把他加入到Bean里面,這樣下次用這個方法的時候Repository會自動找到他的(話說Spring團隊真心NB)。然后我們交給spring托管、測試。。。。。Ok 真心贊
Spring Data Jpa自定義方法關(guān)鍵字
關(guān)鍵字 | 方法名舉例 | 對應(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(?) |
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):棧
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對廣大的程序愛好者有所幫助,同時祝大家有一個好成績,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07關(guān)于java的包Package中同名類的沖突及其理解
這篇文章主要介紹了關(guān)于java的包Package中同名類的沖突及其理解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08IDEA引MAVEN項目jar包依賴導(dǎo)入問題解決方法
這篇文章主要介紹了IDEA引MAVEN項目jar包依賴導(dǎo)入問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11MybatisPlusException:Failed?to?process,Error?SQL異常報錯的解決辦法
這篇文章主要給大家介紹了關(guān)于MybatisPlusException:Failed?to?process,Error?SQL異常報錯的解決辦法,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2023-03-03Spring Cloud重試機制與各組件的重試總結(jié)
這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11SpringBoot-JPA刪除不成功,只執(zhí)行了查詢語句問題
這篇文章主要介紹了SpringBoot-JPA刪除不成功,只執(zhí)行了查詢語句問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12