使用mybatis遇到的關於條件查詢">"以及if test傳參的使用問題

第一個:傳參後判斷的問題

Map map = new HashMap();
map.put("str","1,2");
//將map當做參數傳進去
public list dotest(Map map);
//後臺的xml中執行的sql
<select  result="map"  resultType="map"> 
    select * from t_table 
    where 1=1
    <!--這種寫法是沒有問題的,不等於可以寫-->
    <if test="str!=null and str!='' and str !='1,2'">
        name = #{str}
    </if>
    <!--如果這樣寫就會報錯 str不能讓它與字符串比較-->
    <if test="str!=null and str =='1,2'">
        name = #{str}
    </if>
</select>

第二個:sql中不能直接使用 “<” 或者 “>”
還是用上面代碼說明下

<select  result="map"  resultType="map"> 
    select * from t_table 
    where 1=1
    <!--"<="一起寫是可以的-->
    <if test="str!=null" >
        id <= '3' 
    </if>
    <!--如果這樣寫就會報錯,">"這個符號會被默認成結束字符而不是大於號-->
    <if test="str==null">
        id > '5'
    </if>
    <!--因此你可以改成這樣,使用xml過濾成普通文本-->
    <if test="str==null">
        <![data[ id > '5']]>
    </if>
</select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章