欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Mybatis批量操作sql写法示例(批量新增、更新)

时间:2021-11-21 10:51:20|栏目:JAVA代码|点击:

在使用foreach时,collection属性值的三种情况:

如果传入的参数类型为List时,collection的默认属性值为list,同样可以使用@Param注解自定义keyName;

如果传入的参数类型为array时,collection的默认属性值为array,同样可以使用@Param注解自定义keyName;

如果传入的参数类型为Map时,collection的属性值可为三种情况:

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

批量Insert,参数为List<Object>

mysql的批量新增sql的写法示例,先看一下mapper的写法;

    void batchSaveUser(List<SysUser> userList);

接下来看sql如何写:

   <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,参数为Map<Long, List<Long>>

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

接下来看sql如何写:

 <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,参数为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,写法则是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,写法则是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,参数为List<Object>

**注意:**在执行批量Update的时候,数据库的url配置需要添加一项参数:&allowMultiQueries=true

如果没有这个配置参数的话,执行下面的更新语句会报错:

正确的sql写法如下:

 <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>

总结

上一篇:Tomcat 实现WebSocket详细介绍

栏    目:JAVA代码

下一篇:深入Spring Boot之ClassLoader的继承关系和影响

本文标题:Mybatis批量操作sql写法示例(批量新增、更新)

本文地址:http://www.codeinn.net/misctech/184341.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有