這裏使用的是@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,類型,所以我進行了一下轉換。