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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • idea 普通文件夹 转换成 module操作

    idea 普通文件夹 转换成 module操作

    这篇文章主要介绍了idea 普通文件夹 转换成 module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • springboot jpaRepository为何一定要对Entity序列化

    springboot jpaRepository为何一定要对Entity序列化

    这篇文章主要介绍了springboot jpaRepository为何一定要对Entity序列化,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • springboot使用RedisRepository操作数据的实现

    springboot使用RedisRepository操作数据的实现

    本文主要介绍了springboot使用RedisRepository操作数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

    SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

    这篇文章主要介绍了SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • SpringBoot整合mybatis通用Mapper+自定义通用Mapper方法解析

    SpringBoot整合mybatis通用Mapper+自定义通用Mapper方法解析

    这篇文章主要介绍了SpringBoot整合mybatis通用Mapper+自定义通用Mapper方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • SSH框架网上商城项目第16战之Hibernate二级缓存处理首页热门显示

    SSH框架网上商城项目第16战之Hibernate二级缓存处理首页热门显示

    这篇文章主要介绍了SSH框架网上商城项目第16战之Hibernate的二级缓存处理首页的热门显示,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • J2EE基础之EJB全面了解

    J2EE基础之EJB全面了解

    下面小编就为大家带来一篇J2EE基础之EJB全面了解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • 使用Autowired为什么会被IDEA警告最佳修改方法

    使用Autowired为什么会被IDEA警告最佳修改方法

    这篇文章主要介绍了使用Autowired为什么会被IDEA警告,应该怎么修改最佳,除了使用@Autowired以外,我们其实也有几种好用的方式,使用@Resource替代@Autiwired方法是其中一种,只需要改变一个注解,这里就不展示了,需要的朋友可以参考下
    2023-02-02
  • 关于Java应用日志与Jaeger的trace关联的问题

    关于Java应用日志与Jaeger的trace关联的问题

    这篇文章主要介绍了Java应用日志如何与Jaeger的trace关联,通过jaeger发现这十次请求中有一次耗时特别长,想定位一下具体原因,感兴趣的朋友跟随小编一起看看吧
    2022-01-01
  • javaweb开发提高效率利器JRebel详解

    javaweb开发提高效率利器JRebel详解

    这篇文章主要介绍了javaweb开发提高效率利器JRebel详解,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04

最新评论