关于接口添加条件(起止时间)导致MySQL数据返回无效的问题
场景:
项目的一个接口,条件由原来的 id 变更为了 id 、stime、etime,因此用了实体类来作参数接收,其余语句均改为实体类
报错:项目无报错,但是测试无返回数据,debug可以看到,语句有查询到数据库字段,但是字段无数据。使用SQL在数据库查询无问题
原因,xml文件中的SQL对查询到的时间数据没有进行格式化,导致传入的时间参数格式和查询到的时间格式不一致,没有办法比对。
原SQL
<select id="getWorkTimeOneDay" resultType="entity.TimeEntity" parameterType="String"> SELECT DATE_FORMAT(stime,"%Y-%m-%d %H:%i:%s") as sTime, DATE_FORMAT(e_time,"%Y-%m-%d %H:%i:%s") as eTime, id as vId FROM task_info where stime IS NOT NULL AND etime IS NOT NULL <if test="id != null"> AND id = #{vId} </if> and vid is not null
//以下是需要修改的语句 and stime >= #{stime} and etime <= #{etime} </select>
修改后
and date_format(stime,'%Y-%m-%d %h:%i:%s') >= date_format(#{sTime},'%Y-%m-%d %h:%i:%s')
and date_format(etime,'%Y-%m-%d %h:%i:%s') <= date_format(#{eTime},'%Y-%m-%d %h:%i:%s')

浙公网安备 33010602011771号