【发布时间】:2019-01-23 10:55:06
【问题描述】:
我正在使用 Spring data JPA 编写复杂的查询,比如说统计查询。
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StatisticsPair {
private LocalDateTime time;
private BigDecimal balance;
}
@Data
@Entity
@Table(name="t_order")
public class Order {
private LocalDateTime createdTime;
private BigDecimal amount;
private OrderStatus status;
}
在存储库中查询:
@Query("select new xxx.StatisticsPair(createdTime, sum(amount)) from Order where createdTime >= ?1 and createdTime <= ?2 and status = xxx.OrderStatus.COMPLETED group by createdTime")
List<StatisticsPair> statAmountByDay(LocalDateTime from, LocalDateTime to);
方法statAmountByDay 给了我这样的结果:
[
{"time": "2006-01-02 15:04:05", "balance": 100.00}
{"time": "2006-01-02 16:04:05", "balance": 200.00}
{"time": "2006-01-03 15:04:05", "balance": 200.00}
]
但我要按天统计,只需要日期,时间没用,预期结果:
[
{"time": "2006-01-02", "balance": 300.00}
{"time": "2006-01-03", "balance": 200.00}
]
所以我想知道如何将createdTime(datetime type) 转换为Date 类型,就像mysql 中的函数DATE(createdTime),但我不想使用本机查询,因为我的代码在不同的ENV 上运行,使用不同的数据库。
【问题讨论】:
标签: java spring jpa spring-data-jpa spring-data