加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2020-12-11 23:56:23 所属栏目:MySql教程 来源:网络整理
导读:mybatis本身的说明: By default ,using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer,faster and almost always pref

mybatis本身的说明:

By default,using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer,faster and almost always preferred,sometimes you just want to directly inject a string unmodified into the SQL Statement. For example,for ORDER BY,<span style="color: #000000;"> you might use something like this:

ORDER BY ${columnName}
Here MyBatis won't modify or escape the string.

NOTE It's not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields,or always perform your own escapes and checks.

从上文可以看出:

1. 使用#{}格式的语法在mybatis中使用Preparement语句来安全的设置值,执行sql类似下面的:

PreparedStatement ps =1,id);

此时MyBatis 不会修改或转义字符串。

这种方式类似于:

Statement st = ResultSet rs </span>= st.executeQuery(sql);</pre>

参考文献:

【1】http://mybatis.github.io/mybatis-3/sqlmap-xml.html

【2】https://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读