【问题标题】:How to use findAll method in spring boot with cruderepository如何在带有 crudrepository 的 Spring Boot 中使用 findAll 方法
【发布时间】:2016-05-24 11:31:16
【问题描述】:

我的UserRepository

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

错误:

原因: 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-

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

这是有效的

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

【问题讨论】:

  • 这不是一个可行的解决方案吗? @Query(" select b from User b where b.id in ?1 ") 似乎是正确的
  • @AliDehghani 是的,这是可行的解决方案。

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

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

findAll(),顾名思义,就是获取所有没有条件的模型。您应该添加一个名为的方法 findByIdIn(Collection&lt;Integer&gt; ids)

使用List&lt;User&gt; findAll(Iterable&lt;Integer&gt; ids)List&lt;User&gt; findByIdIn(List&lt;Integer&gt; ids)

【讨论】:

  • 那么这很有趣,也许他需要使用 Iterable 而不是 List 呢?或者,也许他正在混合这些类(存储库和实体类,因为它们具有相同的名称)。但是 findByIdIn 应该可以正常工作。
  • 1)List findAll(Iterable ids); -> 工作 2)List findByIdIn(List ids); -> 工作请在您的答案中添加两者,我会接受。谢谢
  • 已编辑。 (太短了……)
  • @rinuthomaz 非常感谢。 findAllByIdIn 也适用于我。
猜你喜欢
  • 1970-01-01
  • 2018-05-12
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 2018-08-01
相关资源
最近更新 更多