Mybatis動態(tài)SQL標(biāo)簽的使用
動態(tài)SQL是MyBatis的一個強大特性,可以運用動態(tài)SQL語句標(biāo)簽方便我們在SQL中實現(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會有缺點
<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>智能識別多余的and和where,保證構(gòu)造SQL的語法正確
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)簽一樣,沒有username會出現(xiàn)SQL語句錯誤
<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)簽:可用于拼接動態(tài)SQL語句
該標(biāo)簽有以下屬性:
prefix:前綴
suffix:后綴
prefixOverrides:前綴覆蓋,可用于智能的處理”and”,”or”關(guān)鍵字
suffixOverrides:后綴覆蓋,可用于處理update語句中中多余的”,”
作用:可以利用trim來代替<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語句中迭代一個集合,常用于構(gòu)造sql中in條件
foreach標(biāo)簽的屬性主要有 item,index,collection,open,separator,close。
item:表示對集合進行迭代時每一個對象的別名。
index:指定一個名字,用于表示在迭代過程中,每次迭代的位置open是前綴,表示該語句以什么開始。
separator:表示在每次迭代元素之間以什么符號作為分隔符。
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動態(tài)<if>標(biāo)簽的使用的文章就介紹到這了,更多相關(guān)MyBatis動態(tài)<if>標(biāo)簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?EnableAsync注解異步執(zhí)行源碼解析
這篇文章主要為大家介紹了Spring?EnableAsync注解源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Java中提供synchronized后為什么還要提供Lock
這篇文章主要介紹了Java中提供synchronized后為什么還要提供Lock,在Java中提供了synchronized關(guān)鍵字來保證只有一個線程能夠訪問同步代碼塊,下文更多相關(guān)資料需要的小伙伴可以參考一下2022-03-03
Spring?IOC容器Bean注解創(chuàng)建對象組件掃描
這篇文章主要為大家介紹了Spring?IOC容器Bean注解創(chuàng)建對象組件掃描,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
SpringBoot RestTemplate 簡單包裝解析
這篇文章主要介紹了SpringBoot RestTemplate 簡單包裝解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08

