關(guān)于Spring?Data?Jpa?自定義方法實(shí)現(xiàn)問(wè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)。
那么問(wèn)題來(lái)了:Spring Data JPA好處就是讓我們省去了實(shí)現(xiàn)接口的過(guò)程,按照他們給的命名規(guī)范他們會(huì)自動(dòng)實(shí)現(xiàn)我們的業(yè)務(wù)邏輯,那我們自己實(shí)現(xiàn)的接口要怎么注入到其中呢?
上網(wǎng)查找了好多資料,都沒(méi)有說(shuō)的太詳細(xì),更多的是照搬胡抄,這里是我親自寫的,可能很多人會(huì)用到,不多說(shuō)上代碼:
自己的接口
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è)類來(lái)實(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的問(wèn)題,如果用的話,他就會(huì)調(diào)用編譯器的實(shí)現(xiàn)功能去實(shí)現(xiàn)我們自定義的接口也就是:DevicecategoryCustom。
如果去掉的話,他會(huì)去實(shí)現(xiàn)DeviceCategoryDao,那么會(huì)有人問(wèn),他怎么去自己找的呢。
事實(shí)上他是根據(jù)后面的Impl來(lái)尋找的。他不會(huì)提示@override,不過(guò)你寫相同的方法他還是會(huì)覆蓋(覆蓋主接口中的同名方法,如果有的話)DeviceCategoryDao中的同名方法。你可以去嘗試一下。
同時(shí)加上@Repository把他加入到Bean里面,這樣下次用這個(gè)方法的時(shí)候Repository會(huì)自動(dòng)找到他的(話說(shuō)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)文章
一文學(xué)會(huì)處理SpringBoot統(tǒng)一返回格式
MyBatisPlus使用${ew.customSqlSegment}別名問(wèn)題解決
如何在IDE部署springboot項(xiàng)目(有swagger和無(wú)swagger都是一樣的)到服務(wù)器或者虛擬機(jī)上的docke

