【发布时间】:2019-05-13 18:55:10
【问题描述】:
我在尝试向 UI 显示数据时遇到问题。
当我在 MySQL 中使用此查询从我的表中获取 Datetime 日期时。它确实返回了我想要的记录
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'
但是当我使用 Spring 将相同的数据输入到表单并提交时,没有任何返回。我使用的输入是一样的。
我在我的控制器类中指定了@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 不理解我定义的模式。
我也试过输入屏幕上显示的值,是 2018-02-05 08:00:00.0 但没有运气。
有人可以给我任何想法吗?请帮忙!
【问题讨论】:
标签: java spring hibernate jpa datetime-format