方式一
通过apply方法,来实现时间范围查询,该方法可用于数据库函数,动态入参的params对应前面applySql内部的{index}部分,这样是不会有sql注入风险的,反之会有!
queryWrapper.apply(serviceItemListDto.getStartTime() != null,
"date_format (create_time,'%Y-%m-%d') >= date_format ({0},'%Y-%m-%d')", serviceItemListDto.getStartTime())
.apply(serviceItemListDto.getEndTime() != null,
"date_format (create_time,'%Y-%m-%d') <= date_format ({0},'%Y-%m-%d')", serviceItemListDto.getEndTime());
;
方式二
除了使用apply还可以使用ge(大于等于 >=)和le(小于等于 <=)
LambdaQueryWrapper<ObsConsultationRequest> qw = new LambdaQueryWrapper<>();
Optional.ofNullable(request.getStartRequestDatetime()).ifPresent(
date -> qw.ge(ObsConsultationRequest::getRequestDatetime, date));
Optional.ofNullable(request.getEndRequestDatetime()).ifPresent(
date -> qw.le(ObsConsultationRequest::getRequestDatetime, date));