mybatis動(dòng)態(tài)新增(insert)和修改(update)方式
mybatis動(dòng)態(tài)新增(insert)和修改(update)
動(dòng)態(tài)操作這里使用到了標(biāo)簽
trim標(biāo)記是一個(gè)格式化的標(biāo)記,主要用于拼接sql的條件語(yǔ)句(前綴或后綴的添加或忽略),可以完成set或者是where標(biāo)記的功能。
標(biāo)簽的四個(gè)主要的屬性:
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>
這里會(huì)忽略最后的逗號(hào)“,”
修改
<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語(yǔ)句中省略前綴and,當(dāng)然我們也可以使用 where 1=1 后面再跟上判斷語(yǔ)句
mybatis判斷用insert還是update
在實(shí)際開(kāi)發(fā)中會(huì)遇到這種情況,就是一條數(shù)據(jù)需要判斷是新增還是更新,正常的開(kāi)發(fā)思路是先去查詢(xún)這條數(shù)據(jù)的Id是否已經(jīng)存在于數(shù)據(jù)庫(kù),存在就是update,否則為insert,mybatis也是基于這樣的思想實(shí)現(xiàn)的,下面就舉個(gè)例子看一下。
具體實(shí)現(xiàn)
比如,前臺(tái)將一條教師的信息保存到教師的實(shí)體bean中,然后需要將這條信息保存到數(shù)據(jù)庫(kù)中,這時(shí)需要判斷一下教師信息是要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用來(lái)判斷teacherId是否存在,如果存在就會(huì)將存在的個(gè)數(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)思路也是先查詢(xún)Id是否存在,在根據(jù)count判斷是insert還是update。
說(shuō)明
1.實(shí)現(xiàn)原理是selectKey做第一次查詢(xún),然后根據(jù)結(jié)果進(jìn)行判斷,所以這里的order="BEFORE"是必須的,也是因BEFORE,所以沒(méi)法通過(guò)<bind>標(biāo)簽來(lái)臨時(shí)存儲(chǔ)中間的值,只能在入?yún)⒅性黾訉傩詠?lái)存放。
2.就上面這個(gè)例子而言,就要求實(shí)體類(lèi)中包含count屬性(可以是別的名字)。否則selectKey的結(jié)果沒(méi)法保存,如果入?yún)⑹莻€(gè)Map類(lèi)型,就沒(méi)有這個(gè)限制。
3.這種方式只是利用了selectKey會(huì)多執(zhí)行一次查詢(xún)來(lái)實(shí)現(xiàn)的,但是如果你同時(shí)還需要通過(guò)selectKey獲取序列或者自增的id,就會(huì)麻煩很多(oracle麻煩,其他支持自增的還是很容易),例如我在上一篇中利用selectKey 獲取主鍵Id。
4.建議單獨(dú)查看學(xué)習(xí)一下selectKey的用法。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
小白也可以學(xué)會(huì)的Java NIO的Write事件
剛開(kāi)始對(duì)NIO的寫(xiě)操作理解的不深,不知道為什么要注冊(cè)寫(xiě)事件,何時(shí)注冊(cè)寫(xiě)事件,為什么寫(xiě)完之后要取消注冊(cè)寫(xiě)事件,今天特地整理了本篇文章,需要的朋友可以參考下2021-06-06SpringBoot結(jié)合Vue實(shí)現(xiàn)投票系統(tǒng)過(guò)程詳解
這篇文章主要介紹了SpringBoot+Vue框架實(shí)現(xiàn)投票功能的項(xiàng)目系統(tǒng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09Spring Cloud Gateway內(nèi)置的斷言和過(guò)濾器作用說(shuō)明
這篇文章主要介紹了Spring Cloud Gateway內(nèi)置的斷言和過(guò)濾器作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06tk.mybatis如何擴(kuò)展自己的通用mapper
這篇文章主要介紹了tk.mybatis如何擴(kuò)展自己的通用mapper操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06java swing實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲
這篇文章主要為大家詳細(xì)介紹了java swing實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03idea maven 項(xiàng)目src下的配置文件沒(méi)有同步至target的解決操作
這篇文章主要介紹了idea maven 項(xiàng)目src下的配置文件沒(méi)有同步至target的解決操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Springboot配置過(guò)濾器實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Springboot配置過(guò)濾器實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot項(xiàng)目引入MCP的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot項(xiàng)目引入MCP的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04