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

 更新时间:2021年05月31日 09:08:00   作者:Leafage  
Mybatis技术,现在是工作中使用频率越来越高,我们在对数据库进行操作的时候,经常会遇到批量操作的需求,这篇文章主要给大家介绍了关于Mybatis批量操作sql写法的相关资料,需要的朋友可以参考下

在使用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>

总结

到此这篇关于Mybatis批量操作sql写法的文章就介绍到这了,更多相关Mybatis批量操作sql内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • MyBatis-Plus实现公共字段自动填充功能详解

    MyBatis-Plus实现公共字段自动填充功能详解

    在开发中经常遇到多个实体类有共同的属性字段,这些字段属于公共字段,也就是很多表中都有这些字段,能不能对于这些公共字段在某个地方统一处理,来简化开发呢?MyBatis-Plus就提供了这一功能,本文就来为大家详细讲讲
    2022-08-08
  • Java中SimpleDateFormat用法详解

    Java中SimpleDateFormat用法详解

    SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化.这篇文章主要介绍了Java中SimpleDateFormat用法详解,需要的朋友可以参考下
    2017-03-03
  • Java 执行CMD命令或执行BAT批处理方式

    Java 执行CMD命令或执行BAT批处理方式

    这篇文章主要介绍了Java 执行CMD命令或执行BAT批处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 批量将现有Jar包上传到Maven私服

    批量将现有Jar包上传到Maven私服

    今天小编就为大家分享一篇关于批量将现有Jar包上传到Maven私服,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Mybatis常见注解有哪些(总结)

    Mybatis常见注解有哪些(总结)

    这篇文章主要介绍了Mybatis常见注解有哪些(总结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Spring boot实现上传文件到本地服务器

    Spring boot实现上传文件到本地服务器

    这篇文章主要为大家详细介绍了Spring boot实现上传文件到本地服务器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • SpringMVC找不到Controller路径的解决方案

    SpringMVC找不到Controller路径的解决方案

    这篇文章主要介绍了SpringMVC找不到Controller路径的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • java的socket请求和响应方式

    java的socket请求和响应方式

    这篇文章主要介绍了java的socket请求和响应方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • 浅谈SpringBoot优化技巧

    浅谈SpringBoot优化技巧

    这篇文章主要介绍了浅谈SpringBoot优化技巧,需要的朋友可以参考下。
    2017-09-09
  • 浅谈Java中SimpleDateFormat 多线程不安全原因

    浅谈Java中SimpleDateFormat 多线程不安全原因

    SimpleDateFormat是Java中用于日期时间格式化的一个类,本文主要介绍了浅谈Java中SimpleDateFormat 多线程不安全原因,感兴趣的可以了解一下
    2024-01-01

最新评论