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

    在sql配置中比如in(#rewr#) 与in ($rewr$)
    
    
    在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型匹配,而$不进行数据类型匹配,例如: 
    
    select * from table where id = #id# ,其中如果字段id为字符型,那么#id#表示的就是'id'类型,如果id为整型,那么#id#就是id类型。 
    
    select * from table where id = $id$ ,如果字段id为整型,Sql语句就不会出错,但是如果字段id为字符型,那么Sql语句应该写成 select * from table where id = '$id$'
    
    
    $ 的作用实际上是字符串拼接, 
    select * from $tableName$ 
    等效于 
    StringBuffer sb = new StringBuffer(256); 
    sb.append("select * from ").append(tableName); 
    sb.toString(); 

    #用于变量替换 
    select * from table where id = #id# 
    等效于 
    prepareStement = stmt.createPrepareStement("select * from table where id = ?") 
    prepareStement.setString(1,'abc'); 

    ------------------------------------------------ 

    说道这里, 总结一下, 什么时候用$,什么时候 用 # 

    对于变量部分, 应当使用#, 这样可以有效的防止sql注入, 未来,# 都是用到了prepareStement,这样对效率也有一定的提升 

    $只是简单的字符拼接而已,对于非变量部分, 那只能使用$, 实际上, 在很多场合,$也是有很多实际意义的 
    例如 
    select * from $tableName$ 对于不同的表执行统一的查询 
    update $tableName$ set status = #status# 每个实体一张表,改变不用实体的状态 
    特别提醒一下, $只是字符串拼接, 所以要特别小心sql注入问题

    转载https://blog.csdn.net/kiss_vicente/article/details/7602900
  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/fengduandeai/p/8628369.html
Copyright © 2011-2022 走看看