千家信息网

Spring Data Jpa如何实现自定义方法

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章将为大家详细讲解有关Spring Data Jpa如何实现自定义方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Spring Data Jpa 自定义方法
千家信息网最后更新 2025年11月07日Spring Data Jpa如何实现自定义方法

这篇文章将为大家详细讲解有关Spring Data Jpa如何实现自定义方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

    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; @Repositorypublic interface DeviceCategoryDaoCustom { public Person getsFather(Person person); }

    主接口

    public interface DeviceCategoryDao extends  PagingAndSortingRepository, 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(?)

    关于"Spring Data Jpa如何实现自定义方法"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

    方法 接口 更多 篇文章 业务 关键 关键字 功能 同时 就是 真心 还是 逻辑 问题 不错 实用 相同 没有说的 上代 不用 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 魔笛网络技术有限公司 软件开发怎么知道是你开发的 关服的游戏如何自己搭服务器 ti软件开发平台 软件开发的论文降说明书 数据库的扩展名一定要小写吗 沧州管理系统软件开发 发生在身边的网络安全征文 璧山区咨询软件开发流程常见问题 诚实守信数据库技术 sql2000 收缩数据库 数据库模型分解 题目 2021年登封大数据库具体情况 相应的文献数据库网站上 计算机网络技术物联网应用 宝山区私人软件开发生产厂家 圣大网络技术部 centos服务器怎么挂载u盘 只有公网ip的服务器 戴尔服务器支持什么系统 管理系统软件开发解决方案 河南抖音精控软件开发公司 手机安全模式到数据库 软件开发与管理就业前景 软件开发岗 笔试题 软件开发公司有保密协议吗 网络安全网评文章人民网 在什么时候会用到数据库 软件开发过程中测试有哪几种 桂林山水课件软件开发
    0