MyBatis的xml中SQL處理小於號與大於號

         這種問題在xml在SQL語句中的where約束中直接寫>或<號是會報錯 ,這就要求我們來進行特殊處理。

其實很簡單,我們只需作如下轉義替換即可避免上述的錯誤:。

< <= > >= & ' "

&lt;

&lt;=

&gt;

&gt;=

&amp;

&apos;

&quot;

例如常見的時間比較:

錯誤寫法
<select id="select" parameterType="xxx" resultMap="xxx">
    select
        distinct
        <include refid="Base_Column_List" />
    from xxx
    <where>
        <if test="createDate != null">
            create_date <= #{createDate}
        </if>
    </where>
</select>
正確寫法
<select id="select" parameterType="xxx" resultMap="xxx">
    select
        distinct
        <include refid="Base_Column_List" />
    from xxx
    <where>
        <if test="createDate != null">
            create_date &lt;= #{createDate}
        </if>
    </where>
</select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章