具有多个where子句和多个数据集的JPA查询

问题描述 投票:0回答:1
@repository    
public interface BookRepository extends JPARepository<BookEntity bookEntity, Integer bookId>{

@Query
("select b from bookEntity b
where b.name=:#{#bookDTO.name}
and b.isbn=:#{#bookDTO.isbn}
... and 3-4 other where clauses
)
public Optional<BookEntity> findByMultipleCols(@Param("bookDTO") BookDTO bookDTO);

当我只需要运行此查询一次时,这非常有效。 我有一个场景,需要针对不同的 BookDTO 集多次运行此查询。更像是一个 List 对象。

到目前为止,我使用列表迭代器调用此方法 findByMultipleCols(bookDTO)。有没有其他方法可以仅调用此方法一次并使用 List 作为 @Param?

我使用的是Spring Boot 3.1.1版本。

spring-boot hibernate spring-data-jpa jpql jparepository
1个回答
0
投票

您可以在查询中使用“IN”,例如

@Repository public interface BookRepository extends JpaRepository<BookEntity, Integer> {

@Query("SELECT b FROM BookEntity b WHERE b.name IN :#{#bookDTOs.stream().map(BookDTO::getName).collect(Collectors.toList())} AND b.isbn IN

:#{#bookDTOs.stream().map(BookDTO::getIsbn).collect(Collectors.toList())} ...") 列出 findByMultipleBooks(@Param("bookDTOs") 列出 bookDTOs); }

如果您的查询表很大,可以使用批处理、异步或缓存

© www.soinside.com 2019 - 2024. All rights reserved.