首页 > 其他 > 详细

mybatis #号与$号的区别

时间:2019-09-19 18:11:14      阅读:246      评论:0      收藏:0      [点我收藏+]

区别:

 在sql中当传入的参数是字符型,则用#号会带上单引号,不会引起sql注入;

 在sql中当传入的参数是字符型,则用$号不会带上单引号,会引起sql注入;

使用范围:

  当传入的参数用于查询条件,尽量用#号,特殊情况可酌情使用#号或$号;

  例如:select id,name from user where name = #{userName},mybatis解析后的sql语句:select id,name from user where name = ‘张三‘

  当传入的参数用于字段或表名,则必须使用$号,否则会报错(字段不存在或无效表名);

  例如:select id,${filedName} from ${userTable} ,mybatis解析后的sql语句:select id,name from user 

    如果改为:select id,#{filedName} from #{userTable} ,mybatis解析后的sql语句:select id,‘name‘ from ‘user‘ ,报错

mybatis #号与$号的区别

原文:https://www.cnblogs.com/tianchao/p/11551422.html

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