如何在 Spring-Data-JPA 存储库中重用参数?

end*_*ser 5 spring jpql spring-el spring-data-jpa

在查看Spring Data JPA 存储库的查询创建时,我想知道如何重用参数。例如,如果我想做类似的事情,我将如何命名该方法:

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

该查询是否可以转换为 SpEL 方法名称(由查询构建器使用)?

要求将相同的值传递两次似乎是一种拼凑:

E findByLowerLessThanOrEqualAndUpperGreaterThanOrEqual(Double a, Double b); // where a==b
Run Code Online (Sandbox Code Playgroud)

Boh*_*nko 2

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

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

  • 是的,这非常适合 JPQL 查询。您知道如何重用 SpEL 方法中的参数吗? (3认同)