MyBatis使用標(biāo)簽動態(tài)操作數(shù)據(jù)庫詳解
前言
動態(tài)SQL是Mybatis的強(qiáng)大特性之?,能夠完成不同條件下不同的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)該如何實(shí)現(xiàn)呢?
這個時候就需要使用動態(tài)標(biāo)簽來判斷了,比如添加的時候性別gender為非必填字段,具體實(shí)現(xiàn)如下:
Integer insertUserByCondition(UserInfo userInfo);
Mapper.xml實(shí)現(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:可不可以不進(jìn)行判斷,直接把字段設(shè)置為null呢?
A:不可以,這種情況下,如果gender字段有默認(rèn)值,就會設(shè)置為默認(rèn)值
2. <trim>標(biāo)簽
之前的插入用戶功能,只是有?個gender字段可能是選填項(xiàng),如果有多個字段,?般考慮使用標(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實(shí)現(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)簽
對集合進(jìn)行遍歷時可以使用該標(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ù)的代碼片段進(jìn)行抽取,將其通過 標(biāo)簽封裝到?個SQL片段,然后再通過<include> 標(biāo)簽進(jìn)行引用。
:定義可重用的SQL片段 :通過屬性refid,指定包含的SQL片段
<sql id="allColumn"> id, username, age, gender, phone, delete_flag, create_time, update_time </sql>
通過 標(biāo)簽在原來抽取的地方進(jìn)行引用。操作如下:
<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ù)庫詳解的詳細(xì)內(nèi)容,更多關(guān)于MyBatis動態(tài)數(shù)據(jù)庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java構(gòu)造函數(shù)與普通函數(shù)用法詳解
本篇文章給大家詳細(xì)講述了Java構(gòu)造函數(shù)與普通函數(shù)用法以及相關(guān)知識點(diǎn),對此有興趣的朋友可以參考學(xué)習(xí)下。2018-03-03Spring?Boot數(shù)據(jù)響應(yīng)問題實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Spring?Boot數(shù)據(jù)響應(yīng)問題的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03解決mybatis plus報(bào)錯Invalid bound statement
在使用MyBatis時遇到InvalidBoundStatement異常,常因多個MapperScan配置沖突或者包掃描路徑設(shè)置錯誤,解決方法包括保留一個MapperScan聲明、檢查jar包沖突、確保命名空間和掃描路徑正確,使用@TableId注解指定主鍵2024-11-11Java實(shí)現(xiàn)鼠標(biāo)模擬與鍵盤映射
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)鼠標(biāo)模擬與鍵盤映射,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08