zoukankan      html  css  js  c++  java
  • mybatis中#和$的区别

    mybatis中写sql时有两种方法传参#{param1}和${param1}

    #是以占位符的形式将参数作为字符串进行传参,如:select * from stu where name = #{name},这条sql编译后变成select * from stu where name =?

    解析之后会将参数name的值用单引号括住来替换?号:select * from stu where name = ‘value’

    而$的传参则是直接拼接sql,如:select * from stu where name = ${name},解析后变成select * from stu where name = value。由于value是字符串,而没有使用单引号或双引号括住,则此时会报错

    另外,使用#只能将参数作为一个value,而使用$还可以将参数当作sql语法的一部分,比如参数为 “ ‘zhl' and age = 12",如上sql,$和#两者解析结果如下:

    #传参 :  select * from stu where name = “ ‘zhl' and age = 12"; 此时表示name等于“ ‘zhl' and age = 12"

    $传参 :  select * from stu where name =  ‘zhl' and age = 12; 此时表示name等于'zhl',年龄等于12

    因此:使用#传参可以避免sql注入。使用$传参更适合固定的sql,而#传参更适合动态sql

  • 相关阅读:
    Delphi程序结构
    SQL存储过程解密 Encrypted object is not transferable, and script can not be generated

    在河南呢
    还在河南,写随笔吧
    HAVING
    mIRC
    关于CAP理论
    开篇
    移动信息系统
  • 原文地址:https://www.cnblogs.com/zhlblogs/p/9634858.html
Copyright © 2011-2022 走看看