最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis動(dòng)態(tài)新增(insert)和修改(update)方式

 更新時(shí)間:2024年05月18日 10:15:39   作者:六六大歡  
這篇文章主要介紹了mybatis動(dòng)態(tài)新增(insert)和修改(update)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

mybatis動(dòng)態(tài)新增(insert)和修改(update)

動(dòng)態(tài)操作這里使用到了標(biāo)簽

trim標(biāo)記是一個(gè)格式化的標(biāo)記,主要用于拼接sql的條件語句(前綴或后綴的添加或忽略),可以完成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語句中省略前綴and,當(dāng)然我們也可以使用 where 1=1 后面再跟上判斷語句

mybatis判斷用insert還是update

在實(shí)際開發(fā)中會(huì)遇到這種情況,就是一條數(shù)據(jù)需要判斷是新增還是更新,正常的開發(fā)思路是先去查詢這條數(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用來判斷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)思路也是先查詢Id是否存在,在根據(jù)count判斷是insert還是update。

說明

1.實(shí)現(xiàn)原理是selectKey做第一次查詢,然后根據(jù)結(jié)果進(jìn)行判斷,所以這里的order="BEFORE"是必須的,也是因BEFORE,所以沒法通過<bind>標(biāo)簽來臨時(shí)存儲(chǔ)中間的值,只能在入?yún)⒅性黾訉傩詠泶娣拧?/p>

2.就上面這個(gè)例子而言,就要求實(shí)體類中包含count屬性(可以是別的名字)。否則selectKey的結(jié)果沒法保存,如果入?yún)⑹莻€(gè)Map類型,就沒有這個(gè)限制。

3.這種方式只是利用了selectKey會(huì)多執(zhí)行一次查詢來實(shí)現(xiàn)的,但是如果你同時(shí)還需要通過selectKey獲取序列或者自增的id,就會(huì)麻煩很多(oracle麻煩,其他支持自增的還是很容易),例如我在上一篇中利用selectKey 獲取主鍵Id。

4.建議單獨(dú)查看學(xué)習(xí)一下selectKey的用法。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題

    解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題

    這篇文章主要介紹了解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java新手啟航之JDK?21版本安裝開啟編程之路

    Java新手啟航之JDK?21版本安裝開啟編程之路

    Java是一門面向?qū)ο蟮木幊陶Z言,不僅吸收了C++語言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,這篇文章主要介紹了Java新手啟航之JDK?21版本安裝開啟編程之路的相關(guān)資料,需要的朋友可以參考下
    2025-12-12
  • Java中的分布式事務(wù)Seata詳解

    Java中的分布式事務(wù)Seata詳解

    這篇文章主要介紹了Java中的分布式事務(wù)Seata詳解,Seata 是一款開源的分布式事務(wù)解決方案,致力于提供高性能和簡(jiǎn)單易用的分布式事務(wù)服務(wù),Seata 將為用戶提供了 AT、TCC、SAGA 和 XA 事務(wù)模式,為用戶打造一站式的分布式解決方案,需要的朋友可以參考下
    2023-08-08
  • JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能

    JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能

    這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Mybatis反向工程出現(xiàn)BigDecimal類型問題及解決

    Mybatis反向工程出現(xiàn)BigDecimal類型問題及解決

    這篇文章主要介紹了Mybatis反向工程出現(xiàn)BigDecimal類型問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例

    Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例

    這篇文章主要介紹了Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Maven依賴管理之parent與dependencyManagement深入分析

    Maven依賴管理之parent與dependencyManagement深入分析

    首先我們來說說parent標(biāo)簽,其實(shí)這個(gè)不難解釋,就是父的意思,pom也有繼承的。比方說我現(xiàn)在有A,B,C,A是B,C的父級(jí)?,F(xiàn)在就是有一個(gè)情況B,C其實(shí)有很多jar都是共同的,其實(shí)是可以放在父項(xiàng)目里面,這樣,讓B,C都繼承A就方便管理了
    2022-10-10
  • java實(shí)現(xiàn)文件夾解壓和壓縮

    java實(shí)現(xiàn)文件夾解壓和壓縮

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件夾解壓和壓縮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • MyBatis中的JdbcType映射使用詳解

    MyBatis中的JdbcType映射使用詳解

    這篇文章主要介紹了MyBatis中的JdbcType映射使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java中把一個(gè)文件夾下的所有文件復(fù)制到另一個(gè)文件夾的完整實(shí)現(xiàn)方案

    Java中把一個(gè)文件夾下的所有文件復(fù)制到另一個(gè)文件夾的完整實(shí)現(xiàn)方案

    這篇文章主要介紹了如何使用Java原生API實(shí)現(xiàn)文件夾復(fù)制,支持多級(jí)目錄、空文件夾和文件覆蓋等場(chǎng)景,并提供了基于File和FileChannel的實(shí)現(xiàn)方案,此外,還介紹了使用Java 7的Files工具類進(jìn)行簡(jiǎn)化實(shí)現(xiàn),需要的朋友可以參考下
    2026-01-01

最新評(píng)論

景东| 广汉市| 个旧市| 沐川县| 涟水县| 凌云县| 洮南市| 益阳市| 乌审旗| 淮阳县| 庆云县| 青龙| 利川市| 城固县| 辽阳县| 华安县| 勃利县| 温泉县| 天镇县| 西盟| 井研县| 昭平县| 项城市| 通州区| 丰顺县| 什邡市| 新闻| 六盘水市| 岐山县| 县级市| 英山县| 锡林郭勒盟| 二手房| 凯里市| 谷城县| 郴州市| 宜良县| 阿鲁科尔沁旗| 呼图壁县| 芮城县| 汪清县|