Mybatis動(dòng)態(tài)SQL標(biāo)簽的使用
動(dòng)態(tài)SQL是MyBatis的一個(gè)強(qiáng)大特性,可以運(yùn)用動(dòng)態(tài)SQL語(yǔ)句標(biāo)簽方便我們?cè)?/span>SQL中實(shí)現(xiàn)各種邏輯
1.<if>標(biāo)簽
<select id="selectUser" resultType="User">
select * from user
<if test="username!='' and username!=null">
where username=#{username}
</if>
</select>2.<where>標(biāo)簽
只使用if會(huì)有缺點(diǎn)
<select id="selectUser" resultType="User">
select * from user
where
<if test="username!='' and username!=null">
username=#{username}
</if>
<if test="sex!=null">
and sex=#{sex}
</if>
</select>用where可以避免
<select id="selectUser" resultType="User">
select * from user
<where>
<if test="username!='' and username!=null">
and username=#{username}
</if>
<if test="sex!=null">
and sex=#{sex}
</if>
</where>
</select>智能識(shí)別多余的and和where,保證構(gòu)造SQL的語(yǔ)法正確
3.<choose>標(biāo)簽
<select id="selectUser" resultType="User">
select * from user
<where>
<choose>
<when test="username!='' and username!=null">
and username=#{username}
</when>
<when test="sex!=null">
and sex=#{sex}
</when>
</choose>
</where>
</select>4.<set>標(biāo)簽
只用if同where標(biāo)簽一樣,沒(méi)有username會(huì)出現(xiàn)SQL語(yǔ)句錯(cuò)誤
<update id="updateUser" resultType="string">
update user
set
<if test="username!='' and username!=null">
username=#{username}
</if>
<if test="sex!=null">
,sex=#{sex}
</if>
where id=#{id}
</update>使用<set>標(biāo)簽
<update id="updateUser" resultType="string">
update user
<set>
<if test="username!='' and username!=null">
,username=#{username}
</if>
<if test="sex!=null">
,sex=#{sex}
</if>
</set>
where id=#{id}
</update>5.<trim>標(biāo)簽(自由)
trim標(biāo)簽:可用于拼接動(dòng)態(tài)SQL語(yǔ)句
該標(biāo)簽有以下屬性:
prefix:前綴
suffix:后綴
prefixOverrides:前綴覆蓋,可用于智能的處理”and”,”or”關(guān)鍵字
suffixOverrides:后綴覆蓋,可用于處理update語(yǔ)句中中多余的”,”
作用:可以利用trim來(lái)代替<where>或者<set>的功能
使用trim代替where

使用trim代替set
<update id="updateUser" resultType="string">
update user
<trim prefix="set" prefixOverrides=",">
<if test="username!='' and username!=null">
,username=#{username}
</if>
<if test="sex!=null">
,sex=#{sex}
</if>
</set>
where id=#{id}
</update>6.<foreach>標(biāo)簽
foreach標(biāo)簽可以在SQL語(yǔ)句中迭代一個(gè)集合,常用于構(gòu)造sql中in條件
foreach標(biāo)簽的屬性主要有 item,index,collection,open,separator,close。
item:表示對(duì)集合進(jìn)行迭代時(shí)每一個(gè)對(duì)象的別名。
index:指定一個(gè)名字,用于表示在迭代過(guò)程中,每次迭代的位置open是前綴,表示該語(yǔ)句以什么開始。
separator:表示在每次迭代元素之間以什么符號(hào)作為分隔符。
close:是后綴,表示以什么結(jié)束。
collection:指定需要遍歷的集合。
<select id="selectStudentById" resultType="Uer">
select * from user
where id in
<foreach collection="list"
item="stu"
open="("
close=")"
separator=",">
#{stu}
</foreach>
</select>到此這篇關(guān)于MyBatis動(dòng)態(tài)<if>標(biāo)簽的使用的文章就介紹到這了,更多相關(guān)MyBatis動(dòng)態(tài)<if>標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?EnableAsync注解異步執(zhí)行源碼解析
這篇文章主要為大家介紹了Spring?EnableAsync注解源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Java中提供synchronized后為什么還要提供Lock
這篇文章主要介紹了Java中提供synchronized后為什么還要提供Lock,在Java中提供了synchronized關(guān)鍵字來(lái)保證只有一個(gè)線程能夠訪問(wèn)同步代碼塊,下文更多相關(guān)資料需要的小伙伴可以參考一下2022-03-03
Java實(shí)現(xiàn)畫圖的詳細(xì)步驟(完整代碼)
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Java實(shí)現(xiàn)畫圖的詳細(xì)步驟展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Spring?IOC容器Bean注解創(chuàng)建對(duì)象組件掃描
這篇文章主要為大家介紹了Spring?IOC容器Bean注解創(chuàng)建對(duì)象組件掃描,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Java中隨機(jī)數(shù)的產(chǎn)生方式與原理詳解
這篇文章主要介紹了Java中隨機(jī)數(shù)的產(chǎn)生方式與原理詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
SpringBoot RestTemplate 簡(jiǎn)單包裝解析
這篇文章主要介紹了SpringBoot RestTemplate 簡(jiǎn)單包裝解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

