我希望在get请求中从我的数据库获得数据,其中LocalDateTime等于LocalDateTime。
@Override
public List<Timeslot> getAllAvailable(LocalDateTime localDateTime) {
return jdbcTemplate.query("select * from timeslot where day = ?", TIMESLOT_ROW_MAPPER);
}
时隙表代码:
CREATE TABLE "timeslot" (
"timeslot_id" serial,
"day" date NOT NULL,
"start_time" TIME NOT NULL,
"end_time" TIME NOT NULL,
"user_id" serial NOT NULL,
"is_recorded" boolean,
CONSTRAINT "timeslot_pk" PRIMARY KEY ("timeslot_id")
);
控制器代码:
@GetMapping("/allAvailable")
public List<Timeslot> getAllAvailable(@RequestParam("day") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime day) {
return userService.allAvailable(day);
}
但是,当我执行这个请求时,控制台的结果是:org.postgresql.util.PSQLException: ERROR: syntax error at end of input
。如何更改sql请求代码以修复此错误?我应该使用PrepareStatement还是其他什么的?
发布于 2022-04-16 04:19:18
我认为您正在将day作为Date
格式存储在数据库中。在查询中,您将比较day (其类型为Date
)和LocalDateTime
类型(这可能是错误的)。首先从Date
中获取LocalDateTime
,然后作为方法参数传递。例如
jdbcTemplate.query("select * from timeslot where day = ?", TIMESLOT_ROW_MAPPER, localDateTime.toLocalDate());
发布于 2022-04-16 00:05:37
正如@AndrewS所提到的,您没有将localDateTime值作为参数传递。因此,jdbcTemplate
不将?
绑定到localDateTime。
您应该使用query
的重载方法并传递localDateTime
作为最后一个参数:
jdbcTemplate.query("select * from timeslot where day = ?", TIMESLOT_ROW_MAPPER, localDateTime);
https://stackoverflow.com/questions/71888661
复制相似问题