Mybatis 配置文件xml中$与#的区别

Mybatis 中$与#的区别
 #是将传入的值当做字符串的形式,
  select id,name,age from student where id =#{id},
  当前端把id值1,传入到后台的时候,
  就相当于 select id,name,age from student where id ='1'.

  $是将传入的数据直接显示生成sql语句,
  select id,name,age from student where id =${id},
  当前端把id值1,传入到后台的时候,
  就相当于 select id,name,age from student where id = 1.
  
  #{}: 解析为一个 JDBC 预编译语句(prepared statement)的参数标记符,一个 #{ } 被解析为一个参数占位符 。

  ${}: 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换
  使用#可以很大程度上防止sql注入。(语句的拼接),但是如果使用在order by 中就需要使用 $.
  在大多数情况下还是经常使用#,但在不同情况下必须使用$.

猜你喜欢

转载自blog.csdn.net/weixin_41648325/article/details/81126566