SpringDataJPA:使用本机查询的自定义数据映射 [英] SpringDataJPA: custom data mapping with Native Query

查看:378
本文介绍了SpringDataJPA:使用本机查询的自定义数据映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?0", nativeQuery = true)
  User findByEmailAddress(String emailAddress);
}

比方说,我在上面的代码中从用户那里选择*.如果我不希望此方法返回User对象,该怎么办.有没有一种方法可以将数据手动映射到自定义对象MyUser?我可以在UserRepository界面中做所有这些事情吗?

Let's say I have the code above where I select * from user. What should I do if I don't want this method to return User object. Is there a way I can manually map the data to a custom object MyUser? Can I do all this in the UserRepository interface?

谢谢!

推荐答案

您可以执行以下操作

@Query(value = "SELECT YOUR Column1, ColumnN FROM USERS WHERE EMAIL_ADDRESS = ?0", nativeQuery = true)
List<Object[]> findByEmailAddress(String emailAddress);

您必须进行映射.还要看一下Spring Data Repository. 来源

You have to do the mapping. Take a look at the Spring Data Repository as well. Source

这篇关于SpringDataJPA:使用本机查询的自定义数据映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆