Spring Boot 带有多个where子句和多个数据集的JPA查询

b5buobof  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(70)
@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?
Sping Boot 3.1.1版本

the search is with the following set
book1-name  author1     200
book1-name  author2     300

database table content:
book1-name  author2     200
book1-name  author2     300


如果查询正确运行,结果应该只有一行,即第二行。

ldioqlga

ldioqlga1#

您可以在查询中使用“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())} ...")
List<BookEntity> findByMultipleBooks(@Param("bookDTOs") List<BookDTO> bookDTOs); }

字符串
如果查询表很大,则可以使用批处理、缓存或缓存

相关问题