首页 > 技术文章 > #{ }和${ }参数值的获取的区别

Darius-Bennett 2017-09-05 20:37 原文

#{ }支持从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} :排序是不支持预编译的!

推荐阅读