mybatis 中生成的字段不带引号 #{}和${}

#{},和 ${}传参的区别

1) 使用#{参数}传入会加上单引号,sql语句解析是会加上""

比如  select * from table where name = #{name} ,传入的name为小李,那么最后打印出来的就是

 select * from table where name = ‘小李’,就是会当成字符串来解析

#{}传参能防止sql注入,如果你传入的参数为 单引号',那么如果使用${},这种方式 那么是会报错的

2)${}

另外一种场景是,如果你要做动态的排序,比如  order by   column,这个时候务必要用${},

因为如果你使用了#{},那么打印出来的将会是  select * from table order by  'name'  ,这样是没用,

猜你喜欢

转载自blog.csdn.net/Jacob_Zheng/article/details/81101602