Mybatis中,${}与#{}参数获取的区别
${}和#{}对比:
共同点:都可以获取map中的值或者pojo对象的属性值
区别:
#{}:是以预编译的形式,将参数设置到sql语句中;PreparedStatement;防止sql注入
${}:取出的值直接拼装在sql语句中;会有安全问题;
例:
select * from tbl_employee where id=${id} and last_name=#{last_name} ##Preparing:select * from tbl_employee where id=2 and last_name=?
大多数情况下我们取参数的值都应该去使用#{};
原生jdbc不支持占位符的地方我们就可以使用${}进行取值
例(分表、排序...):
select * from ${year}_salary where xxx select * from tbl_employee order by ${f_name}
#{}更丰富的用法:
规定参数的一些规则:
javaType、jdbcType、mode(存储过程)、numericScale、resultMap、typeHandler、jdbcTypeName、expression(未来准备支持的功能)
jdbcType通常需要在某种特定的条件下被设置:
在我们数据为null的时候,有些数据库可能不能识别mybatis对null的默认处理。比如Oracle(报错);
本文来自博客园,作者:金玉良猿,转载请注明原文链接:https://www.cnblogs.com/LLFA/p/14349115.html

浙公网安备 33010602011771号