mybatis中#和$的區別


1 #是將傳入的值當做字符串的形式,eg:select id,name,age from student where id =#{id},當前端把id值1,傳入到后台的時候,就相當於 select id,name,age from student where id ='1'.

 2 $是將傳入的數據直接顯示生成sql語句,eg:select id,name,age from student where id =${id},當前端把id值1,傳入到后台的時候,就相當於 select id,name,age from student where id = 1.

 3 使用#可以很大程度上防止sql注入。(語句的拼接)

 4 但是如果使用在order by 中就需要使用 $.

 5 在大多數情況下還是經常使用#,但在不同情況下必須使用$. 

我覺得#與的區別最大在於:#{} 傳入值時,sql解析時,參數是帶引號的,而的區別最大在於:#{} 傳入值時,sql解析時,參數是帶引號的,而{}穿入值,sql解析時,參數是不帶引號的。

一 : 理解mybatis中 $與#

    在mybatis中的$與#都是在sql中動態的傳入參數。

    eg:select id,name,age from student where name=#{name}  這個name是動態的,可變的。當你傳入什么樣的值,就會根據你傳入的值執行sql語句。

二:使用$與#

   #{}: 解析為一個 JDBC 預編譯語句(prepared statement)的參數標記符,一個 #{ } 被解析為一個參數占位符 。

   ${}: 僅僅為一個純碎的 string 替換,在動態 SQL 解析階段將會進行變量替換。

  name-->dcy

 eg:  select id,name,age from student where name=#{name}   -- name='dcy'

       select id,name,age from student where name=${name}    -- name=dcy

 

原文:https://blog.csdn.net/qq_35773649/article/details/89884757

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM