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

    • 介绍

      在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$'。

    • 区别  

      的作用实际上是字符串拼接: 

    1 select * from $tableName$ 

      等效于:

    1 StringBuffer sb = new StringBuffer(256); 
    2 sb.append("select * from ").append(tableName); 
    3 sb.toString(); 

      #用于变量替换 

    1 select * from table where id = #id# 

      等效于:

    1 prepareStement = stmt.createPrepareStement("select * from table where id = ?") ;
    2 prepareStement.setString(1,'abc'); 
    • 用法总结

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

      2、$只是简单的字符拼接而已,对于非变量部分, 那只能使用$, 实际上, 在很多场合,$也是有很多实际意义的 。

      例如 :select * from $tableName$ 对于不同的表执行统一的查询。 $只是字符串拼接, 所以要特别小心sql注入问题。

      3、能同时使用#和$的时候最好用#。

     

  • 相关阅读:
    Spring IOC
    MyBatis环境搭建
    Spring AOP
    DWR在Spring中应用
    利用反射自动封装成实体对象
    Spring源码下载
    You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
    由于未能创建 Microsoft Visual C# 2010 编译器,因此未能打开项目 "xxx" ”的解决方法
    正则表达式
    安卓模拟器里面的cpu/abi里面有AMD、intel x86、mlps应该选择哪个
  • 原文地址:https://www.cnblogs.com/lcngu/p/5334993.html
Copyright © 2011-2022 走看看