1 #是將傳入的值當(dāng)做字符串的形式,eg:select id,name,age from student where id =#{id},當(dāng)前端把id值1,傳入到后臺的時(shí)候,就相當(dāng)于?select id,name,age from student where id ='1'.
?2 $是將傳入的數(shù)據(jù)直接顯示生成sql語句,eg:select id,name,age from student where id =${id},當(dāng)前端把id值1,傳入到后臺的時(shí)候,就相當(dāng)于?select id,name,age from student where id =?1.
?3 使用#可以很大程度上防止sql注入。(語句的拼接)
?4 但是如果使用在order by 中就需要使用 $.
?5 在大多數(shù)情況下還是經(jīng)常使用#,但在不同情況下必須使用$.?
我覺得#與{}穿入值,sql解析時(shí),參數(shù)是不帶引號的。
一 : 理解mybatis中 $與#
? ? 在mybatis中的$與#都是在sql中動(dòng)態(tài)的傳入?yún)?shù)。
? ? eg:select id,name,age?from student where name=#{name} ?這個(gè)name是動(dòng)態(tài)的,可變的。當(dāng)你傳入什么樣的值,就會根據(jù)你傳入的值執(zhí)行sql語句。
二:使用$與#
? ?#{}: 解析為一個(gè) JDBC 預(yù)編譯語句(prepared statement)的參數(shù)標(biāo)記符,一個(gè) #{ } 被解析為一個(gè)參數(shù)占位符?。
? ?${}: 僅僅為一個(gè)純碎的 string 替換,在動(dòng)態(tài) SQL 解析階段將會進(jìn)行變量替換。
? 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
---------------------
作者:灬囖
來源:CSDN
原文:https://blog.csdn.net/hao65103940/article/details/79099159?utm_source=copy
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!