Mybatis(五)#{}与${}的区别与使用

在映射文件中,有两种方式可在SQL语句中引入参数的值:

  1. #{}
<select id="selectMultiParam" resultType="cn.krain.domain.Student">
    select * from student where name=#{myname} or age=#{myage}
</select>
<!-- 经过Mybatis处理过后的SQL语句为: select * from student where name=? or age=? -->
  1. ${}
<select id="selectMultiParam" resultType="cn.krain.domain.Student">
    select * from student where name=${myname} or age=${myage}
</select>
<!-- 经过Mybatis处理过后的SQL语句为: select * from student where name='康康' or age=23 -->

#{}与${}的区别:

  1. #{}使用"?"作为占位符,使用preparedStatement对象进行预编译,效率高
  2. #{}能够避免SQL注入,更安全
  3. ${}是将参数与SQL拼接起来,使用Statement对象,效率低
  4. 存在SQL注入风险,缺乏安全性
  5. ${}可以用来替换表名或列名
全部评论

相关推荐

陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务