關(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)鍵字 | 方法名舉例 | 對應的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)文章
MyBatisPlus使用${ew.customSqlSegment}別名問題解決
如何在IDE部署springboot項目(有swagger和無swagger都是一樣的)到服務(wù)器或者虛擬機上的docke

