1. 程式人生 > 其它 >關於介面新增條件(起止時間)導致MySQL資料返回無效的問題

關於介面新增條件(起止時間)導致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') &gt;= date_format(#{sTime},'%Y-%m-%d %h:%i:%s')
          and date_format(etime,'%Y-%m-%d %h:%i:%s') &lt;= date_format(#{eTime},'%Y-%m-%d %h:%i:%s')