java 如何在带有rawrepository的spring boot中使用findAll方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37412327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use findAll method in spring boot with cruderepository
提问by rinuthomaz
My UserRepository
:
我的UserRepository
:
public interface UserRepository extends CrudRepository<User, Integer> {
List<User> findAll(List<Integer> ids);
}
Error:
错误:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type User
引起:org.springframework.data.mapping.PropertyReferenceException:没有找到用户类型的属性 findAll
Can some one tell me how to get list of User
objects based on List of Id's.
有人可以告诉我如何User
根据 Id 列表获取对象列表。
This is working
这是工作
@Query(" select new User(id,x,y,z) from User b where b.id in ?1 ")
List<User> findById(List<Integer> id);
回答by Mico
Firstly, I would rename the repository to UserRepository
, because having 2 User
classes is confusing.
首先,我会将存储库重命名为UserRepository
,因为有 2 个User
类令人困惑。
findAll()
, by definition, is meant to get all the models with no criteria. You should add a method named
findByIdIn(Collection<Integer> ids)
findAll()
,顾名思义,就是获取所有没有标准的模型。您应该添加一个名为的方法
findByIdIn(Collection<Integer> ids)
Use List<User> findAll(Iterable<Integer> ids)
or List<User> findByIdIn(List<Integer> ids)
使用List<User> findAll(Iterable<Integer> ids)
或List<User> findByIdIn(List<Integer> ids)