首先都是把参数进行sql语句的拼接
#{param}
会对自动传入的数据加一个双引号,传入的数据都当成一个字符串(这也正是它可以防止sql注入的原因)【先编译SQL语句,再传参数】
name="desc"
select * from tb_user where name = #{name}
select * from tb_user where name = "desc"
${param}
传递的参数会被当成sql语句中的一部分,不防止sql注入 【先拼接上参数,再编译SQL】
name="desc"
select * from tb_user where name = ${name}
select * from tb_user where name = desc
desc是mysql的关键字,所以这句sql肯定错
注入:
name = “1 or 1 = 1”
select * from tb_user where name = ${name}
select * from tb_user where name = 1 or 1 = 1
# 这句会把所有数据都查出来,因为1=1 是恒等式
select * from tb_user where name = #{name}
select * from tb_user where name = "1 or 1 = 1"
# 这句会把名字叫 “1 or 1=1”的查出来,这个名字只是个字符串
注意
排序 order by 后面只能用$进行传值,
因为用#会把传入的asc,desc和排序字段 都只当做字符串操作,不再是关键字
本文详细解析了在MyBatis中使用#和$符号进行参数传递的区别,强调了#符号对于防止SQL注入攻击的重要性,并举例说明了不当使用$符号可能导致的安全隐患。

被折叠的 条评论
为什么被折叠?



