Mybatis中#{} 和 ${} 的区别
1、#{} 是预编译处理、是占位符,{} 是字符串替换、是拼接符
2、Mybatis在处理#{},会将sql中的#{}替换为?号,调用PreparedStatement来赋值
3、Mybatis在处理${}时,就是把${}替换成变量的值,调用Statement来赋值
4、使用#{}可以有效的防止SQL注入
,提高系统安全性
案例
-- 假设
name = "zhangsan"
password = '1 or 1=1'
select * from user where name = #{name} and password = #{password} 转化为
select * from user where name = "zhangsan" and password = "1 or 1=1"
select * from user where name = ${name} and password = ${password} 转化为
select * from user where name = zhangsan and password = 1 or 1=1
Spring中#{} 和 ${} 的区别
1、@Value("#{}") Spring 表达式语言(简称SpEL)
2、@Value("${}") 从配置文件读取值的用法
3、@Value("zhangsan") 可以这样直接对这个成员变量赋值
**SpEL 字面量**:
整数:#{8}
小数:#{8.8}
科学计数法:#{1e4}
String:可以使用单引号或者双引号作为字符串的定界符号。
[Boolean](https://so.csdn.net/so/search?q=Boolean&spm=1001.2101.3001.7020):#{true}
**SpEL引用bean , 属性和方法**:
**引用其他对象:#{car}
引用其他对象的属性:#{car.brand}
调用其它方法 , 还可以链式操作:#{car.toString()}
调用静态方法静态属性:#{T(java.lang.Math).PI}**
SpEL支持的运算符号:
算术运算符:+,-,*,/,%,^(加号还可以用作字符串连接)
比较运算符:< , > , == , >= , <= , lt , gt , eg , le , ge
逻辑运算符:and , or , not , |
if-else 运算符(类似三目运算符):?:(temary), ?:(Elvis)
正则表达式:#{admin.email matches ‘[a-zA-Z0-9._%±]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}’}
总之:在spring中${}能做的#{}都能做