<select id="selectUserById" parameterType="int" resultType="User">
    select * from user where id=#{id};
</select>

#{}和${}都可以作为参数传递。

区别:

  • #{}的处理相当于预处理,作为一个占位符,传进的参数自动加单引号处理。而这种方式可以防止sql注入。
  • ${}的处理相当于字符串拼接。传入的参数中根据需要自己添加单引号,但是#{}无法处理order by 情况,而${}可以。

一般能使用#{}就别用${},除非特殊情况,如order by ${columnName}。

 

 posted on 2019-07-31 11:21  会飞的金鱼  阅读(90)  评论(0)    收藏  举报