MyBatis探究-----接口类映射XML文件中符号$和#的区别

   1. $和#的区别

  • #{}和${}都可以获取map中的值或者pojo对象属性的值
  • #{}:是以预编译的形式,将参数设置到sql语句中,防止sql注入
  • ${}:取出的值直接拼装在sql语句中;会有安全问题

   2. $和#的用法

2.1 表名、排序作为变量时,必须使用 ${ }

select * from ${year}_salary where xxx;

select * from tbl_employee order by ${f_name} ${order}

2.2 能使用 #{ } 的地方就用 #{ }

接口映射文件中SQL语句:select * from t_employee where empId=${empId} and empName=#{empName}
执行的预编译SQL语句:select * from t_employee where empId=2 and empName=?

猜你喜欢

转载自www.cnblogs.com/fengfuwanliu/p/10596965.html