gpt4 book ai didi

java - 与 @DateTimeFormat 注解一起使用时 Hibernate 返回 0 条记录

转载 作者:行者123 更新时间:2023-11-30 05:52:02 27 4
gpt4 key购买 nike

我在尝试向 UI 显示数据时遇到问题。

当我在 MySQL 中使用此查询从表中获取日期时间日期时。它确实返回了我想要的记录

select flight0_.id as id1_0_, flight0_.arrival_city as arrival_2_0_, flight0_.date_of_departure as date_of_3_0_, flight0_.departure_city as departur4_0_, flight0_.estimated_departure_time as estimate5_0_, flight0_.flight_number as flight_n6_0_, flight0_.operating_airlines as operatin7_0_ from flight flight0_ where flight0_.departure_city='AUS' and flight0_.arrival_city='NYC' and flight0_.date_of_departure='02-05-2018'

这是结果: enter image description here

但是当我使用 Spring 将相同的数据输入到表单并提交时,没有任何返回。我使用的输入是相同的。

enter image description here

这是结果: enter image description here

我在 Controller 类中指定了@DateTimeFormat注释,使用与数据库中显示的模式相同的模式,即“yyyy-MM-dd”

@RequestMapping("findFlights")
public String findFlights(@RequestParam("from") String from, @RequestParam("to") String to,
@RequestParam("departureDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date departureDate,
ModelMap modelMap) {
List<Flight> flights = repository.findFlights(from, to , departureDate);
modelMap.addAttribute("flights", flights);

return "displayFlights";
}

这是我的存储库类:

public interface FlightRepository extends JpaRepository<Flight, Long> {
@Query("select f from Flight f where f.departureCity=:departureCity and f.arrivalCity=:arrivalCity and f.dateOfDeparture=:dateOfDeparture")
List<Flight> findFlights(@Param("departureCity") String from, @Param("arrivalCity") String to, @Param("dateOfDeparture") Date departureDate);
}

为了测试,我尝试删除该注释并停止从该列查询,它按预期工作。所以我很确定 @DateTimeFormat 注释根本不起作用。或者它可以工作,但不知何故 Hibernate 不理解我定义的模式。

enter image description here

我也尝试输入屏幕上显示的值,即 2018-02-05 08:00:00.0 但没有成功。

有人可以给我任何关于这个的想法吗?请帮忙!

最佳答案

谢谢,我自己想出来了。我目前在马来西亚吉隆坡工作。因此默认的“UTC”时区与 MySQL 日期时间格式不匹配。 (仍然使用系统时间)。我在 application.properties 中进行了更改

spring.datasource.url=jdbc:mysql://localhost:3306/reservation?useLegacyDatetimeCode=false&serverTimezone=Asia/Kuala_Lumpur

现在一切正常。

关于java - 与 @DateTimeFormat 注解一起使用时 Hibernate 返回 0 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53746922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com