1. 程式人生 > >xml裡寫sql語句大於,小於的正確方法

xml裡寫sql語句大於,小於的正確方法

今天把原來的xml裡的選擇時間段內的方法改了一下,原來寫的是在兩個時間之間,用between … and …,這樣做的話,兩個引數,起始日期和終止日期必須都要輸入,不能滿足查詢某個時間之前,或者之後的功能,於是我就想改成下面這樣:

<if test="start != null and start != 'undefined' and start != ''">
    AND date_format(gather_time,'%Y-%m') >= #{start}                                    
</if>
<if
test="stop != null and stop != 'undefined' and stop != ''"> AND date_format(gather_time,'%Y-%m') <= #{stop} </if>

但是無法執行,後來查資料知道是xml的格式問題,在xml裡,不能用 < , >,= 之類的符號。一般都是用下面的字元來代替<、>、=,如下所示:

所以上面的sql語句應該這樣寫:

<if test="start != null and start != 'undefined' and start != ''"
> AND date_format(gather_time,'%Y-%m') &gt;= #{start} </if> <if test="stop != null and stop != 'undefined' and stop != ''"> AND date_format(gather_time,'%Y-%m') &lt;= #{stop} </if>

這下就可以順利執行了。