gpt4 book ai didi

spring - @Query无法识别参数是否在Spring Framework中的单引号内

转载 作者:行者123 更新时间:2023-12-02 12:48:16 25 4
gpt4 key购买 nike

我在@Query中使用Spring Data有一个大问题。

我有以下查询:

SELECT created_at::DATE "date",
count(*)
FROM
absence
WHERE
created_at::DATE between '2018-05-27' AND '2018-05-31'
GROUP BY created_at::DATE;

这个查询现在可以在postgres中正常使用了,现在可以在 Spring 使用:
  @Query("SELECT created_at\\:\\:DATE \"date\",count(*) FROM absence " +
"WHERE created_at\\:\\:DATE between '?1' AND '?2' " +
"GROUP created_at\\:\\:DATE", nativeQuery = true)
fun getAbsenceCount(beginDate: String,endDate: String): List<AbsenceCount>

该查询根本不起作用,问题是spring无法识别parather beginDate(?1)和endDate(?2)。
我尝试了很多来自stackoverflow的解决方案,例如 solutionsolution 2,但我无法摆脱这个问题。
我不知道这是故意的还是 Spring 的错误。

最佳答案

尝试简单地使用经典的SQL cast函数:

@Query(value = "" + 
"select " +
" cast(a.created_at as date) as createdAt, " +
" count(*) as quantity " +
"from " +
" absence a " +
"where " +
" cast(a.created_at as date) between ?1 and ?2 " +
"group " +
" cast(a.created_at as date)" +
"", nativeQuery = true)
fun getAbsenceCount(beginDate: String, endDate: String): List<AbsenceCount>

其中 AbsenceCount是这样的投影(java):
public interface AbsenceCount {
LocalDate getCreatedAt();
Long getQuantity();
}

关于spring - @Query无法识别参数是否在Spring Framework中的单引号内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50694467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com