mybatis中的${}和#{}的区别

发布时间 2023-03-28 11:50:26作者: just1t

使用的sql实例:select * from student where id = , 传入的id值为 1
如果使用的是${},select * from student where id = ${id},在解析sql语句之后,得到的真正执行的sql为:select * from student where id = 1
 
 
如果我们使用的是#{},select * from student where id =#{id},在解析sql之后,真正指定的sql语句为:select * from student where id = '1'
 
可以看出他们两个的区别:
   如果我们使用的是#{},在解析之后就会将其添加上'',表示这是一个字符串,当时如果我们使用的是${},在解析之后,就不会添加'';
也就是说如果我们使用${}进行拼接sql的时候,存在sql注入问题(自我立即:将sql语句中的关键字当作参数进行使用),容易导致数据库不安全。