【问题标题】:how to collect specific field in spring query creation?如何在春季查询创建中收集特定字段?
【发布时间】:2022-01-24 14:16:06
【问题描述】:

我为用户创建了 spring api,他们可以使用旧密码和公司来获取新的用户名和密码。我们如何在 Spring 查询创建中选择特定字段?

我创建了这样的仓库:

@Repository
public interface UserRepository extends JpaRepository<User, String> {

    List<User> findUserByCompanyAndOldUsername(String company, String oldUsername);

}

然后我像这样返回行表的数据:

@GetMapping("/index")
public ResponseEntity<List<User>> getUserByCompanyAndOldUsername(@RequestParam String company,
                                                                 @RequestParam String oldUsername){
    return new ResponseEntity<>(userRepository.findUserByCompanyAndOldUsername(company, oldUsername), HttpStatus.OK);
}

结果:

  {
    "id": 1,
    "oldUsername": "FDIFSA00101",
    "customerKey": "FRIFSA00101",
    "company": "FRI",
    "newUser": "FSA00101",
    "oldPassword": "$2a$10$36dHjzxiEpvrjVPE6OhzkeiinNZt3/H7SjpKEI/LHqBAJ0MAZD6O2",
    "newPassword": "Password"
  }

我只需要 newUser 和 newPassword 数据来显示在 thymeleaf html 页面上,而不是所有数据。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    在反序列化到您的 dto 时忽略这些字段。您可以使用:

    @JsonIgnore
    @JsonProperty(value = "field_name")
    

    在字段上方或在您不希望看到任何响应值的 getter 方法之前使用这些注释。它应该可以工作。

    注意:对于驼峰命名,请遵循以下样式:firstNamefirst_name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 2018-02-02
      相关资源
      最近更新 更多