关于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 |
---|---|---|
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(?) |
来源:https://blog.csdn.net/qq_23660243/article/details/43194465


猜你喜欢
- 很不错的手电筒APP,分享给大家,希望大家喜欢。1. Java代码 package com.
- 概述常用的弹窗有菜单,或者Dialog,但更加人性化和可自定义的还是PopupWindow如果只是展示列表数据或者弹窗列表选择,直接使用Li
- 本文实例为大家分享了Android实现指针刻度转盘的具体代码,供大家参考,具体内容如下一. 先上个效果图,实现如图所示刻度转盘和2个文本的绘
- 一、图示spring再简化:SpringBoot-jar:内嵌tomacat;微服务架构!二、springboot是什么spring是一个为
- 1.封装1.介绍封装是指把抽象出的属性和方法封装在一起,数据被保护在内部,程序的其他部分只能通过被授权的方法,才能对数据操作。2.封装的理解
- 1.Spring Gateway概述1.1 什么是Spring Cloud GatewaySpring Cloud Gateway是Spri
- 在使用多线程过程中,可能会遇到在一些情况下必须等待子线程全部执行结束后主线程才进行下一步,做法如下: //在使用多线程过程中,可能会遇到在一
- 众所周知,在墙内开发很头疼的一件事就是Maven仓库的连接速度太慢。虽然对于很多互联网企业和大中型软件公司,建个镜像是分分钟的事。但对于个人
- 前言所谓的字符串其实就是一串连续的字符,它是由许多单个字符连接而成的。如多个英文字母所组成的一个英文单词。字符串中可以包含任意字符,这些字符
- 我们深知在操作Java流对象后要将流关闭,但往往事情不尽人意,大致有以下几种不能一定将流关闭的写法:1.在try中关流,而没在finally
- 配置文件中使用${}注入值方式在springboot中使用System.setProperty设置参数user: user-na
- 本文实例讲述了WinForm中comboBox控件数据绑定实现方法。分享给大家供大家参考,具体如下:下面介绍三种对comboBox绑定的方式
- 上一篇我们详解了setttings.xml的配置项,里面的配置项基本都和仓库有关系,我们使用maven更多的也是要从仓库下载jar包,然后也
- 废话不多说了,直接给大家贴关键代码了。具体代码如下所示:using System;using System.Collections.Gene
- springboot配置mysql数据库spring.datasource.url报错spring.datasource.url=jdbc:
- 苹果的iphone有语音识别用的是Google的技术,做为Google力推的Android 自然会将其核心技术往Android 系统里面植入
- 1.前言(基于JDK1.7)最近想把一些java基础的东西整理一下,但是又不知道从哪里开始!想了好久,还是从最基本的jvm开始吧!这一节就简
- java输入流报错:Exception in thread "main" java.util.NoSuchElement
- 一、银行存取款1.前言毕竟谁不喜欢钱呢!(不是😅)我看谁不喜欢在知识的海洋中遨游😤!2.描述银行存取款的流程是人们非常熟悉的事情,用户可以在
- 1.特殊数组的特征值题目描述思路详解看到本题,首先思考需要排序,然后查找,这里为了效率采用二分查找。假设定义x=(left+riht)/ 2