#{ }和${ }参数值的获取的区别

#{ }支持从Map中取值;支持从POJO对象中取值

${ }支持从Map中取值;支持从POJO对象中取值

select * from tbl_employee where id = ${id} and last_name = #{lastName}
preparing:select * from tbl_employee where id = 2 and last_name = ?

区别:
#{}:是以预编译的形式,将参数设置到sql语句中,PreparedStatement;防止sql注入
${}:取出的值直接拼装在sql语句中,会有安全问题;
大多情况下,我们取参数的值都应该去使用#{};

原生JDBC不支持占位符的地方我们就可以使用${}进行取值
比如分表、排序;按照年份分表拆分
select * from ${year}_salary where xxx;[表名不支持预编译]
select * from tbl_employee order by ${f_name} ${order} :排序是不支持预编译的!

原文地址:https://www.cnblogs.com/Darius-Bennett/p/7481840.html