我们需要使用 Spring data jpa 从 SQL Server 检索数据。
由于我们有数百万条记录,因此我们无法一次请求加载所有数据。是否可以使用本机查询进行流式传输?
是的,您可以让您的查询方法在 Spring Data JPA 中返回 Stream 类型,并且仍然使用本机查询:
@Query(value = "YOUR QUERY", nativeQuery = true)
Stream<YourRecordType> findAllRecords();
请记住在使用完毕后关闭 Stream 资源,如 Spring Data JPA 文档中的建议:
Stream 可能会包装底层数据存储特定的资源
因此,使用后必须关闭。您可以手动
使用 close() 方法或使用 Java 7 关闭 Stream
尝试资源块 (...)
以下是相关内容docs https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-streaming and an example https://github.com/Ro4052/grad-library-project/blob/03755e147aecd0dd894f04fe20fa4285f9dfcfed/src/main/java/com/scottlogic/librarygradproject/Repositories/BorrowRepository.java#L22.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)