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

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

 更新時間:2024年05月18日 10:15:39   作者:六六大歡  
這篇文章主要介紹了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問題

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

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

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

    Java中的分布式事務Seata詳解

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

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

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

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

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

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

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

    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實現(xiàn)文件夾解壓和壓縮

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

    MyBatis中的JdbcType映射使用詳解

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

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

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

最新評論

荆门市| 黔西县| 临潭县| 武平县| 来宾市| 广昌县| 武冈市| 赤壁市| 义马市| 抚松县| 双辽市| 日喀则市| 大城县| 宽甸| 澄城县| 禄劝| 咸阳市| 盐津县| 楚雄市| 许昌县| 河间市| 临夏市| 浦北县| 定襄县| 中方县| 白沙| 江门市| 香河县| 新干县| 四子王旗| 卓尼县| 响水县| 洛川县| 宁城县| 东山县| 手游| 双牌县| 舟曲县| 扎兰屯市| 开平市| 万盛区|