从JpaRepository Native Query返回一个字符串?

时间:2016-05-31 09:13:33

标签: java sql string repository spring-data

我正在尝试使用native query来返回表格中某个column的值。

我知道原生查询可用于返回对象,即在此示例中?:

public interface UserRepository extends JpaRepository<User, Long> {

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

但是,如何写这个以返回一列的值?即如果我只想返回用户的名称(字符串), 用户object

1 个答案:

答案 0 :(得分:5)

试试这个:

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT u.name FROM USERS u WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
  String findByEmailAddress(String emailAddress);
}