关于Spring Data Jpa 自定义方法实现问题

作者:qq_23660243 时间:2023-11-28 10:08:32 

Spring Data Jpa 自定义方法的实现

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

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

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

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

自己的接口


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(我自己方法的接口)。

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


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 真心赞

Spring Data Jpa自定义方法关键字

关键字方法名举例对应的SQL
AndfindByNameAndAgewhere name = ? and age = ?
OrfindByNameOrAgewhere name = ? or age = ?
IsfindByNameIswhere name = ?
EqualsfindByNameEqualswhere name = ?
BetweenfindByAgeBetweenwhere age between ? and ?
LessThanfindByAgeLessThanwhere age < ?
LessThanEqualsfindByAgeLessThanEqualwhere age <= ?
GreatorThanfindByAgeGreaterThanwhere age > ?
GreatorThanEqualsfindByAgeGreaterThanEqualwhere age >= ?
AfterfindByAgeAfterwhere age > ?
BeforefindByAgeBeforewhere age < ?
IsNullfindByNameIsNullwhere name is null
IsNotNull,NotNullfindByNameIsNotNull,findByNameNotNullwhere name is not null
NotfindByNameNotwhere name <>?
InfindByAgeInwhere age in (?)
NotInfindByAgeNotInwhere age not in (?)
NotLikefindByNameNotLikewhere name not like ?
LikefindByNameLikewhere name like ?
StartingWithfindByNameStartingWithwhere name like ‘?%'
EndingWithfindByNameEndingWithwhere name like ‘%?'
Containing,ContainsfindByNameContaining,findByNameContainswhere name like ‘%?%'
OrderByfindByOrderByAgeDescorder by age desc
TruefindByBossTruewhere boss = true
FalsefindByBossFalsewhere boss = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name) = UPPER(?)

来源:https://blog.csdn.net/qq_23660243/article/details/43194465

标签:Spring,Data,Jpa,自定义方法
0
投稿

猜你喜欢

  • 详解SpringBoot+Mybatis实现动态数据源切换

    2022-06-04 02:42:02
  • C#判断字符串是否存在字母及字符串中字符的替换实例

    2022-04-15 03:49:48
  • Java实现简易计算器(逆波兰表达式)

    2022-06-17 13:48:23
  • Kotlin基础学习之lambda中return语句详解

    2023-09-04 16:33:41
  • Android高级xml布局之输入框EditText设计

    2022-09-24 14:25:56
  • Kotlin数据容器深入讲解

    2022-03-28 05:19:34
  • C# WinForm中Panel实现用鼠标操作滚动条的实例方法

    2021-08-08 01:52:42
  • Java使用Tesseract-Ocr识别数字

    2022-12-30 05:15:44
  • Android简单的利用MediaRecorder进行录音的实例代码

    2023-04-24 03:52:57
  • java基础的详细了解第六天

    2021-11-05 16:18:49
  • 一文告诉你为什么要重写hashCode()方法和equals()方法

    2021-09-05 07:05:55
  • 对Mapper 中几种update的区别说明

    2023-05-19 04:39:44
  • Jetpack Compose自定义动画与Animatable详解

    2021-07-04 20:26:08
  • SpringBoot实现过滤器和拦截器的方法

    2022-10-21 23:29:34
  • Java实现简单学生管理系统

    2023-01-06 16:40:38
  • C++内存池的简单实现

    2022-05-27 05:20:50
  • C#实现的简单随机数产生器功能示例

    2023-06-05 12:57:54
  • java 多线程的三种构建方法

    2023-07-01 22:18:26
  • java基础javeSE程序逻辑控制语法

    2022-09-21 23:41:47
  • Android 捕获运行时异常详解

    2023-12-22 21:16:30
  • asp之家 软件编程 m.aspxhome.com