仓库:
包com.apsilabs.meterwebapi.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.apsilabs.meterwebapi.model.ActualsBlockLoad;
import com.apsilabs.meterwebapi.viewmodel.ActualsBlock;
@Repository
public interface ActualsBlockLoadRepository extends JpaRepository<ActualsBlockLoad, Long> {
@Query(nativeQuery=true,value= "SELECT NEW com.apsilabs.meterwebapi.viewmodel.ActualsBlock(CONVERT(hour(rtc), CHAR(50)) as hours,CONVERT(date(rtc), CHAR(50)) as dates,CONVERT(IFNULL(avg(JSON_EXTRACT(JSON_UNQUOTE(data),'$.\\\"1012270255\\\"')),0),CHAR(50)) as voltage) from actuals_block_load group by hours,dates order by dates,hours")
public List<ActualsBlock> getActualsBlockLoad();
}
我有一个错误:
您的SQL语法有误;检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第1行的'.apsilabs.meterwebapi.viewmodel.ActualsBlock(CONVERT(hour(rtc),CHAR(50))作为hou'附近使用
新课程:
package com.apsilabs.meterwebapi.viewmodel;
public class ActualsBlock {
private String hours;
private String dates;
private String voltage;
public String getHours() {
return hours;
}
public void setHours(String hours) {
this.hours = hours;
}
public String getDates() {
return dates;
}
public void setDates(String dates) {
this.dates = dates;
}
public String getVoltage() {
return voltage;
}
public void setVoltage(String voltage) {
this.voltage = voltage;
}
public ActualsBlock(String hours, String dates, String voltage) {
super();
this.hours = hours;
this.dates = dates;
this.voltage = voltage;
}
public ActualsBlock() {
super();
}
}
最佳答案
您已使用nativeQuery = true且此查询不是本机查询。
SELECT NEW com.apsilabs.meterwebapi.viewmodel.ActualsBlock(CONVERT(hour(rtc), CHAR(50)) as hours,CONVERT(date(rtc), CHAR(50)) as dates,CONVERT(IFNULL(avg(JSON_EXTRACT(JSON_UNQUOTE(data),'$.\\\"1012270255\\\"')),0),CHAR(50)) as voltage) from actuals_block_load group by hours,dates order by dates,hours.
为什么要使用新的com.apsilabs.meterwebapi.viewmodel.ActualsBlock(CONVERT .... if是本机查询