Mybatis和Mybatis-Plus时间范围查询,亲测有效

一. MySQL

1. mapper.xml

 <if test="queryParams.beginTime!=null and queryParams.beginTime.trim() neq ''">
    and date_format(create_time,'%Y-%m-%d %H:%i:%s') >= date_format(#{queryParams.beginTime},'%Y-%m-%d %H:%i:%s')
  </if>
  <if test="queryParams.endTime!=null and queryParams.endTime.trim() neq ''">
    and date_format(create_time,'%Y-%m-%d %H:%i:%s') &lt;= date_format(#{queryParams.endTime},'%Y-%m-%d %H:%i:%s')
  </if>

2.Mybatis-Plus时间范围查询

 IPage<Record> result = iRecordService.page(new Page<>(pageNum, pageSize),
        new LambdaQueryWrapper<Record>()
            .apply(StrUtil.isNotBlank(beginDate),
                    "date_format (create_time,'%Y-%m-%d') >= date_format('" + beginDate + "','%Y-%m-%d')")
            .apply(StrUtil.isNotBlank(endDate),
                    "date_format (create_time,'%Y-%m-%d') <= date_format('" + endDate + "','%Y-%m-%d')")
            .orderByDesc(Record::getCreateTime()));
posted @ 2020-04-14 10:22  有来技术  阅读(33958)  评论(0编辑  收藏  举报