这里使用的是@Query
@Query(value = "select new OrderDetail (productId,productName,sum(productQuantity) as productQuantity) from OrderDetail where " +
"TIMESTAMPDIFF(DAY,:time,createTime) = 0 " +
"group by productId,productName")
List<OrderDetail> findByCreateTime(@Param("time") String timeStamp);
写这个sql语句必须提供相应的构造方法,默认的构造方法和部分字段的构造方法
实体类
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class OrderDetail {
@Id
private String detailId;
private String orderId;
private String productId;
private String productName;
private BigDecimal productPrice;
private Integer productQuantity;
private String productIcon;
//创建时间
private Date createTime;
//修改时间
private Date updateTime;
public OrderDetail(String productId, String productName, Long productQuantity) {
this.productId = productId;
this.productName = productName;
this.productQuantity = Integer.parseInt(productQuantity.toString());
}
}
因为SUM函数返回的是long,类型,所以我进行了一下转换。