简体   繁体   English

如何使用Spring Boot从自定义查询返回包含少量字段的自定义列表?

[英]How to return a custom List with few fields from a custom query with Spring Boot?

I'm developing a REST web service with Spring Boot 2 and Java 8. I have a problem in creating a list with few fields compared to when I get a list with all the fields of a table. 我正在使用Spring Boot 2和Java 8开发REST Web服务。与使用表的所有字段列表时相比,我在创建包含少量字段的列表时遇到问题。

@Query("SELECT c.idCliente, c.nome, c.cognome, c.username, c.email FROM Cliente c WHERE c.username LIKE %:username%")
List<Cliente> findClienteByUsername(@Param("username") String username);

The result is: 结果是:

[
   3,
   null,
   null,
   "user",
   "aaa"
]

But when show all fields get this: 但是当显示所有字段时都会得到:

{"idCliente":3,"username":"user","nome":null,"cognome":null,"dataNascita":null,"email":"aaa","password":"ciao","dataCreazione":"2018-11-23T21:33:31.000+0000","instagram":null,"facebook":null,"google":null,"twitter":null,"attivo":"1","ultimaSospensione":null}

Why can't I get the field names along with the values? 为什么我不能获得字段名称和值?

While you have to select a few fields you have to go with Spring Projection through DTO class. 虽然您必须选择几个字段,但您必须通过DTO课程使用Spring Projection。 You have to define the constructor with the selected fields. 您必须使用所选字段定义构造函数。

@Query("SELECT new Cliente(c.idCliente, c.nome, c.cognome, c.username, c.email) FROM Cliente c WHERE c.username LIKE %:username%")
List<Cliente> findClienteByUsername(@Param("username") String username);

You can refer below link with detailed information. 您可以在下面链接中查看详细信息。 https://smarterco.de/spring-data-jpa-query-result-to-dto/ https://smarterco.de/spring-data-jpa-query-result-to-dto/

Or you can do the manual translation for each row using a mapper function. 或者,您可以使用映射器功能对每行进行手动转换。

 @Query("SELECT c.id, c.nome, c.cognome, c.username, c.email FROM Cliente c WHERE c.username LIKE %:username%")
List<Object[]> findClientDeatailsByuserName(@Param("username")String username);

Try this one: Create the below mentioned constructor into an Cliente entity class. 试试这个:在Cliente实体类中创建下面提到的构造函数。

Cliente(int idCliente,String noame,String cognome,String username,String email){
   this.idCliente=idCliente;
   this.nome=nome;
   this.cognome=cognome;
   this.username=username;
   this.email=email;   
 }

Call the query as: 将查询调用为:

@Query("SELECT new packageName.Cliente(c.idCliente, c.nome, c.cognome, c.username, c.email) FROM Cliente c WHERE c.username LIKE %:username%")
List<Cliente> findClienteByUsername(@Param("username") String username);

It will show all fields with value. 它将显示所有具有价值的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 spring 引导中注入自定义 bean 列表 - How to inject custom list of beans in spring boot 如何使用Spring Boot从Class中仅返回几种类型? - How to return only few types from Class using spring boot? 如何从 Spring Data JPA GROUP BY 查询返回自定义对象 - How to return a custom object from a Spring Data JPA GROUP BY query spring boot jpa:从与表架构无关的jpa查询返回自定义对象 - spring boot jpa: return custom object from jpa query not related to table schema elasticsearch 中的自定义查询与 spring 引导 - Custom query in elasticsearch with spring boot Spring 在 Controller 中启动自定义查询 - Spring Boot Custom Query In Controller 如何使用自定义注释在Spring Boot中隐藏带注释的字段? - How to hide annotated fields in Spring Boot using custom annotations? 从 spring 引导自定义验证器返回不同的 HTTP 状态代码 - Return different HTTP status code from spring boot custom validators 如何使用Spring Data在Spring Boot中从实体中选择几个字段? - How to select few fields from a Entity in Spring Boot using Spring Data? 如何在 Spring Boot 中为 /error 页面返回自定义错误消息 - How to return custom error message in Spring Boot for /error page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM