一、mysql
1.传入时间范围参数类型是字符串
<if test="startTime!=null and startTime.trim() neq ''">
and date_format(create_time,'%Y-%m-%d %H:%i:%s') >= str_to_date(#{startTime},'%Y-%m-%d %H:%i:%s')
</if>
<if test="endTime!=null and endTime.trim() neq ''">
and date_format(create_time,'%Y-%m-%d %H:%i:%s') <= str_to_date(#{endTime},'%Y-%m-%d %H:%i:%s')
</if>
2.传入时间范围参数类型是Date
<if test="startTime!=null and startTime.trim() neq ''">
and date_format(create_time,'%Y-%m-%d %H:%i:%s') >= date_format(#{startTime},'%Y-%m-%d %H:%i:%s')
</if>
<if test="endTime!=null and endTime.trim() neq ''">
and date_format(create_time,'%Y-%m-%d %H:%i:%s') <= date_format(#{endTime},'%Y-%m-%d %H:%i:%s')
</if>
3.Mybatis-Plus时间范围查询
Page<Record> page = new Page<>(page, limit);
IPage<Record> result = iRecordService.page(page,
new LambdaQueryWrapper<Record>()
.apply(StrUtil.isNotBlank(start_date),
"date_format (optime,'%Y-%m-%d') >= date_format('" + start_date + "','%Y-%m-%d')")
.apply(StrUtil.isNotBlank(end_date),
"date_format (optime,'%Y-%m-%d') <= date_format('" + end_date + "','%Y-%m-%d')")
.orderByDesc(HmsFaceDetectLog::getOptime));
Mybatis Plus 时间查询无效
错误写法
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
QueryWrapper query = new QueryWrapper();
query.ge("create_time", LocalDateTime.parse("2019-05-13 23:00:00", df));
正确写法
query.apply("UNIX_TIMESTAMP(create_time) >= UNIX_TIMESTAMP('" + "2019-05-12 03:00:00" + "')");
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。