Jooq查询时间的条件写法
国内Jooq的文档实在太少了,需要用到时间查询的时候居然找不到任何资料。自己整理了一下几种用法。
查询在某一天的记录
public List<Record> findList(LocalDate date){
return create
.selectFrom(Record)
.where(Record.createDate
.between(date.atStartOfDay(), date.plusDays(1).atStartOfDay())
)
.fetch()?.into(Record.class)
}
date的格式是“2017-03-09“,这里createDate
的时间格式是LocalDateTime
,所以我将date
转换成LocalDateTime
。解析出来的sql语句是这样的'record'.'create_date' between '2017-03-09T00:00' and '2017-03-10T00:00'
。
查询从某一天到某一天的记录
public List<Record> findList(LocalDate date){
return create
.selectFrom(Record)
.where(Record.createDate
.cast(LocalDate.class)
.between(date, date.plusDays(1))
)
.fetch()?.into(Record.class)
}
这里我将createDate
转换成了LocalDate
,再查询在“2017-03-09”和“2017-03-10”之间的记录。这里返回的结果是包含“03-09”到“03-10”两天的。
查询在某一天之前的记录(不包括作参数的那天)
将between(date, date.plusDays(1))
改成 lessThan(date)
查询在某一天之后的记录(不包括作参数的那天)
将between(date, date.plusDays(1))
改成 greaterThan(date)
查询在某一天之前的记录(包括作参数的那天)
将between(date, date.plusDays(1))
改成 lessOrEqual(date)
查询在某一天之后的记录(包括作参数的那天)
将between(date, date.plusDays(1))
改成 greaterOrEqual(date)
PS:直接在
.where(Record.createDate.cast(LocalDate.class).eq(date)
条件语句中判断equals,理论上是可行的。然而我这边生成的sql放在数据库中能够返回数据的,但是程序中返回空集。如有大神知道原因,麻烦告知,谢谢。
转载请注明出处
感觉自己萌萌哒
http://blog.csdn.net/qq_19937081/article/details/60964024