Mybatis $符號防止注入

在mybatis的xml語句中 根據情況不同 採用不同的排序方式 

Xml代碼 

<if test="ordercolumn != null">  
    ORDER BY #{ordercolumn} DESC  
</if>  


但是沒有生效,參考別人意見後,原來預編譯時,將ordercolumn字段名轉爲字符串String格式,比如ordercolumn="name", sql語句是 

Sql代碼 

 

ORDER BY “name” DESC  


所以排序無法生效。 

    解決辦法:將xml按如下修改 

Xml代碼 

<choose>  
    <when test="ordertype!=null and ordertype==1">  
        ORDER BY carindex DESC  
    </when>  
    <when test="ordertype!=null and ordertype==2">  
        ORDER BY statdate DESC  
    </when>  
    <otherwise>  
        ORDER BY carindex DESC  
    </otherwise>  
</choose>  



傳參數ordertype使用枚舉形式,排序生效。 

 

2.可以使用 特殊字符串轉義和過濾

限制入參長度
嘗試解決

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章