Mybatis 中$與#的區別,預防SQL注入

一直沒注意Mybatis 中$與#的區別,固然也是更習慣使用#,沒想到避免了SQL注入,可是因爲要處理項目中安全滲透的問題,不可避免的又遇到了這個問題,特此記錄一下。前端

首先是共同點:sql

 在mybatis中的$與#都是在sql中動態的傳入參數。安全

select id,name,age from user where name=#{name}

這個name是動態的,可變的,當傳入什麼樣的值,就會根據你傳入的值執行sql語句。mybatis

其次是二者的區別:code

1 #是將傳入的值當作字符串的形式字符串

select id,name,age from user where id =#{id},
//當前端把id值1,傳入到後臺的時候,就至關於
select id,name,age from user where id ='1'.

 2 $是將傳入的數據直接顯示生成sql語句class

select id,name,age from user where id =#{id},
//當前端把id值1,傳入到後臺的時候,就至關於
select id,name,age from user where id =1.

$是將傳入的數據直接顯示生成sql語句,這樣就容易形成SQL注入。後臺

相關文章
相關標籤/搜索