iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >继承jpa Repository 写自定义方法查询的实例分析
  • 514
分享到

继承jpa Repository 写自定义方法查询的实例分析

2023-06-21 23:06:54 514人浏览 薄情痞子
摘要

这篇文章给大家介绍继承jpa Repository 写自定义方法查询的实例分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。继承jpa Repository写自定义方法查询今天在写jpa查询的时候,

这篇文章给大家介绍继承jpa Repository 写自定义方法查询的实例分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

    继承jpa Repository写自定义方法查询

    今天在写jpa查询的时候,遇到了添加自定义方法,项目启动报错原因,现总结如下:

    首先定义实体类

    @Entity@Table(name = "user")Class User{     @Id    @GeneratedValue       int id;      @Column      String age;      @Column      String school;      @Column      String userName;  set,get方法 (省略) }
    public interface UserRepository extends JpaRepository<User, Long> {      List<User> findByUsernameLike(String username);     List<User> aaa();}

    启动项目时,

    项目报错提示信息为

    org.springframework.data.mapping.PropertyReferenceException: No property aaa found for type com.fpi.safety.common.entity.po.User

    再将List<User> aaa();方法去掉后,项目又可以正常启动运行

    是什么原因呢?

    经查找,原来是继承jpa,必须满足一些规则,规则如下

    继承jpa Repository 写自定义方法查询的实例分析

    继承jpa Repository 写自定义方法查询的实例分析

    Spring Data JPA框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如find,findBy,read,readBy,get,getBy,然后对剩下的部分进行解析。

    假如创建如下的查询:findByUserName(),框架在解析该方法时,首先剔除findBy,然后对剩下的属性进行解析,假设查询实体为User

    先判断userName(根据POJO规范,首字母变为小写)是否为查询实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,继续第二步;

    从右往左截取第一个大写字母开头的字符串此处是Name),然后检查剩下的字符串是否为查询实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,则重复第二步,继续从右往左截取;最后假设用户为查询实体的一个属性;

    接着处理剩下部分(UserName),先判断用户所对应的类型是否有userName属性,如果有,则表示该方法最终是根据“User.userName”的取值进行查询;否则继续按照步骤2的规则从右往左截取,最终表示根据“User.userName”的值进行查询。

    可能会存在一种特殊情况,比如User包含一个的属性,也有一个userNameChange属性,此时会存在混合。可以明确在属性之间加上“_”以显式表达意思,比如“findByUser_NameChange )“或者”findByUserName_Change()“

    从上面,我们可以得知,jap在解析是,aaa在user类中是没有属性的,所以报错No property aaa found.

    如果我们想要使用jap框架,又不想再多增加一个自定义类,则必须符合其命名规则

    如果,你记不住jpa的规则也没关系,你可以自己再多写一个类来实现自定义查询方法

    如下:

    自定义一个接口,该接口用来声明自己额外定义的查询。

    public interface UseerRepositoryTwo {    public List<User> searchUser(String name, int id);}

    创建一个接口,该接口 extends JpaRepository 或者 CurdRepository, 以及上面自己定义的接口 UseerRepositoryTwo

    public interface UserRepositoryTwoService extends CrudRepository<LogDTO, Integer>, CustomizedLogRepository {}

    实现UserRepositoryTwoService

    注意此处的类名,必须以 2 中创建的接口的名字UserRepositoryTwoService,后面加上 Impl 来声明,而不是写成 UseerRepositoryTwoImpl

    public class UserRepositoryTwoServiceImpl implements UserRepositoryTwoService {    @Autowired    @PersistenceContext    private EntityManager entityManager;    @Override    public List<User> searchLogs(int Id, String name) {        ......    }}

    自己在写自定义实现即可~

    JpaRepository 命名规范

    KeyWordSampleJPQL
    AndfindByLastnameAndFirstnamewhere x.lastname=?1 and x.firstname=?2
    OrfindByLastnameOrFirstnamewhere x.lastname=?1 or x.firstname=?2
    BetweenfindByStartDateBetweenwhere x.startDate between ?1 and ?2
    LessThanfindByAgeLessThanwhere x.startDate < ?1
    GreaterThanfindByAgeGreaterThanwhere x.startDate >?1
    AfterfindByStartDateAfterwhere x.startDate >n ?1
    BeforefindByStartDateBeforewhere x.startDate < ?1
    IsNullfindByAgeIsNullwhere x.age is null
    IsNotNull,NotNullfindByAge(Is)NotNullwhere x.age not null
    LikefindByFirstnameLikewhere x.firstname like ?1
    notLikefindByFirstnameNotLikewhere x.firstname not like ?1
    StartingWithfindByFirstnameStartingWithXXXwhere x.firstname like ?1(parameter bound with appended %)
    EndingWithfindByFirstnameEndingWithXXXwhere x.firstname like ?1(parameter bound with appended %)
    ContainingfindByFirstnameContainingwhere x.firstname like ?1(parameter bound wrapped in %)
    OrderByfindByAgeOrderByLastnamewhere x.age = ?1 order by x.lastname desc
    NotfindByLastnameNotwhere x.lastname <> ?1
    NotInfindByAgeNotIn(Collection age )where x.age not in ?1
    TruefindByActiveTrue()where x.active = true
    FalsefindByActiveFalse()

    where x.active = false

    例:

    @RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)public interface EmployeeRepository { //extends Repository<Employee,Integer>{    public Employee findByName(String name);    // where name like ?% and age <?    public List<Employee> findByNameStartingWithAndAgeLessThan(String name, Integer age);    // where name like %? and age <?    public List<Employee> findByNameEndingWithAndAgeLessThan(String name, Integer age);    // where name in (?,?....) or age <?    public List<Employee> findByNameInOrAgeLessThan(List<String> names, Integer age);    // where name in (?,?....) and age <?    public List<Employee> findByNameInAndAgeLessThan(List<String> names, Integer age);    @Query("select o from Employee o where id=(select max(id) from Employee t1)")    public Employee getEmployeeByMaxId();    @Query("select o from Employee o where o.name=?1 and o.age=?2")    public List<Employee> queryParams1(String name, Integer age);    @Query("select o from Employee o where o.name=:name and o.age=:age")    public List<Employee> queryParams2(@Param("name")String name, @Param("age")Integer age);    @Query("select o from Employee o where o.name like %?1%")    public List<Employee> queryLike1(String name);    @Query("select o from Employee o where o.name like %:name%")    public List<Employee> queryLike2(@Param("name")String name);    @Query(nativeQuery = true, value = "select count(1) from employee")//这个使用了原生sql    public long getCount();    @Modifying    @Query("update Employee o set o.age = :age where o.id = :id")    public void update(@Param("id")Integer id, @Param("age")Integer age);}

    关于继承jpa Repository 写自定义方法查询的实例分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

    --结束END--

    本文标题: 继承jpa Repository 写自定义方法查询的实例分析

    本文链接: https://www.lsjlt.com/news/301570.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • 继承jpa Repository 写自定义方法查询的实例分析
      这篇文章给大家介绍继承jpa Repository 写自定义方法查询的实例分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。继承jpa Repository写自定义方法查询今天在写jpa查询的时候,...
      99+
      2023-06-21
    • 继承jpa Repository 写自定义方法查询实例
      目录继承jpa Repository写自定义方法查询首先定义实体类项目报错提示信息为是什么原因呢?JpaRepository常用方法增改删查继承jpa Repository写自定义方...
      99+
      2024-04-02
    • Python自定义类继承threading.Thread的方法
      本篇内容介绍了“Python自定义类继承threading.Thread的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!说明使用thre...
      99+
      2023-06-30
    • JPA如何使用findBy方法自定义查询
      目录JPA使用findBy方法自定义查询在JPA中使用findBy方法自定义查询在postman测试请求的接口如下JPA的findBy语法整理前提操作JPA中支持的关键词JPA使用f...
      99+
      2024-04-02
    • JPA怎么使用findBy方法自定义查询
      这篇文章给大家分享的是有关JPA怎么使用findBy方法自定义查询的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。JPA使用findBy方法自定义查询最近在项目中使用spring boot+jpa的方式来访问数据库...
      99+
      2023-06-21
    • jpa自定义排序查询的方法是什么
      在JPA中,可以使用以下方法自定义排序查询:1. 使用@Query注解:可以在查询方法上添加@Query注解,通过JPQL或者SQL...
      99+
      2023-09-16
      jpa
    • spring data jpa 查询自定义字段,转换为自定义实体方式
      目标:查询数据库中的字段,然后转换成 JSON 格式的数据,返回前台。 环境:idea 2016.3.4, jdk 1.8, mysql 5.6, spring-boot 1.5.2...
      99+
      2024-04-02
    • Spring Data Jpa返回自定义对象的3种方法实例
      目录方法一、简单查询直接new对象方法二、Service层使用EntityManager方法三、Dao层使用Map接收自定义对象总结tasks表对应的Entity @Entity @...
      99+
      2024-04-02
    • SpringData JPA增删改查操作方法实例分析
      这篇文章主要讲解了“SpringData JPA增删改查操作方法实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringData JPA增删改查操作方法实例分析”吧!1、服务层调用...
      99+
      2023-07-02
    • Spring Data Jpa 复杂查询方式总结(多表关联及自定义分页)
      目录Spring Data Jpa复杂查询总结1.查找出Id小于3,并且名称带有shanghai的记录2.通过旅店名称分页查询旅店以及城市的所有信息3.HQL通过旅店名称查询旅店以及...
      99+
      2024-04-02
    • Spring Data JPA实现查询结果返回map或自定义的实体类
      目录Spring Data JPA查询结果返回map或自定义的实体类1.工具类2.具体应用spingboot:jpa:Spring data jpa 返回map 结果集Spring ...
      99+
      2024-04-02
    • mybatis-plus实现自定义SQL、多表查询与多表分页查询语句实例
      目录前言1、自定义SQL2、多表查询3、多表分页查询4、多表分页条件查询总结前言 本文介绍了在mybatis-plus中如何实现:自定义SQL语句,多表查询语句,多表分页查询语句 在...
      99+
      2024-04-02
    • jQuery中Datatable多个查询条件自定义提交事件的示例分析
      这篇文章主要为大家展示了“jQuery中Datatable多个查询条件自定义提交事件的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“jQuery中Dat...
      99+
      2024-04-02
    • SwiftUI自定义导航的方法实例
      目录前言切换标签(tabs)控制导航堆栈小结前言 默认情况下,SwiftUI提供的各种导航API在很大程度上是以用户直接输入为中心的——也就是说,导航是在系统...
      99+
      2024-04-02
    • PHP编写查询指定分类商品的方法
      PHP编写查询指定分类商品的方法 在网站开发过程中,经常需要根据商品的分类来进行查询和展示。下面介绍一种使用PHP编写查询指定分类商品的方法,并提供具体的代码示例。 首先,我们需要在数...
      99+
      2024-03-10
      查询 php 分类商品 sql语句 php编写
    • Angular自定义指令Tooltip的方法实例
      目录目录结构编写 tooltip 组件编写 tooltip 指令页面上调用总结Yeah,关注我的读者应该知道,上一篇文章了解 Angular 开发的内容,我们已经概览了 Angula...
      99+
      2024-04-02
    • C#读写自定义的Config文件的实现方法
      目录一、前言二、添加config文件三、读写配置文件一、前言 在软件开发中,经常用到设置这样的功能,如果设置中的功能不多,用 Json、XML 这样的数据结构存储非常的麻烦,一个字段...
      99+
      2024-04-02
    • MyBatis自定义映射关系和关联查询实现方法详解
      目录一、使用注解实现自定义映射关系1. 编写注解方法2. 编写测试方法3. 查看运行结果二、使用注解实现一对一关联查询1. 编写注解方法2. 编写测试方法3. 查看运行结果三、使用注...
      99+
      2023-05-15
      MyBatis自定义映射关系 MyBatis关联查询
    • 解析iReport自定义行数分页的操作方法
      iReport 是为JasperReports Library和JasperReports Server设计的报表可视化设计器。iReport是一个能够创建复杂报表的开源项目。它10...
      99+
      2024-04-02
    • Sharding-Jdbc自定义复合分片的实现方法
      这篇文章主要讲解了“Sharding-Jdbc自定义复合分片的实现方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Sharding-Jdbc自定义复合分片的实现方法”吧!目录Shardin...
      99+
      2023-06-20
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作