<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("select * from ").append(tableName); <br />sb.toString(); <br /><br />#用於變量替換 <br />select * from table where id = #id# <br />等效於 <br />prepareStement = stmt.createPrepareStement("select * from table where id = ?") <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> </p>數據庫
<p> </p>app
<p> </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">、#能夠進行預編譯,進行類型匹配,#變量名#  會轉化爲 jdbc 的 類型 <br />    $不進行數據類型匹配,$變量名$就直接把 $name$替換爲 name的內容對象
<br /></font>    例如:
<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>