如何在springrepository的spring boot中使用findAll方法

rin*_*maz 0 java spring spring-data-jpa spring-boot

我的UserRepository:

public interface UserRepository extends CrudRepository<User, Integer> {
    List<User> findAll(List<Integer> ids);
}
Run Code Online (Sandbox Code Playgroud)

错误:

引起:org.springframework.data.mapping.PropertyReferenceException:找不到类型User的属性findAll

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

有人可以告诉我如何获取User基于Id 列表的对象列表.

这很有效

@Query(" select new User(id,x,y,z) from User b where b.id in ?1 ")
List<User> findById(List<Integer> id);
Run Code Online (Sandbox Code Playgroud)

Mic*_*ico 6

首先,我会将存储库重命名为UserRepository,因为有2个User类令人困惑.

findAll()根据定义,是为了获得没有标准的所有模型.您应该添加一个名为的方法 findByIdIn(Collection<Integer> ids)

使用List<User> findAll(Iterable<Integer> ids)List<User> findByIdIn(List<Integer> ids)