Mybatis中#和$区别(带脑图)
2024-10-14 15:21:57
零、引言
使用 #{name} 的时候,MyBatis会进行预编译,防止SQL注入的问题(官方话)
用一个通俗一点的例子来解释,比如有如下MyBatis的SQL语句
一、最正确的用法
<select id="find">
... where name = #{name} order by ${columnName}
</select>
... 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>
... 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#####################
最新文章
- java关键字之final
- NGUI Atlas Maker sprites with black line issue
- squid源码安装下的conf文件默认值和提示
- 全球酷站秀:15个顶尖的 CSS3 网站作品
- hdu 5000 dp **
- hdu 5749 Colmerauer
- 海外支付:抵御信用卡欺诈的CyberSource
- EF——一个实体对应两张表,两个实体对应一张表 06 (转)
- 吐槽:Lambda表达式
- Andrdoid中相应用程序的行为拦截实现方式之----从Java层进行拦截
- GoLang获取struct的tag
- spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析
- 【转】STL中vector、list、deque和map的区别
- 【其他】【PL/SQL Developer】【1】解决PL/SQL Developer过期的情况
- pytest.2.运行多个文件
- 24.类的加载机制和反射.md
- C# p2p UDP穿越NAT,UDP打洞源码
- python补充2
- java 实现七大基本排序算法
- django文章收藏
热门文章
- 腾讯云提示invalid pos, pos is bigger than filesize! pos: 0, file_size: 0错误
- js实现文本框文本域光标处插入图片文本的插件(并且光标在插入内容的内容后显示)
- C盘空间不够,清除VS下的 Font Cache
- JAVA-JSP内置对象
- 带网上开户表单jQuery焦点图
- Mybatis generator 自动生成代码(2)
- SOAPUI 測试Http 协义
- Parquet
- [Linux基础环境/软件]Linux下安装mysql
- 【WPF】右键菜单ContextMenu可点击区域太小的问题