MyBatis使用標(biāo)簽動態(tài)操作數(shù)據(jù)庫詳解
前言
動態(tài)SQL是Mybatis的強大特性之?,能夠完成不同條件下不同的sql拼接
在傳統(tǒng)的使用JDBC的方法中,相信大家在組合復(fù)雜的的SQL語句的時候,需要去拼接,稍不注意哪怕少了個空格,都會導(dǎo)致錯誤,Mybatis的動態(tài)SQL功能正是為了解決這種問題,其通過if, trim, where, set, foreach、include標(biāo)簽,可組合成非常靈活的SQL語句,從而提高開發(fā)人員的效率,下面就去感受Mybatis動態(tài)SQL的魅力吧。
1. <if>標(biāo)簽
在注冊用戶的時候,可能會有這樣?個問題,如下圖所示:

注冊分為兩種字段:必填字段和非必填字段,那如果在添加用戶的時候有不確定的字段傳入,程序應(yīng)該如何實現(xiàn)呢?
這個時候就需要使用動態(tài)標(biāo)簽來判斷了,比如添加的時候性別gender為非必填字段,具體實現(xiàn)如下:
Integer insertUserByCondition(UserInfo userInfo);
Mapper.xml實現(xiàn):
<insert id="insertUserByCondition">
INSERT INTO userinfo (
username,
`password`,
age,
<if test="gender != null">
gender,
</if>
phone)
VALUES (
#{username},
#{age},
<if test="gender != null">
#{gender},
</if>
#{phone})
</insert>注意test中的gender,是傳入對象中的屬性,不是數(shù)據(jù)庫字段
Q:可不可以不進行判斷,直接把字段設(shè)置為null呢?
A:不可以,這種情況下,如果gender字段有默認值,就會設(shè)置為默認值
2. <trim>標(biāo)簽
之前的插入用戶功能,只是有?個gender字段可能是選填項,如果有多個字段,?般考慮使用標(biāo)簽結(jié)合標(biāo)簽,對多個字段都采取動態(tài)生成的方式。
標(biāo)簽中有如下屬性:
- prefix:表示整個語句塊,以prefix的值作為前綴
- suffix:表示整個語句塊,以suffix的值作為后綴
- prefixOverrides:表示整個語句塊要去除掉的前綴
- suffixOverrides:表示整個語句塊要去除掉的后綴
調(diào)整Mapper.xml的插入語句為:
<insert id="insertUserByCondition">
INSERT INTO userinfo
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="username !=null">
username,
</if>
<if test="password !=null">
`password`,
</if>
<if test="age != null">
age,
</if>
<if test="gender != null">
gender,
</if>
<if test="phone != null">
phone,
</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="username !=null">
#{username},
</if>
<if test="password !=null">
#{password},
</if>
<if test="age != null">
#{age},
</if>
<if test="gender != null">
#{gender},
</if>
<if test="phone != null">
#{phone}
</if>
</trim>
</insert>3. <where>標(biāo)簽
看下?這個場景,系統(tǒng)會根據(jù)我們的篩選條件,動態(tài)組裝where條件
需求:傳入的用戶對象,根據(jù)屬性做where條件查詢,用戶對象中屬性不為null的,都為查詢條件
原有SQL:
SELECT * FROM userinfo WHERE age = 18 AND gender = 1 AND delete_flag =0
Mapper.xml實現(xiàn)
<select id="queryByCondition" resultType="com.example.demo.model.UserInfo">
select id, username, age, gender, phone, delete_flag, create_time,update_timefrom userinfo
<where>
<if test="age != null">
and age = #{age}
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="deleteFlag != null">
and delete_flag = #{deleteFlag}
</if>
</where>
</select>只會在子元素有內(nèi)容的情況下才插入where子句,?且會?動去除子句的開頭的AND或OR
以上標(biāo)簽也可以使用 替換,但是此種情況下,當(dāng)子元素都沒有內(nèi)容時,where關(guān)鍵字也會保留
4. <set>標(biāo)簽
需求:根據(jù)傳入的用戶對象屬性來更新用戶數(shù)據(jù),可以使用標(biāo)簽來指定動態(tài)內(nèi)容.
接口定義:根據(jù)傳入的用戶id屬性,修改其他不為null的屬性
Integer updateUserByCondition(UserInfo userInfo);
Mapper.xml
<update id="updateUserByCondition">
update userinfo
<set>
<if test="username != null">
username = #{username},
</if>
<if test="age != null">
age = #{age},
</if>
<if test="deleteFlag != null">
delete_flag = #{deleteFlag},
</if>
</set>
where id = #{id}
</update><set> :動態(tài)的在SQL語句中插入set關(guān)鍵字,并會刪掉額外的逗號.(用于update語句中)
以上標(biāo)簽也可以使用 替換。
5. <foreach>標(biāo)簽
對集合進行遍歷時可以使用該標(biāo)簽。標(biāo)簽有如下屬性:
- collection:綁定方法參數(shù)中的集合,如List,Set,Map或數(shù)組對象
- item:遍歷時的每?個對象
- open:語句塊開頭的字符串
- close:語句塊結(jié)束的字符串
- separator:每次遍歷之間間隔的字符串
需求:根據(jù)多個userid,刪除用戶數(shù)據(jù)
接口方法:
void deleteByIds(List<Integer> ids);
ArticleMapper.xml中新增刪除sql:
<delete id="deleteByIds">
delete from userinfo
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>6. <include>標(biāo)簽
問題分析:
在xml映射文件中配置的SQL,有時可能會存在很多重復(fù)的片段,此時就會存在很多冗余的代碼

我們可以對重復(fù)的代碼片段進行抽取,將其通過 標(biāo)簽封裝到?個SQL片段,然后再通過<include> 標(biāo)簽進行引用。
:定義可重用的SQL片段 :通過屬性refid,指定包含的SQL片段
<sql id="allColumn"> id, username, age, gender, phone, delete_flag, create_time, update_time </sql>
通過 標(biāo)簽在原來抽取的地方進行引用。操作如下:
<select id="queryAllUser" resultMap="BaseMap">
select
<include refid="allColumn"></include>
from userinfo
</select>
<select id="queryById" resultType="com.example.demo.model.UserInfo">
select
<include refid="allColumn"></include>
from userinfo where id= #{id}
</select>以上就是MyBatis使用標(biāo)簽動態(tài)操作數(shù)據(jù)庫詳解的詳細內(nèi)容,更多關(guān)于MyBatis動態(tài)數(shù)據(jù)庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot創(chuàng)建非可執(zhí)行jar包的實例教程
這篇文章主要介紹了Spring Boot創(chuàng)建非可執(zhí)行jar包的實例教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
SpringBoot+POI實現(xiàn)給word添加水印功能
這篇文章主要介紹了SpringBoot+POI實現(xiàn)給word添加水印功能,文中通過代碼示例講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06
RecyclerChart動態(tài)屬性圖標(biāo)聯(lián)動數(shù)據(jù)動態(tài)加載詳解
這篇文章主要為大家介紹了RecyclerChart動態(tài)屬性圖標(biāo)聯(lián)動數(shù)據(jù)動態(tài)加載詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Jboss Marshalling服務(wù)端無法接受消息
這篇文章主要介紹了Jboss Marshalling服務(wù)端無法接受消息,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03

