欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis批量操作sql寫(xiě)法示例(批量新增、更新)

 更新時(shí)間:2021年05月31日 09:08:00   作者:Leafage  
Mybatis技術(shù),現(xiàn)在是工作中使用頻率越來(lái)越高,我們?cè)趯?duì)數(shù)據(jù)庫(kù)進(jìn)行操作的時(shí)候,經(jīng)常會(huì)遇到批量操作的需求,這篇文章主要給大家介紹了關(guān)于Mybatis批量操作sql寫(xiě)法的相關(guān)資料,需要的朋友可以參考下

在使用foreach時(shí),collection屬性值的三種情況:

如果傳入的參數(shù)類(lèi)型為L(zhǎng)ist時(shí),collection的默認(rèn)屬性值為list,同樣可以使用@Param注解自定義keyName;

如果傳入的參數(shù)類(lèi)型為array時(shí),collection的默認(rèn)屬性值為array,同樣可以使用@Param注解自定義keyName;

如果傳入的參數(shù)類(lèi)型為Map時(shí),collection的屬性值可為三種情況:

1.遍歷map.keys;
2.遍歷map.values;
3.遍歷map.entrySet()

批量Insert,參數(shù)為L(zhǎng)ist<Object>

mysql的批量新增sql的寫(xiě)法示例,先看一下mapper的寫(xiě)法;

    void batchSaveUser(List<SysUser> userList);

接下來(lái)看sql如何寫(xiě):

   <insert id="batchSaveUser">
       insert into sys_user (ding_user_id, username, nickname, password, email, 
       mobile, avatar, creator_id, create_time, updator_id, update_time, is_delete)
       values
       <foreach collection="list" item="user" separator=",">
           (
           #{user.dingUserId}, #{user.username}, #{user.nickname}, #{user.password}, #{user.email},
           #{user.mobile}, #{user.avatar}, #{user.creatorId}, now(), #{user.updatorId}, now(), 0
           )
       </foreach>
   </insert>

批量Insert,參數(shù)為Map<Long, List<Long>>

void batchSaveGroupAndUser(@Param("map") Map<Long, List<Long>> groupUserMap);

接下來(lái)看sql如何寫(xiě):

 <insert id="batchSaveGroupAndUser" parameterType="java.util.Map">
        insert into sys_group_member (group_id, user_id, creator_id, create_time)
        values
        <foreach collection="map.keys" item="groupId" separator=",">
            <foreach collection="map[groupId]" item="userId" separator=",">
                (
                #{groupId}, #{userId}, 'admin', now()
                )
            </foreach>
        </foreach>
    </insert>

批量Insert,參數(shù)為Map<String, String>

 void batchInsert(@Param("map") Map<String, String> map);
 <insert id="batchInsert" parameterType="java.util.Map">
        insert into brand_info (code, `name`, is_delete, create_time)
        values
        <foreach collection="map.entrySet()" index="key" item="value" open="(" close=")" separator=",">
            #{key}, #{value}, 0, now()
        </foreach>
    </insert>

如果是只需要遍歷key,寫(xiě)法則是collection=“map.keys”

 <insert id="batchSave" parameterType="java.util.Map">
        insert into brand_info (code, is_delete, create_time)
        values
        <foreach collection="map.keys" item="key" open="(" close=")" separator=",">
            #{key}, 0, now()
        </foreach>
    </insert>

同理,如果是只需要遍歷value,寫(xiě)法則是collection=“map.values”

 <insert id="batchSave" parameterType="java.util.Map">
        insert into brand_info (code, is_delete, create_time)
        values
        <foreach collection="map.values" item="value" open="(" close=")" separator=",">
            #{value}, 0, now()
        </foreach>
    </insert>

批量Update,參數(shù)為L(zhǎng)ist<Object>

**注意:**在執(zhí)行批量Update的時(shí)候,數(shù)據(jù)庫(kù)的url配置需要添加一項(xiàng)參數(shù):&allowMultiQueries=true

如果沒(méi)有這個(gè)配置參數(shù)的話,執(zhí)行下面的更新語(yǔ)句會(huì)報(bào)錯(cuò):

正確的sql寫(xiě)法如下:

 <update id="batchUpdateCorporation" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            update sys_corporation set
            <if test="item.name != null and item.name !=''">
                `name` = #{item.name},
            </if>
            <if test="item.code != null and item.code !=''">
                code = #{item.code},
            </if>
            <if test="item.parentCode != null and item.parentCode !=''">
                parent_code = #{item.parentCode},
            </if>
            updater = 'system',
            update_time = now()
            where id = #{item.id}
        </foreach>
    </update>

總結(jié)

到此這篇關(guān)于Mybatis批量操作sql寫(xiě)法的文章就介紹到這了,更多相關(guān)Mybatis批量操作sql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis-Plus實(shí)現(xiàn)公共字段自動(dòng)填充功能詳解

    MyBatis-Plus實(shí)現(xiàn)公共字段自動(dòng)填充功能詳解

    在開(kāi)發(fā)中經(jīng)常遇到多個(gè)實(shí)體類(lèi)有共同的屬性字段,這些字段屬于公共字段,也就是很多表中都有這些字段,能不能對(duì)于這些公共字段在某個(gè)地方統(tǒng)一處理,來(lái)簡(jiǎn)化開(kāi)發(fā)呢?MyBatis-Plus就提供了這一功能,本文就來(lái)為大家詳細(xì)講講
    2022-08-08
  • Java中SimpleDateFormat用法詳解

    Java中SimpleDateFormat用法詳解

    SimpleDateFormat 是一個(gè)以國(guó)別敏感的方式格式化和分析數(shù)據(jù)的具體類(lèi)。 它允許格式化 (date -> text)、語(yǔ)法分析 (text -> date)和標(biāo)準(zhǔn)化.這篇文章主要介紹了Java中SimpleDateFormat用法詳解,需要的朋友可以參考下
    2017-03-03
  • Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式

    Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式

    這篇文章主要介紹了Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 批量將現(xiàn)有Jar包上傳到Maven私服

    批量將現(xiàn)有Jar包上傳到Maven私服

    今天小編就為大家分享一篇關(guān)于批量將現(xiàn)有Jar包上傳到Maven私服,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • Mybatis常見(jiàn)注解有哪些(總結(jié))

    Mybatis常見(jiàn)注解有哪些(總結(jié))

    這篇文章主要介紹了Mybatis常見(jiàn)注解有哪些(總結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Spring boot實(shí)現(xiàn)上傳文件到本地服務(wù)器

    Spring boot實(shí)現(xiàn)上傳文件到本地服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了Spring boot實(shí)現(xiàn)上傳文件到本地服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • SpringMVC找不到Controller路徑的解決方案

    SpringMVC找不到Controller路徑的解決方案

    這篇文章主要介紹了SpringMVC找不到Controller路徑的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java的socket請(qǐng)求和響應(yīng)方式

    java的socket請(qǐng)求和響應(yīng)方式

    這篇文章主要介紹了java的socket請(qǐng)求和響應(yīng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 淺談SpringBoot優(yōu)化技巧

    淺談SpringBoot優(yōu)化技巧

    這篇文章主要介紹了淺談SpringBoot優(yōu)化技巧,需要的朋友可以參考下。
    2017-09-09
  • 淺談Java中SimpleDateFormat 多線程不安全原因

    淺談Java中SimpleDateFormat 多線程不安全原因

    SimpleDateFormat是Java中用于日期時(shí)間格式化的一個(gè)類(lèi),本文主要介紹了淺談Java中SimpleDateFormat 多線程不安全原因,感興趣的可以了解一下
    2024-01-01

最新評(píng)論