深刻理解MyBatis中 和 的區別

2021-09-29 05:24:39 字數 1318 閱讀 3598

首先看一下下面這兩個sql語句的區別:

select id, username, password, role

from user

where username = #

and password = #

select id, username, password, role

from user

where username = $

and password = $

mybatis中的#和$的區別:

1、#{}將傳入的資料都當成乙個字串,會對自動傳入的資料加乙個雙引號。

如:where username=#,如果傳入的值是111,那麼解析成sql時的值為where username="111", 如果傳入的值是id,則解析成的sql為where username="id". 

2、${}將傳入的資料直接顯示生成在sql中。

如:where username=$,如果傳入的值是111,那麼解析成sql時的值為where username=111;

3、針對上面的sql,如果傳入的值是;drop table user;,

那麼第一條用#{}的sql解析為:select id, username, password, role from user where username=";drop table user;"

那麼第二條用${}的sql解析為:select id, username, password, role from user where username=;drop table user;

注意:使用#{}預編譯時把值用 '' 引號包裹起來就變成了乙個字串的值,但是使用${}時,它是沒有引號包裹,就是直接填充的,當輸入 ;drop table user;時。實際是兩條sql語句了,這時候已經sql注入了。

3、#方式能夠很大程度防止sql注入,$方式無法防止sql注入。

4、$方式一般用於傳入資料庫物件,例如傳入表名和列名,還有排序時使用order by動態引數時需要使用$ ,order by $

5、一般能用#的就別用$,若不得不使用「$」這樣的引數,要手工地做好過濾工作,來防止sql注入攻擊。

6、在mybatis中,「$」這樣格式的引數會直接參與sql編譯,從而不能避免注入攻擊。但涉及到動態表名和列名時,只能使用「$」這樣的引數格式。所以,這樣的引數需要我們在**中手工進行處理來防止注入。

【總結】在編寫mybatis的對映語句時,盡量採用「#」這樣的格式。若不得不使用「$」這樣的引數,要手工地做好過濾工作,來防止sql注入攻擊。

深刻理解IdentityHashMap

新建pojo package test public class cat public string getname public void setname string name public integer getage public void setage integer age public...

深刻理解IdentityHashMap

新建pojo package test public class cat public string getname public void setname string name public integer getage public void setage integer age public...

JS深刻理解補充

對於函式的理解,首先看乙個函式定義 function functiondefined 顯而易見,functiondefined 為函式名字,在js中為指向這個函式體的指標,代表這個函式的指標的變數,並且和原始資料型別一樣儲存在棧中。而functiondefined函式體則儲存在堆中。每當new出乙個...