mybatis動態(tài)新增(insert)和修改(update)方式
mybatis動態(tài)新增(insert)和修改(update)
動態(tài)操作這里使用到了標簽
trim標記是一個格式化的標記,主要用于拼接sql的條件語句(前綴或后綴的添加或忽略),可以完成set或者是where標記的功能。
標簽的四個主要的屬性:
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標簽還可以在where語句中省略前綴and,當然我們也可以使用 where 1=1 后面再跟上判斷語句
mybatis判斷用insert還是update
在實際開發(fā)中會遇到這種情況,就是一條數(shù)據(jù)需要判斷是新增還是更新,正常的開發(fā)思路是先去查詢這條數(shù)據(jù)的Id是否已經(jīng)存在于數(shù)據(jù)庫,存在就是update,否則為insert,mybatis也是基于這樣的思想實現(xiàn)的,下面就舉個例子看一下。
具體實現(xiàn)
比如,前臺將一條教師的信息保存到教師的實體bean中,然后需要將這條信息保存到數(shù)據(jù)庫中,這時需要判斷一下教師信息是要update還是insert。
教師信息實體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;
}
}可以看到在實體bean中除了正常的教師信息外多了一count,它就是mybatis用來判斷teacherId是否存在,如果存在就會將存在的個數(shù)保存到count中,當然一般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的實現(xiàn)思路也是先查詢Id是否存在,在根據(jù)count判斷是insert還是update。
說明
1.實現(xiàn)原理是selectKey做第一次查詢,然后根據(jù)結果進行判斷,所以這里的order="BEFORE"是必須的,也是因BEFORE,所以沒法通過<bind>標簽來臨時存儲中間的值,只能在入?yún)⒅性黾訉傩詠泶娣拧?/p>
2.就上面這個例子而言,就要求實體類中包含count屬性(可以是別的名字)。否則selectKey的結果沒法保存,如果入?yún)⑹莻€Map類型,就沒有這個限制。
3.這種方式只是利用了selectKey會多執(zhí)行一次查詢來實現(xiàn)的,但是如果你同時還需要通過selectKey獲取序列或者自增的id,就會麻煩很多(oracle麻煩,其他支持自增的還是很容易),例如我在上一篇中利用selectKey 獲取主鍵Id。
4.建議單獨查看學習一下selectKey的用法。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決Mybatis-plus自定義TypeHandler查詢映射結果一直為null問題
這篇文章主要介紹了解決Mybatis-plus自定義TypeHandler查詢映射結果一直為null問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Mybatis反向工程出現(xiàn)BigDecimal類型問題及解決
這篇文章主要介紹了Mybatis反向工程出現(xiàn)BigDecimal類型問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
Maven依賴管理之parent與dependencyManagement深入分析
首先我們來說說parent標簽,其實這個不難解釋,就是父的意思,pom也有繼承的。比方說我現(xiàn)在有A,B,C,A是B,C的父級?,F(xiàn)在就是有一個情況B,C其實有很多jar都是共同的,其實是可以放在父項目里面,這樣,讓B,C都繼承A就方便管理了2022-10-10
Java中把一個文件夾下的所有文件復制到另一個文件夾的完整實現(xiàn)方案
這篇文章主要介紹了如何使用Java原生API實現(xiàn)文件夾復制,支持多級目錄、空文件夾和文件覆蓋等場景,并提供了基于File和FileChannel的實現(xiàn)方案,此外,還介紹了使用Java 7的Files工具類進行簡化實現(xiàn),需要的朋友可以參考下2026-01-01

