mybatis動態(tài)新增(insert)和修改(update)方式
mybatis動態(tài)新增(insert)和修改(update)
動態(tài)操作這里使用到了標(biāo)簽
trim標(biāo)記是一個格式化的標(biāo)記,主要用于拼接sql的條件語句(前綴或后綴的添加或忽略),可以完成set或者是where標(biāo)記的功能。
標(biāo)簽的四個主要的屬性:
prefix
:前綴覆蓋并增加其內(nèi)容suffix
:后綴覆蓋并增加其內(nèi)容prefixOverrides
:前綴判斷的條件suffixOverrides
:后綴判斷的條件
新增
<insert id="saveDynamicCow" useGeneratedKeys="true" keyProperty="intCowId"> insert into cowtest <trim prefix="(" suffix=")" suffixOverrides=","> <if test="intPastureId != null and '' != intPastureId"> intPastureId, </if> <if test="varCowCode != null and '' != varCowCode"> varCowCode, </if> <if test="cSex != null and '' != cSex"> cSex, </if> <if test="addSource != null and '' != addSource"> addSource, </if> <if test="sireClass != null and '' != sireClass"> sireClass, </if> <if test="dateLeave != null and '' != dateLeave"> dateLeave, </if> <if test="intLeaveClass != null and '' != intLeaveClass"> intLeaveClass, </if> <if test="intReason != null and '' != intReason"> intReason, </if> <if test="intCurBar != null and '' != intCurBar"> intCurBar, </if> <if test="intCurBarName != null and '' != intCurBarName"> intCurBarName, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="intPastureId != null and '' != intPastureId"> #{intPastureId}, </if> <if test="varCowCode != null and '' != varCowCode"> #{varCowCode}, </if> <if test="cSex != null and '' != cSex"> #{cSex}, </if> <if test="addSource != null and '' != addSource"> #{addSource}, </if> <if test="sireClass != null and '' != sireClass"> #{sireClass}, </if> <if test="dateLeave != null and '' != dateLeave"> #{dateLeave}, </if> <if test="intLeaveClass != null and '' != intLeaveClass"> #{intLeaveClass}, </if> <if test="intReason != null and '' != intReason"> #{intReason}, </if> <if test="intCurBar != null and '' != intCurBar"> #{intCurBar}, </if> <if test="intCurBarName != null and '' != intCurBarName"> #{intCurBarName}, </if> </trim> </insert>
這里會忽略最后的逗號“,”
修改
<update id="updateDynamicCow"> update cowtest <trim prefix="SET" suffixOverrides=","> <if test="dateBirthDate != null and '' != dateBirthDate"> dateBirthDate= #{dateBirthDate}, </if> <if test="decBirWeight != null and '' != decBirWeight"> decBirWeight= #{decBirWeight}, </if> <if test="decQuotiety != null and '' != decQuotiety"> decQuotiety= #{decQuotiety}, </if> <if test="intCurBar != null and '' != intCurBar"> intCurBar= #{intCurBar}, </if> <if test="intCurBarName != null and '' != intCurBarName"> intCurBarName= #{intCurBarName}, </if> <if test="intCurFetal != null and '' != intCurFetal"> intCurFetal= #{intCurFetal}, </if> <if test="intBreed != null and '' != intBreed"> intBreed= #{intBreed}, </if> <if test="cSex != null and '' != cSex"> cSex= #{cSex}, </if> </trim> where varCowCode= #{varCowCode} </update>
此外
trim標(biāo)簽還可以在where語句中省略前綴and,當(dāng)然我們也可以使用 where 1=1 后面再跟上判斷語句
mybatis判斷用insert還是update
在實(shí)際開發(fā)中會遇到這種情況,就是一條數(shù)據(jù)需要判斷是新增還是更新,正常的開發(fā)思路是先去查詢這條數(shù)據(jù)的Id是否已經(jīng)存在于數(shù)據(jù)庫,存在就是update,否則為insert,mybatis也是基于這樣的思想實(shí)現(xiàn)的,下面就舉個例子看一下。
具體實(shí)現(xiàn)
比如,前臺將一條教師的信息保存到教師的實(shí)體bean中,然后需要將這條信息保存到數(shù)據(jù)庫中,這時需要判斷一下教師信息是要update還是insert。
教師信息實(shí)體bean如下:Teacher.java
public class Teacher { private int teacherId;//教師Id private String teacherName;//教師名 private int count;//mybatis判斷Id是否存在 public int getTeacherId() { return teacherId; } public void setTeacherId(int teacherId) { this.teacherId = teacherId; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
可以看到在實(shí)體bean中除了正常的教師信息外多了一count,它就是mybatis用來判斷teacherId是否存在,如果存在就會將存在的個數(shù)保存到count中,當(dāng)然一般Id都是主鍵,所有count也就一般都是1。
下邊看一下mybatis的映射文件。
<insert id="AddTeacher" parameterType="com.mycompany.entity.Teacher"> <selectKey keyProperty="count" resultType="int" order="BEFORE"> select count(*) from Teacher where teacher_id = #{teacherId} </selectKey> <if test="count > 0"> update event <set> <if test="teacherName!= null" > teacher_name= #{teacherName}, </if> </set> <where> teacher_id = #{teacherId} </where> </if> <if test="count==0"> insert into teacher(teacher_id,teacher_name) values (#{teacherId},#{teacherName}) </if> </insert>
可以看到mybatis的實(shí)現(xiàn)思路也是先查詢Id是否存在,在根據(jù)count判斷是insert還是update。
說明
1.實(shí)現(xiàn)原理是selectKey做第一次查詢,然后根據(jù)結(jié)果進(jìn)行判斷,所以這里的order="BEFORE"是必須的,也是因BEFORE,所以沒法通過<bind>標(biāo)簽來臨時存儲中間的值,只能在入?yún)⒅性黾訉傩詠泶娣拧?/p>
2.就上面這個例子而言,就要求實(shí)體類中包含count屬性(可以是別的名字)。否則selectKey的結(jié)果沒法保存,如果入?yún)⑹莻€Map類型,就沒有這個限制。
3.這種方式只是利用了selectKey會多執(zhí)行一次查詢來實(shí)現(xiàn)的,但是如果你同時還需要通過selectKey獲取序列或者自增的id,就會麻煩很多(oracle麻煩,其他支持自增的還是很容易),例如我在上一篇中利用selectKey 獲取主鍵Id。
4.建議單獨(dú)查看學(xué)習(xí)一下selectKey的用法。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot結(jié)合Vue實(shí)現(xiàn)投票系統(tǒng)過程詳解
這篇文章主要介紹了SpringBoot+Vue框架實(shí)現(xiàn)投票功能的項(xiàng)目系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09Spring Cloud Gateway內(nèi)置的斷言和過濾器作用說明
這篇文章主要介紹了Spring Cloud Gateway內(nèi)置的斷言和過濾器作用說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06tk.mybatis如何擴(kuò)展自己的通用mapper
這篇文章主要介紹了tk.mybatis如何擴(kuò)展自己的通用mapper操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06java swing實(shí)現(xiàn)簡單的五子棋游戲
這篇文章主要為大家詳細(xì)介紹了java swing實(shí)現(xiàn)簡單的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03idea maven 項(xiàng)目src下的配置文件沒有同步至target的解決操作
這篇文章主要介紹了idea maven 項(xiàng)目src下的配置文件沒有同步至target的解決操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Springboot配置過濾器實(shí)現(xiàn)過程解析
這篇文章主要介紹了Springboot配置過濾器實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08SpringBoot項(xiàng)目引入MCP的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot項(xiàng)目引入MCP的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04