【问题标题】:How to use Customize your Queries in JPA with Query Methods (spring boot)如何使用查询方法在 JPA 中自定义您的查询(spring boot)
【发布时间】:2019-09-28 13:59:51
【问题描述】:

我想在不使用 Spring JPA 中的查询方法的情况下根据条件从数据库中检索特定值。
他们想要查询的是

SELECT TOP 1 * FROM Co2 WHERE Co2.room = ?1 order by co2.id desc

可以在普通的原生查询注释中使用,如下所示:

public interface Co2Respository extends CrudRepository<Co2, Integer> {
    
    @Query("SELECT TOP 1 * FROM Co2 WHERE Co2.room = ?1 order by co2.id desc",
    nativeQuery = true)
    Co2 findLastInsertedValueForRoom(int id);
}

问题是如何使用 Spring JPA 中的自定义查询方法实现相同的功能

【问题讨论】:

    标签: spring hibernate jpa spring-data-jpa spring-data


    【解决方案1】:

    我会回答我自己的问题, 上述查询的等效自定义方法是:

    Co2 findTopByRoomOrderByIdDesc(Room room);
    

    【讨论】:

      猜你喜欢
      • 2019-02-16
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 2015-12-07
      相关资源
      最近更新 更多