上網查找了好多資料,都沒有說的太詳細,更多的是照搬胡抄,這裏是我親自寫的,可能很多人會用到,不多說上代碼:
自己的接口:
- 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 {
- }
- 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的問題,如果用的話,他就會調用編譯器的實現功能去實現我們自定義的接口也就是:DevicecategoryCustom。如果去掉的話,他會去實現DeviceCategoryDao,那麼會有人問,他怎麼去自己找的呢。事實上他是根據後面的Impl來尋找的。他不會提示@override,不過你寫相同的方法他還是會覆蓋(覆蓋主接口中的同名方法,如果有的話)DeviceCategoryDao中的同名方法。你可以去嘗試一下。
同時加上@Repository把他加入到Bean裏面,這樣下次用這個方法的時候Repository會自動找到他的(話說Spring團隊真心NB)。然後我們交給spring託管、測試。。。。。Ok 真心贊