Mybatis中的 ${} 和 #{}区别

Mybatis 中核心配置文件parameterType向SQL中传参有两种方式——#{}和${}

比如要查找一个学生姓名

select * from student where username = #{name} 

预编译后,#{}会被编译为:

select * from student where username = ?      #可以有效的防止sql注入问题

而使用${}在解析时,会直接传入字符串:

select * from student where username = 'zhangsan'

#{}和${}的区别

  • #{}能够防止sql注入,${}无法防止Sql注入
  • ${}用于传入数据库对象,例如传入表名
  • 能用#{}的就别用 ${}
发布了34 篇原创文章 · 获赞 55 · 访问量 2555

猜你喜欢

转载自blog.csdn.net/qq_41256881/article/details/105376034
今日推荐