上网查找了好多资料,都没有说的太详细,更多的是照搬胡抄,这里是我亲自写的,可能很多人会用到,不多说上代码:
自己的接口:
- 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 真心赞