你好我有实体并且已经创建了如此指定的字段:
@JsonIgnore
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
protected Date createdAt;
每个实体都包含此字段。现在我有复杂的SQL查询来计算日期之间的利润。查询如下所示:
@Query("SELECT SUM(CASE WHEN t.outcome = true THEN - t.amount ELSE t.amount END) as income " +
"FROM Transaction t " +
"WHERE t.property = :property " +
"AND t.createdAt BETWEEN :dateFrom AND :dateTo")
Double getPropertyProfitBetweenDates(@Param("property")Property property, @Param("dateFrom")Date dateFrom, @Param("dateTo")Date dateTo);
根据需要解析日期的函数如下所示:
Calendar start = Calendar.getInstance();
start.add(Calendar.MONTH, -i);
start.set(Calendar.DAY_OF_MONTH, 1);
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
Calendar end;
end = (Calendar) start.clone();
end.add(Calendar.MONTH, 1);
end.add(Calendar.DAY_OF_MONTH, -1);
Date dateFrom = start.getTime();
Date dateTo = end.getTime();
当我尝试通过存储库访问数据时,我调用这样的方法:
transactionService.getPropertyProfitBetweenDates(property, dateFrom, dateTo)
每次我收到null,但是当我在mysql workbench中mannualy运行查询时,我得到了正确的记录。你能帮我解决这个问题吗?
答案 0 :(得分:0)
我使用java.sql.Date