ibatis 中 $与#的区别

<pre>在sql配置中好比in(#rewr#) 与in ($rewr$) <pre>在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$' $ 的做用其实是字符串拼接, <br />select * from $tableName$ <br />等效于 <br />StringBuffer sb = new StringBuffer(256); <br />sb.append(&quot;select * from &quot;).append(tableName); <br />sb.toString(); <br /><br />#用于变量替换 <br />select * from table where id = #id# <br />等效于 <br />prepareStement = stmt.createPrepareStement(&quot;select * from table where id = ?&quot;) <br />prepareStement.setString(1,'abc'); <br /><br />------------------------------------------------ <br /><br />说道这里, 总结一下, 何时用$,何时 用 # <br /><br />对于变量部分, 应当使用#, 这样能够有效的防止sql注入, 将来,# 都是用到了prepareStement,这样对效率也有必定的提高 <br /><br />$只是简单的字符拼接而已,对于非变量部分, 那只能使用$, 实际上, 在不少场合,$也是有不少实际意义的 <br />例如 <br />select * from $tableName$ 对于不一样的表执行统一的查询 <br />update $tableName$ set status = #status# 每一个实体一张表,改变不用实体的状态 <br />特别提醒一下, $只是字符串拼接, 因此要特别当心sql注入问题。</pre></pre>sql

<p>&#160;</p>数据库

<p>&#160;</p>app

<p>&#160;</p>.net

<p>原文出处 <a href="http://jsczxy2.iteye.com/blog/1218679">http://jsczxy2.iteye.com/blog/1218679</a></p>code

<p>1<font color="#ff0000">、#能够进行预编译,进行类型匹配,#变量名#&#160; 会转化为 jdbc 的 类型 <br />&#160;&#160;&#160; $不进行数据类型匹配,$变量名$就直接把 $name$替换为 name的内容对象

<br /></font>&#160;&#160;&#160; 例如:

<br />     select * from tablename where id = #id# ,假设id的值为12,其中若是数据库字段id为字符型,那么#id#表示的就是'12',若是id为整型,那么#id#就是 12blog

<br />    会转化为jdbc的 select * from tablename where id=?,把?参数设置为id的值字符串

<br />     select * from tablename where id = $id$ ,若是字段id为整型,Sql语句就不会出错,可是若是字段id为字符型,it

<br />     那么Sql语句应该写成 select * from table where id = '$id$'编译

<br />三、#方式可以很大程度防止sql注入.

<br />四、$方式没法方式sql注入.

<br />五、$方式通常用于传入数据库对象.例如传入表名.

<br />六、因此ibatis用#比$好,通常能用#的就别用$.

<br />另外,使用##能够指定参数对应数据库的类型

<br />如:

<br />select * from tablename where id = #id:number#  <br />在作in,like 操做时候要特别注意

<br />总结如下:

<br />$号使用在具体pojo类也就是非基本类型的取值,而#号使用在具体有基本类型的取值</p>

<p><a href="http://static.oschina.net/uploads/img/201308/25001651_lFI6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201308/25001652_pNAW.png" width="244" height="148" /></a></p>

相关文章
相关标签/搜索