码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-11 19:22:31      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:mybatis传参

使用#传入参数是,sql语句解析是会加上‘‘,比如  

                        select * from table where name = #{name} ,

传入的name为小李,那么最后打印出来的就是

                            select * from table where name = ‘小李’,

就是会当成字符串来解析,这样相比于$的好处是比较明显对的吧,

#{}传参能防止sql注入,如果在mapper文件中中,你用

                         select * from table where name = ${name}

那么解析出来的sql就是

                         select * from table where name = 小李,

这种语句在某些数据库是要报错的。

如果你传入的参数为 单引号‘那么如果使用${},这种方式 那么是会报错的,

相反,另一种场景,如果你要做动态的排序,比如  order by   column,这个时候务必要用${column},因为如果你使用了#{column},那么打印出来的将会是

                                   select * from table order by  ‘name‘  

这样的结果同样错误!!!!


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

标签:mybatis传参

原文地址:http://4443915.blog.51cto.com/4433915/1825457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!