[入坑系列] Mybatis 中$與#的區別

1.理解

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.sql

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

 4 可是若是使用在order by 中就須要使用 $.post

 5 在大多數狀況下仍是常用#,但在不一樣狀況下必須使用$. spa

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

2.理解mybatis中 $與#

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

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

3.使用$與#

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

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

  name-->cy

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

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

相關文章
相關標籤/搜索