从存储库接口检索列表到DTO列表类的最佳方法是什么 [英] What is the best way to Retrieve List from repository interface to DTO list Class

查看:134
本文介绍了从存储库接口检索列表到DTO列表类的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DTO软件包中有一个类名DailyElectricity,它包含
max,min,sum,getter和setter的平均值

There is class name DailyElectricity which is in DTO package and it contain max , min, sum , average with getter and setter

public class DailyElectricity implements Serializable {

  private static final long serialVersionUID = 3605549122072628877L;


  private LocalDate date;

  private Long sum;

  private Double average;

  private Long min;

  private Long max;


}

有一个接口,其作用是从数据库获取数据

there is interface which its job is to get data from database

@RestResource(exported = false)
public interface HourlyElectricityRepository 
    extends PagingAndSortingRepository<HourlyElectricity,Long> {
  Page<HourlyElectricity> findAllByPanelIdOrderByReadingAtDesc(Long panelId,Pageable pageable);


  @Query("SELECT max(a.generatedElectricity), sum(a.generatedElectricity),min(a.generatedElectricity),max(a.generatedElectricity)  from HourlyElectricity a where DATE_FORMAT(reading_at,'%Y-%m-%d')=DATE_FORMAT(CURDATE()-1,'%Y-%m-%d') and  panel_id=:panelId")
  List<DailyElectricity> getStaticsDailyElectricity(@Param("panelId")Long panelId);

}

但是当我调用API时,它会给出

it complie without any exception but when i call API it give

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.techtrial.dto.DailyElectricity]

它无法隐藏到dto类

推荐答案

问题是春天没有办法弄清楚如何将查询结果转换为期望的自定义​​对象 DailyElectricity ;为了使这种映射成为可能,您需要做两件事:

The issue is that there is no way for spring to figure out how to convert the result of your query to the custom object DailyElectricity you are expecting from; in order to make this mapping possible you need to do two things:


  1. 创建一个构造函数,以便可以创建一个新对象并通过查询的每一行产生的值将其初始化:

  1. create a constructor to so you can create a new object and initialize it through the value resulted from each row of you query:

public DailyElectricity (Long max,Long sum,Long min,Double average){    
    this.sum=sum;
    this.average=average;
    this.min=min;
    this.max=max;   
}


  • 然后在<$ c $中对查询使用以下结构c> HourlyElectricityRepository

    @Query("SELECT new com.example.DailyElectricity( max(a.generatedElectricity), sum(a.generatedElectricity),min(a.generatedElectricity),avg(a.generatedElectricity))  from HourlyElectricity a where DATE_FORMAT(reading_at,'%Y-%m-%d')=DATE_FORMAT(CURDATE()-1,'%Y-%m-%d') and  panel_id=:panelId")
       List<DailyElectricity> getStaticsDailyElectricity(@Param("panelId")Long panelId);
    




    • 请注意我在查询中使用的软件包名称( com.example.DailyElectricity ),并确保在测试之前使用与项目相对应的正确软件包名称。

      • Please notice the package name I have used in the Query (com.example.DailyElectricity) and make sure you are using the right package name corresponding to you project before testing.
      • 这篇关于从存储库接口检索列表到DTO列表类的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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