這篇文章主要講解了“mybatis占位符#{}和${}的用法和區(qū)別”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“mybatis占位符#{}和${}的用法和區(qū)別”吧!
創(chuàng)新互聯(lián)公司主營(yíng)城區(qū)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開(kāi)發(fā)公司,城區(qū)h5重慶小程序開(kāi)發(fā)搭建,城區(qū)網(wǎng)站營(yíng)銷(xiāo)推廣歡迎城區(qū)等地區(qū)企業(yè)咨詢(xún)
基本類(lèi)型:
基本類(lèi)型,參數(shù)名稱(chēng)與占位符中的名稱(chēng)無(wú)關(guān)。
#{} 傳入值時(shí),sql解析時(shí),參數(shù)是帶引號(hào)的
如果用了@Param("xxx") ,則mybatis會(huì)自動(dòng)生成map作為入?yún)ⅲ敲磪?shù)名稱(chēng)則必須與占位符一致
<select id="findById" parameterType="int" resultType="cn.wh.vo.Role"> select * from t_role where id = #{xxxid} </select>
測(cè)試:
@Test public void testSelectOne(){ Role role = (Role)session.selectOne("cn.wh.mapper.RoleMapper.findById",1); System.out.println(role.getName()); }
自定義類(lèi)型
自定義類(lèi)型作為參數(shù),自定義類(lèi)中需要為為屬性提供get方法,如果沒(méi)有提供get方法,那么會(huì)根據(jù)占位符中的名稱(chēng)去反射獲取值,如果占位符中的名稱(chēng)和屬性不一致,那么報(bào)ReflectionException。
<select id="findListBypage" parameterType="cn.wh.util.PageUtil" resultType="Role"> select * from t_role limit #{index},#{size} </select>
測(cè)試:
@Test public void testPage1(){ PageUtil pu = new PageUtil(); pu.setIndex(3); pu.setSize(3); List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.findListBypage", pu); for(Role r:list){ System.out.println(r.getName()); } }
Map
Map作為參數(shù)類(lèi)型,key和占位符中的名稱(chēng)一致即可,如果名稱(chēng)不一致那么將會(huì)把null,傳遞到占位符中。
注意:#{}占位符不能解決一下 3 類(lèi)問(wèn)題:
表名是動(dòng)態(tài)的: Select * from #{table_name}
列名是動(dòng)態(tài)的:Select #{column_name} from t_role
排序列是動(dòng)態(tài)的: Select * from t_role order by #{columu}
${}傳入值,sql解析時(shí),參數(shù)是不帶引號(hào)的。
因此${}參數(shù)不能為基本數(shù)據(jù)類(lèi)型,只能為自定義類(lèi)型和map
<!-- 查詢(xún)所有 --> <select id="findAll" parameterType="map" resultType="cn.wh.vo.Role"> select * from ${tableName} </select>
測(cè)試:
@Test public void testSelectList(){ Map<String,String> map = new HashMap<String,String>(); map.put("tableName", "t_role"); List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.findAll",map); for(Role role:list){ System.out.println(role.getId()+"----"+role.getName()); } }
作為連接符使用:
<select id="selectLike1" parameterType="map" resultType="Role"> select *from t_role where name like '${name}%'; </select>
測(cè)試:
@Test public void testLike2(){ Map<String,String> map = new HashMap<String,String>(); map.put("name", "黃"); List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.selectLike1",map); for(Role r:list){ System.out.println(r.getName()); } }
在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)你傳入什么樣的值,就會(huì)根據(jù)你傳入的值執(zhí)行sql語(yǔ)句。
#{}: 解析為一個(gè) JDBC 預(yù)編譯語(yǔ)句(prepared statement)的參數(shù)標(biāo)記符,一個(gè) #{ } 被解析為一個(gè)參數(shù)占位符 。
${}: 僅僅為一個(gè)純碎的 string 替換,在動(dòng)態(tài) SQL 解析階段將會(huì)進(jìn)行變量替換。
例子:
eg 1: select id,name,age from student where name=#{name} ;
解析為:select id,name,age from student where name='cy';
eg 2: select id,name,age from student where name=${name};
解析為:select id,name,age from student where name=cy;
#是將傳入的值當(dāng)做字符串的形式,eg:select id,name,age from student where id =#{id},當(dāng)前端把id值1,傳入到后臺(tái)的時(shí)候,就相當(dāng)于 select id,name,age from student where id ='1'.
$是將傳入的數(shù)據(jù)直接顯示生成sql語(yǔ)句,eg:select id,name,age from student where id =${id},當(dāng)前端把id值1,傳入到后臺(tái)的時(shí)候,就相當(dāng)于 select id,name,age from student where id = 1.
使用#可以很大程度上防止sql注入。(語(yǔ)句的拼接)
但是如果使用在order by 中就需要使用 $.
在大多數(shù)情況下還是經(jīng)常使用#,但在不同情況下必須使用$.
我覺(jué)得#與的區(qū)別最大在于:#{} 傳入值時(shí),sql解析時(shí),參數(shù)是帶引號(hào)的,而的區(qū)別最大在于:#{} 傳入值時(shí),sql解析時(shí),參數(shù)是帶引號(hào)的,而{}穿入值,sql解析時(shí),參數(shù)是不帶引號(hào)的。
感謝各位的閱讀,以上就是“mybatis占位符#{}和${}的用法和區(qū)別”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)mybatis占位符#{}和${}的用法和區(qū)別這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站名稱(chēng):mybatis占位符#{}和${}的用法和區(qū)別
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article42/gieiec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航、品牌網(wǎng)站制作、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)