如何配置从LocalDateTime到LocalDate的转换器



我有一张餐馆及其菜单的桌子。菜肴的创建和更新保存在LocalDateTime中。我想搜索当天的菜单,可以输入一个没有时间的日期,并默认为当前日期。问题的出现是因为我使用LocalDateTime存储菜肴,以便查看确切的时间。如果我指定LocalDateTime,则当前代码有效,但我希望使用LocalDate。我将感谢你的想法^_^

@Entity
@Table(name = "dish")
@Setter
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Dish extends BaseEntity {
public Dish(Integer id, String title, LocalDateTime createdAt, LocalDateTime updatedAt, Integer price) {
super(id);
this.title = title;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.price = price;
}
@Column(name = "title", nullable = false)
@NotEmpty
@Size(max = 128)
private String title;
@CreationTimestamp
@Column(name = "created_at", nullable = false, insertable = false, updatable = false)
@NotNull
private LocalDateTime createdAt;
@UpdateTimestamp
@Column(name = "updated_at", nullable = false, insertable = false, updatable = false)
@NotNull
private LocalDateTime updatedAt;
@Column(name = "price", nullable = false)
@Range(min = 10, max = 100000)
private Integer price;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restaurant_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonBackReference
private Restaurant restaurant;
}

储存库

@Query("SELECT DISTINCT r from Restaurant r JOIN FETCH r.menu m where m.updatedAt = ?1")
List<Restaurant> findAllByMenuByLocalDateTime(LocalDateTime date);

控制器

@GetMapping("/all-menu-of-the-day")
public List<Restaurant> findAllByMenuByLocalDateTime(
@RequestParam(defaultValue = "#{T(java.time.LocalDateTime).now()}")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date) {
return restaurantService.findAllByMenuByLocalDateTime(date);
}

您是否尝试在查询中只从m.updatedAt获取日期?不确定您使用的是SQL还是SQL Server

相关内容

  • 没有找到相关文章

最新更新