经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
关于Spring?Data?Jpa?自定义方法实现问题
来源:jb51  时间:2021/12/8 17:01:06  对本文有异议

Spring Data Jpa 自定义方法的实现

最近项目中用到了Spring Data JPA,在里面我继承了一个PagingAndSortingRepository的接口,期望的是利用Spring Data JPA提供的便利。

同时我也希望自己有一个能定义自己方法的接口,因为单纯靠Spring Data JPA中提供的功能还是有很多业务逻辑实现不了,我必须自己实现。

那么问题来了:Spring Data JPA好处就是让我们省去了实现接口的过程,按照他们给的命名规范他们会自动实现我们的业务逻辑,那我们自己实现的接口要怎么注入到其中呢?

上网查找了好多资料,都没有说的太详细,更多的是照搬胡抄,这里是我亲自写的,可能很多人会用到,不多说上代码:

自己的接口

  1. package com.mhc.dao;
  2. import org.springframework.stereotype.Repository;
  3. import com.mhc.entity.Person;
  4. @Repository
  5. public interface DeviceCategoryDaoCustom {
  6. public Person getsFather(Person person);
  7. }

主接口

  1. public interface DeviceCategoryDao extends
  2. PagingAndSortingRepository<Person, String>, DeviceCategoryDaoCustom {
  3. }

上面是我的接口继承PagingAndSortingRepository、DeviceCategoryDaoCustom(我自己方法的接口)。

我新建一个类来实现我自己的接口

  1. package com.mhc.dao;
  2. import javax.persistence.PersistenceContext;
  3. import javax.transaction.Transactional;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.repository.CrudRepository;
  6. import org.springframework.data.repository.NoRepositoryBean;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.stereotype.Repository;
  9. import org.springframework.stereotype.Service;
  10. import com.mhc.entity.Person;
  11. @Repository("crudRepositoryDaoCustom")
  12. class DeviceCategoryDaoImpl implements DeviceCategoryDaoCustom {
  13. @Transactional
  14. public Person getsFather(Person person) {
  15. // TODO Auto-generated method stub
  16. Person father = new Person();
  17. father = person.getParentPerson();
  18. return father;
  19. }
  20. }

在这里有个需要注意的地方,就是用不用implements的问题,如果用的话,他就会调用编译器的实现功能去实现我们自定义的接口也就是:DevicecategoryCustom。

如果去掉的话,他会去实现DeviceCategoryDao,那么会有人问,他怎么去自己找的呢。

事实上他是根据后面的Impl来寻找的。他不会提示@override,不过你写相同的方法他还是会覆盖(覆盖主接口中的同名方法,如果有的话)DeviceCategoryDao中的同名方法。你可以去尝试一下。

同时加上@Repository把他加入到Bean里面,这样下次用这个方法的时候Repository会自动找到他的(话说Spring团队真心NB)。然后我们交给spring托管、测试。。。。。Ok 真心赞

Spring Data Jpa自定义方法关键字

关键字 方法名举例 对应的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(?)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号