mybatis框架支持直接用>=或<=查询日期范围,如:

<if test="startTime != null and startTime != ''">

   and timer.create_time  >= #{startTime}

<if test="endTime != null and endTime != ''">

    and timer.create_time  <= #{endTime}

</if>

问题:开始时间和结束时间是同一个日期,就查不出来
解决方法:用DATE_FORMAT转换日期

<if test="ew.plannedStartDate != null">
    <![CDATA[ and DATE_FORMAT(planned_start_date, '%Y-%m-%d') >= #{ew.plannedStartDate} ]]>
</if>
<if test="ew.plannedEndDate != null">
    <![CDATA[ and DATE_FORMAT(planned_start_date, '%Y-%m-%d') <= #{ew.plannedEndDate} ]]>
</if>

mybatis -plus框架中:

.apply(ObjectUtil.isNotEmpty(dto.getStartTime()),"planned_complete_date >= DATE_FORMAT('"+dto.getStartTime()+"','%Y-%m-%d')")
.apply(ObjectUtil.isNotEmpty(dto.getEndTime()),"planned_complete_date <= DATE_FORMAT('"+dto.getEndTime()+"','%Y-%m-%d')");

更多推荐

mybatis 时间范围查询