1. 程式人生 > >Mybatis日期類型的關系判斷

Mybatis日期類型的關系判斷

span != myba 使用 表示 關系運算符 AR art bsp

進行時間段的查詢時,在mapper文件中直接使用">","<"等關系運算符是無法解析的

<if test="executeStartDate != null and executeStartDate != ‘‘">
     and execute_time >= to_date(#{executeStartDate},‘yyyy-MM-dd HH24:MI:SS‘)
</if>
<if test="executeEndDate != null and executeEndDate != ‘‘">
     and execute_time 
<= to_date(#{executeEndDate},‘yyyy-MM-dd HH24:MI:SS‘) </if>

解決方法有兩種,一種是使用"&gt;","&lt;"來表示大於和小於關系,這樣,在解析時,這些特殊字符會被轉義成所匹配的運算符

<if test="executeStartDate != null and executeStartDate != ‘‘">
      and execute_time &gt;= to_date(#{executeStartDate},‘yyyy-MM-dd HH24:MI:SS‘)
</if> <if test="executeEndDate != null and executeEndDate != ‘‘"> and execute_time &lt;= to_date(#{executeEndDate},‘yyyy-MM-dd HH24:MI:SS‘) </if>

另一種是使用"<![CDATA[ ]]>"來嵌套不需要轉義的內容

<if test="executeStartDate != null and executeStartDate != ‘‘">
     and execute_time 
<![CDATA[>=]]> to_date(#{executeStartDate},‘yyyy-MM-dd HH24:MI:SS‘) </if> <if test="executeEndDate != null and executeEndDate != ‘‘"> and execute_time <![CDATA[<=]]> to_date(#{executeEndDate},‘yyyy-MM-dd HH24:MI:SS‘) </if>

Mybatis日期類型的關系判斷