首页 > 移动平台 > 详细

[MyBatis]Mapper中 $ 与 # 的区别

时间:2021-02-28 00:18:49      阅读:24      评论:0      收藏:0      [点我收藏+]
  1. #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id =‘1‘.

  2. $是将传入的数据直接显示生成sql语句,eg:select id,name,age from student where id =${id},当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id = 1.

  3. 使用#可以很大程度防止sql注入。(语句的拼接)

  4. 但是如果使用在order by 中就需要使用 $.

  5. 在大多数情况下还是经常使用#,但在不同情况下必须使用$.

我觉得#{}传入值,sql解析时,参数是不带引号的。

  1. 一:理解mybatis$#
在mybatis中的$与#都是在sql中动态的传入参数。

eg:select id,name,age from student where name=#{name}  这个name是动态的,可变的。当你传入什么样的值,就会根据你传入的值执行sql语句。
  1. 二:使用$#
  • #{}: 解析为一个 JDBC 预编译语句(prepared statement)的参数标记符,一个 #{ } 被解析为一个参数占位符
  • ${}: 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换

eg: name-->cy

-- name=‘cy‘
select id,name,age from student where name=#{name}
-- name=cy
select id,name,age from student where name=${name}

————————————————
原文链接:https://blog.csdn.net/hao65103940/article/details/79099159

[MyBatis]Mapper中 $ 与 # 的区别

原文:https://www.cnblogs.com/melodyjerry/p/14457208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!