如何在 spring boot with crudrepository 中使用 findAll 方法

How to use findAll method in spring boot with cruderepository

我的UserRepository:

public interface UserRepository extends CrudRepository<User, Integer> {
    List<User> findAll(List<Integer> ids);
}

错误:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type User

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

谁能告诉我如何根据 ID 列表获取 User 对象列表。

这是有效的

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

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

findAll(),顾名思义,就是得到所有没有条件的模型。您应该添加一个名为 findByIdIn(Collection<Integer> ids)

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