当前位置:网站首页>#{}和${}的区别
#{}和${}的区别
2022-07-28 22:23:00 【m0_54861649】
在使用mybatis的时候我们会使用到#{}和${}这两个符号来为sql语句传参数
那么这两者有什么区别呢?
1.#{}是预编译处理,是占位符,${}是字符串替换,是拼接符
2.Mybatis在处理#{}的时候会将sql中的#{}替换成?号,调用PreparedStatement来赋值
如:select * from user where name = #{userName};设userName=yuze
看日志我们可以看到解析时将#{userName}替换成了 ?
select * from user where name = ;
然后再把yuze放进去,外面加上单引号
3.Mybatis在处理 的时候就是把 {}的时候就是把 的时候就是把{}替换成变量的值,调用Statement来赋值
如:select * from user where name = #{userName};设userName=yuze
看日志可以发现就是直接把值拼接上去了
select * from user where name = yuze;
这极有可能发生sql注入,下面举了一个简单的sql注入案例
4.#{}的变量替换是在DBMS中、变量替换后,#{}对应的变量自动加上单引号
5. 的变量替换是在 D B M S 外、变量替换后, {}的变量替换是在DBMS外、变量替换后, 的变量替换是在DBMS外、变量替换后,{}对应的变量不会加上单引号
6.使用#{}可以有效的防止sql注入,提高系统的安全性
下面举一个简单的sql注入问题:
这是一条用户的账号、密码数据

当用户登录,我们验证账号密码是否正确时用这个sql:
username=yyy;password=123
select * from user where username=${username} and password=${password}
显然这条sql没问题可以查出来,但是如果有人不知道密码但是想登录账号怎么办
我们不需要填写正确的密码:
密码输入1 or 1=1,sql执行的其实是
select * from user where username='yyy' and password=1 or 1 =1
注意:这里的yyy外面的单引号不是 符号提供的。 {}符号提供的。 符号提供的。{}没有这个功能,可以是sql手动拼接的,这里前后逻辑可能并不严密,但是sql入去最简单的例子就是这样。
边栏推荐
- Connection pool - return connection details (Part 2)
- [TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
- Web系统常见安全漏洞介绍及解决方案-CSRF攻击
- Centos7 install mysql8
- Websocket heartbeat mechanism (keep alive mechanism)
- curl (7) Failed connect to localhost8080; Connection refused
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- Leetcode64. Minimum path sum
- VMware VCSA 7.0 Install
- 【C】逆序字符串(俩种递归思路)
猜你喜欢

centos7安装mysql8

Leetcode60. 排列序列

Leetcode64. 最小路径和

【C】atoi和offsetof的介绍和模拟实现

CANoe应用案例之DoIP通信

Classification and determination method of Worthington stemxyme

SAP oracle 复制新实例后数据库远程连接报错 ora-01031

With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally

mysql索引失效的常见9种原因详解

Interpretation of ISO 13400 (doip) standard
随机推荐
Leetcode61. 旋转链表
器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
Real time data warehouse: Didi's real-time data warehouse landing practice
Application of Devops in Internet of things solutions
Interpretation of ISO 13400 (doip) standard
JS advanced ES6 ~ es13 new features
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
Use pytoch to quickly train the network model
Add build dependency error
Briefly introduce the working principle and characteristics of block cipher encryption block link mode (cryptography shift cipher encryption and decryption)
Powercl batch creates and manages virtual switches
【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
【微服务】Nacos集群搭建以及加载文件配置
Websocket heartbeat mechanism (keep alive mechanism)
双重for循环优化
Compose 的声明式代码如此简洁?
JS高级 之 ES6~ES13 新特性
Data warehouse: Doris' application practice in meituan
PowerCLi VMware vCenter 通过自建的PXE Server一键批量部署常规New-VM
基于 FPGA 实现数字时钟详细原理讲解及验证结果