【问题标题】:How do I reuse a parameter witha Spring-Data-JPA Repository?如何使用 Spring-Data-JPA Repository 重用参数?
【发布时间】:2017-11-23 11:22:15
【问题描述】:

在查看 Spring Data JPA 存储库的 Query Creation 时,我想知道如何重用参数。例如,如果我想做这样的事情,我将如何命名该方法:

@Query("select c from #{#entityName} c where c.lower <= ?1 and c.upper >= ?1")
E findByConversionFor(Double amount);

能否将该查询转换为 SpEL 方法名称(供查询构建器使用)?

要求将相同的值传递两次似乎很麻烦:

E findByLowerLessThanOrEqualAndUpperGreaterThanOrEqual(Double a, Double b); // where a==b

【问题讨论】:

标签: spring spring-data-jpa jpql spring-el


【解决方案1】:

只需用@Param("amount") 标记您的参数,然后就可以按名称使用它:

@Query("select c from #{#entityName} c where c.lower <= :amount and c.upper >= :amount")

【讨论】:

  • 是的,这非常适合 JPQL 查询。你知道如何在 SpEL 方法中重用参数吗?
  • 那么,有没有办法通过“查询方法名创建”? (不需要@Query 注释)
猜你喜欢
  • 2018-01-09
  • 2014-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 2020-10-30
  • 2018-11-13
  • 1970-01-01
相关资源
最近更新 更多