#{}是預編譯處理,${}是字串替換。#{}對傳入的引數進行解析,mybatis建立預編譯語句的時候,會為引數設定值,而${}是將${}替換成變數的值。
1. #{}是預編譯處理,${}是字串替換
如果name的值是zhangsan,那麼兩者編譯之後的沒有區別,均為select * from user where name = #;
select * from user where name = $;
select * from user where name = 'zhangsan';
只是預處理時的方式不一樣,mybatis在處理#{}時,會將sql中的#{}替換為?號,呼叫preparedstatement的set方法來賦值;
mybatis在處理${}時,就是把${}替換成變數的值。
3. #{}方式能夠很大程度防止sql注入,提高系統安全性
4.${}方式存在sql注入攻擊的可能
select * from $ where name = #
如果tablename的值是 user;delete from user;-- 那麼
5.${}方式一般用於傳入資料庫物件,例如傳入表名等就只能用${}(例子見第4點)
6.一般能用#{}的就別用${}
myBatis中 和 區別
1.將傳入的資料都當成乙個字串,會對自動傳入的資料加乙個雙引號。如 order by user id 如果傳入的值是111,那麼解析成sql時的值為order by 111 如果傳入的值是id,則解析成的sql為order by id 2.將傳入的資料直接顯示生成在sql中。如 order by u...
mybatis 中 和 區別
在使用mybatis 框架時 在xml的配置檔案中,通常是使用 來獲取數值的 如 select from t user inf where id 這時 如果你傳入的值為zhangsan 則會編譯成為 select from t user inf where id zhangsan mybatis 會...
Mybatis 中 和 區別
號與 區別 號表示引數,代表乙個字串。如 select a,b,c from table1 where id value 傳入引數後如 value 1 則可生成 select a,b,c from table1 where id 1 select a,b,c from table1 where ci...