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注入。后台

相关文章
相关标签/搜索