零、引言

使用 #{name} 的时候,MyBatis会进行预编译,防止SQL注入的问题(官方话)
用一个通俗一点的例子来解释,比如有如下MyBatis的SQL语句

一、最正确的用法

<select id="find">
... where name = #{name} order by ${columnName}
</select>
xxxxxxxxxx
3
 
1
<select id="find">
2
    ... where name = #{name} order by ${columnName}
3
</select>
说明:如果name的类型为String值为LCF,columnName的类型为String值为 id
上述SQL翻译结果是:
... where name = 'LCF' order by id
xxxxxxxxxx
1
 
1
... where name = 'LCF' order by id
1. #{name} 会根据传入数据的类型进行预编译,所以在生成SQL的时候自动加上了单引号。
2.${columnName} 会直接将结果替换进来。

二、反例说明

<select id="find">
... where name = ${name} order by #{columnName}
</select>
xxxxxxxxxx
3
 
1
<select id="find">
2
    ... where name = ${name} order by #{columnName}
3
</select>
说明:基本类型和值如上所述,该SQL翻译出来的结果是什么?
... where name = LCF order by 'id'
xxxxxxxxxx
1
 
1
... where name = LCF order by 'id'
仔细看LCF是没有单引号引起来的,反倒是 id 被单引号括起来了。

三、结论

#会进行预编译,防止SQL注入。
$会被直接进行字符替换,容易造成SQL注入。

#####################LCF###############2017-06-21#####################

最新文章

  1. java关键字之final
  2. NGUI Atlas Maker sprites with black line issue
  3. squid源码安装下的conf文件默认值和提示
  4. 全球酷站秀:15个顶尖的 CSS3 网站作品
  5. hdu 5000 dp **
  6. hdu 5749 Colmerauer
  7. 海外支付:抵御信用卡欺诈的CyberSource
  8. EF——一个实体对应两张表,两个实体对应一张表 06 (转)
  9. 吐槽:Lambda表达式
  10. Andrdoid中相应用程序的行为拦截实现方式之----从Java层进行拦截
  11. GoLang获取struct的tag
  12. spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析
  13. 【转】STL中vector、list、deque和map的区别
  14. 【其他】【PL/SQL Developer】【1】解决PL/SQL Developer过期的情况
  15. pytest.2.运行多个文件
  16. 24.类的加载机制和反射.md
  17. C# p2p UDP穿越NAT,UDP打洞源码
  18. python补充2
  19. java 实现七大基本排序算法
  20. django文章收藏

热门文章

  1. 腾讯云提示invalid pos, pos is bigger than filesize! pos: 0, file_size: 0错误
  2. js实现文本框文本域光标处插入图片文本的插件(并且光标在插入内容的内容后显示)
  3. C盘空间不够,清除VS下的 Font Cache
  4. JAVA-JSP内置对象
  5. 带网上开户表单jQuery焦点图
  6. Mybatis generator 自动生成代码(2)
  7. SOAPUI 測试Http 协义
  8. Parquet
  9. [Linux基础环境/软件]Linux下安装mysql
  10. 【WPF】右键菜单ContextMenu可点击区域太小的问题