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. ${}可以用来替换表名或列名
全部评论

相关推荐

01-29 15:45
已编辑
华中科技大学 前端工程师
COLORSN:可以试一下,小厂看技术栈是不是很落后,如果太拉胯就别去,个人认为有实习氛围比你自己琢磨要高效不少,然后就是小厂其实也有可能会问的很难,这都比较难说,还是看自己项目含金量够不够,寒假还能不能推进学习再选择,毕竟去实习过年就10天假了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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