【问题标题】:How can i return only specific information with findAll() instead of everything?我如何才能使用 findAll() 而不是所有内容只返回特定信息?
【发布时间】:2020-06-24 15:19:21
【问题描述】:

例如我有 Repository 类:

public interface PersonRepo extends JpaRepository<Person, Long>, JpaSpecificationExecutor<Person>

我想使用 JpaSpecificationExecutor 提供的 findAll() 方法。

如果我想获取所有用户,它会返回完整的用户 DTO-s,包括加密密码和用户角色等...

PersonRepo.findAll()

我如何告诉 findAll 只发送姓名和电子邮件而不是所有内容。

我使用 Mapstruct 将我的 Person 类转换为 PersonDTO。

【问题讨论】:

  • 保持 findAll() 不变。您可以创建调用 findAll() 并存储在本地列表中的方法。然后你可以过滤你想要的并返回新的列表

标签: java spring jpa dto findall


【解决方案1】:

正如here 所述,您必须定义自己的方法并为其使用@Query 注释。

【讨论】:

  • @giliermo 如果您觉得我的回答有帮助,很高兴接受它作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 2020-02-07
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多