1、#可以進(jìn)行預(yù)編譯,進(jìn)行類型匹配,#變量名#? 會轉(zhuǎn)化為 jdbc的?類型
?? $不進(jìn)行數(shù)據(jù)類型匹配,$變量名$就直接把$name$替換為 name的內(nèi)容
?? 例如:
????select * from tablename where id = #id#,假設(shè)id的值為12,其中如果數(shù)據(jù)庫字段id為字符型,那么#id#表示的就是'12',如果id為整型,那么#id#就是 12
??? 會轉(zhuǎn)化為jdbc的select * from tablename where id=?,把?參數(shù)設(shè)置為id的值
????select * from tablename where id = $id$,如果字段id為整型,Sql語句就不會出錯(cuò),但是如果字段id為字符型,
????那么Sql語句應(yīng)該寫成 select * from table where id = '$id$'
????
3、#方式能夠很大程度防止sql注入.
4、$方式無法方式sql注入.
5、$方式一般用于傳入數(shù)據(jù)庫對象.例如傳入表名.
6、所以ibatis用#比$好,一般能用#的就別用$.
另外,使用##可以指定參數(shù)對應(yīng)數(shù)據(jù)庫的類型
如:
select * from tablename where id =#id:number#?
在做in,like 操作時(shí)候要特別注意
mysql: select * from user where user_name like concat('%',#name#,'%')oracle: select * from user where user_name like '%'||#name#||'%'sql server: select * from user where user_name like '%'+#name#+'%'
posted on 2011-11-17 22:15
RoyPayne 閱讀(615)
評論(0) 編輯 收藏 所屬分類:
ibatis