Solution directory
Method 1: ge method (greater than or equal to >=) and le method (less than or equal to <=)
@Override
public IPage<HealthCheckCommentReport> getPageList(HealthCheckCommentReport healthCheckCommentReport, PageRequestVo pageRequestVo) {
Page<HealthCheckCommentReport> page = new Page<>();
page.setCurrent(pageRequestVo.getPageIndex());
page.setSize(pageRequestVo.getPageSize());
// 查询条件
LambdaQueryWrapper<HealthCheckCommentReport> queryWrapper = Wrappers.<HealthCheckCommentReport>lambdaQuery();
queryWrapper.eq(Objects.nonNull(healthCheckCommentReport.getEntId()), HealthCheckCommentReport::getEntId,healthCheckCommentReport.getEntId());
Optional.ofNullable(pageRequestVo.getStartTime()).ifPresent(
date -> queryWrapper.ge(HealthCheckCommentReport::getCheckTime, date));
Optional.ofNullable(pageRequestVo.getEndTime()).ifPresent(
date -> queryWrapper.le(HealthCheckCommentReport::getCheckTime, date));
queryWrapper.orderByDesc(HealthCheckCommentReport::getCreateTime);
return baseMapper.selectPage(page,queryWrapper);
}
备注
: The optional syntax of java8 is used here, the principle is actually the if else judgment
Method 2: Use the apply method to write sql statements
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());
;
备注
: The apply method here writes the where condition
Method 3: Use the between method to realize the time period query.
LocalDateTime startTime = LocalDateTime.parse("2022-01-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime endTime = LocalDateTime.parse("2022-01-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.between("create_time", startTime, endTime);
List<User> userList = userMapper.selectList(queryWrapper);
In the above code, we implemented the query of the create_time field within the specified time period through the between method.