mybatis-plus时间段查询的几种方式
·
方式一:ge方法(大于等于 >=)和le方法(小于等于 <=)
@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);
}
备注
:此处用了java8的Optional的语法,原理其实就是if else判断而已
方式二:通过apply方法,进行编写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方法编写的就是where条件
方式三:通过 between 方法实现时间段查询。
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);
在上面的代码中,我们通过 between 方法实现了对 create_time 字段在指定时间段内的查询。
更多推荐
已为社区贡献5条内容
所有评论(0)