標籤:htm relative 也會 order by 預設 log ext r檔案 ges
MyBatis mapper檔案中的變數引用方式#{}與${}的差別
內容來源:http://blog.csdn.net/szwangdf/article/details/26714603
預設情況下,使用#{}文法,MyBatis會產生PreparedStatement語句中,並且安全的設定PreparedStatement參數,這個過程中MyBatis會進行必要的安全檢查和轉義。
樣本1:
執行SQL:Select * from emp where name = #{employeeName}
參數:employeeName=>Smith
解析後執行的SQL:Select * from emp where name = ?
執行SQL:Select * from emp where name = ${employeeName}
參數:employeeName傳入值為:Smith
解析後執行的SQL:Select * from emp where name =Smith
綜上所述、${}方式會引發SQL注入的問題、同時也會影響SQL語句的先行編譯,所以從安全性和效能的角度出發,能使用#{}的情況下就不要使用${}
但是${}在什麼情況下使用呢?
有時候可能需要直接插入一個不做任何修改的字串到SQL語句中。這時候應該使用${}文法。
比如,動態SQL中的欄位名,如:ORDER BY ${columnName}
注意:當使用${}參數作為欄位名或表名時、需指定statementType為“STATEMENT”,如:
[html] view plain copy
- <select id="queryMetaList" resultType="Map" statementType="STATEMENT">Select * from emp where name = ${employeeName} ORDER BY ${columnName}</select>
mybatis中$和#java代碼示範