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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:28:08  来源:igfitidea点击:

How to use findAll method in spring boot with cruderepository

javaspringspring-bootspring-data-jpa

提问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

Refer - http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html?is-external=true#findAll-java.lang.Iterable-

参考 - http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html?is-external=true#findAll-java.lang.Iterable-

Can some one tell me how to get list of Userobjects 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 Userclasses 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)