【发布时间】:2013-02-20 07:52:21
【问题描述】:
我有一个大表,我想通过 Spring Data Repository 访问它。
目前,我正在尝试扩展PagingAndSortingRepository 接口,但似乎我只能定义返回列表的方法,例如:
public interface MyRepository extends
PagingAndSortingRepository<MyEntity, Integer>
{
@Query(value="SELECT * ...")
List<MyEntity> myQuery(Pageable p);
}
另一方面,PagingAndSortingRepository 附带的findAll() 方法返回一个Iterable(我假设数据没有加载到内存中)。
是否可以定义同时返回 Iterable 和/或不一次将所有数据加载到内存中的自定义查询?
有没有其他方法可以处理大表?
【问题讨论】:
-
List实现了Iterable接口,因此您的自定义查询方法确实返回Iterable。 -
我想当我使用 findAll() 时 Spring Data 不会将所有内容加载到内存中,我错了吗?我将编辑问题。
-
underlying implementation 只是检索一个列表,所以没有那么复杂。
-
我明白了,谢谢你的解释。顺便问一下,你将如何解决这个问题?使用 Pageable 参数是不将所有内容加载到内存中的唯一方法吗?
标签: spring repository spring-data