JPA JPQL IN 子句:如何在 JPA 中使用 IN 子句?
JPA JPQL IN clause: How to use IN clause in JPA?
@Query("SELECT al FROM Customer al WHERE al.companyCode = ?1 AND al.fileCode IN ?2")
List findallByGroup(int CompanyCode, String groups);
或者
@Query("SELECT al FROM Customer al WHERE al.companyCode = :CompanyCode AND al.fileCode IN :groups")
List<Customer> findallByGroup(@Param("CompanyCode") int CompanyCode,@Param("groups") List<BigInteger> groups);
或
@Query("SELECT al FROM Customer al WHERE al.companyCode = :CompanyCode AND al.fileCode IN (:groups)")
List<Customer> findallByGroup(@Param("CompanyCode") int CompanyCode,@Param("groups") List<BigInteger> groups);
findAllByCompanyCodeAndFileCodeIn(int CompanyCode, List<String> groups)
您不需要 @Query
。 Spring 数据可以从方法名中理解查询。使用上面的方法。
与 OP 的特定查询没有直接关系,但与一般的 JPA IN 子句相关:
与 pvpkiran 的回答相反,如果您正在执行 SELECT 查询以外的任何操作(例如删除、更新),使用 @Query
:
可能效率更高
@Modifying
@Query("DELETE from Customer al where al.fileCode in :groups")
deleteByFileCodeIn(@Param("groups") List<String> groups)
而不是依赖Spring JPA的查询方法:
deleteByFileCodeIn(List<String> groups) // avoid this
原因:
Spring JPA 查询方法对 IN 子句的默认实现效率低下。在引擎盖下,它将 1. 首先 select 所有符合 IN 标准的记录,然后 2. 对找到的 每个 记录执行 DELETE 语句。
select customer.id as id,... from customer where customer.file_code in (?,?,?,...)
delete from customer where id=?
delete from customer where id=?
delete from customer where id=?
...
这意味着如果有 1000 条匹配记录,它将生成并执行 1000 条删除语句 -- 而不是通常预期的单个 DELETE...IN 语句。
通过对 IN 子句使用 @Query
,您可以覆盖 Spring JPA 的默认实现,并指示使用更高效的查询。在我自己的测试中,这导致大型 (>3K) 数据集的响应时间提高了 10 倍。
需要注意的是,根据数据库的不同,IN 子句中可以使用的参数数量可能会有限制。这可以通过 列表来克服。
@Query("SELECT al FROM Customer al WHERE al.companyCode = ?1 AND al.fileCode IN ?2")
List findallByGroup(int CompanyCode, String groups);
或者
@Query("SELECT al FROM Customer al WHERE al.companyCode = :CompanyCode AND al.fileCode IN :groups")
List<Customer> findallByGroup(@Param("CompanyCode") int CompanyCode,@Param("groups") List<BigInteger> groups);
或
@Query("SELECT al FROM Customer al WHERE al.companyCode = :CompanyCode AND al.fileCode IN (:groups)")
List<Customer> findallByGroup(@Param("CompanyCode") int CompanyCode,@Param("groups") List<BigInteger> groups);
findAllByCompanyCodeAndFileCodeIn(int CompanyCode, List<String> groups)
您不需要 @Query
。 Spring 数据可以从方法名中理解查询。使用上面的方法。
与 OP 的特定查询没有直接关系,但与一般的 JPA IN 子句相关:
与 pvpkiran 的回答相反,如果您正在执行 SELECT 查询以外的任何操作(例如删除、更新),使用 @Query
:
@Modifying
@Query("DELETE from Customer al where al.fileCode in :groups")
deleteByFileCodeIn(@Param("groups") List<String> groups)
而不是依赖Spring JPA的查询方法:
deleteByFileCodeIn(List<String> groups) // avoid this
原因:
Spring JPA 查询方法对 IN 子句的默认实现效率低下。在引擎盖下,它将 1. 首先 select 所有符合 IN 标准的记录,然后 2. 对找到的 每个 记录执行 DELETE 语句。
select customer.id as id,... from customer where customer.file_code in (?,?,?,...)
delete from customer where id=?
delete from customer where id=?
delete from customer where id=?
...
这意味着如果有 1000 条匹配记录,它将生成并执行 1000 条删除语句 -- 而不是通常预期的单个 DELETE...IN 语句。
通过对 IN 子句使用 @Query
,您可以覆盖 Spring JPA 的默认实现,并指示使用更高效的查询。在我自己的测试中,这导致大型 (>3K) 数据集的响应时间提高了 10 倍。
需要注意的是,根据数据库的不同,IN 子句中可以使用的参数数量可能会有限制。这可以通过