Mybatis批量添加(foreach标签)

delete from  xxx_table where id  in 
<foreach collection="list" item="item" index="index" open="(" separator="," close=")"> 
		#{item}
</foreach>  => (1,2,3,4)

foreach标签:主要应用于批量删除与批量插入操作。

foreach标签包含的的常用属性有collection,item,index,open,separator,close

  • collection:表示迭代集合的名称,自定义名称,可以使用@Param注解指定
    • 只有一个List/Array/Map类型参数时,如果不加@Param注解,collection分别必须填:list、array、map中的key属性
    • 有多个参数时,不加@Param注解,collection和默认参数名称一致
    • 如果加@Param注解,都和注解定义一致
  • item:表示本次迭代获取的元素,自定义名称;表示本次迭代获取的元素,若collection为List、Set或者数组,则表示其中的元素;若collection为map,则代表key-value的value,该参数为必选
  • separator:分隔符,mybatis会在每次迭代后给sql语句append上separator属性指定的字符,该参数为可选项
  • open:表示该语句以什么开始,最常用的是左括弧’(’,注意:mybatis会将该字符拼接到整体的sql语句之前,并且只拼接一次,该参数为可选项
  • close:表示该语句以什么开始,最常用的是左括弧’(’,注意:mybatis会将该字符拼接到整体的sql语句之前,并且只拼接一次,该参数为可选项
  • index:在list、Set和数组中,index表示当前迭代的位置,在map中,index代指是元素的key,该参数是可选项。

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况

1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key。

使用示例

对于前两种情况的使用非常简单易懂,这里我们对第三种情况进行简单的使用,来进行一个批量插入。

controller层

        LocalDateTime localDateTime = LocalDateTime.now();
        teacherCourseList.setCreate_time(localDateTime);
        teacherCourseList.setUpdate_time(localDateTime);
        log.info("入参======="+teacherCourseList);

        Map<String,Object> map=new HashMap<>();
        map.put("username",teacherCourseList.getUsername());
        map.put("CourseList",teacherCourseList.getCourseList());
        map.put("create_time",teacherCourseList.getCreate_time());
        map.put("update_time",teacherCourseList.getUpdate_time());
        courseService.batchInsert(map);

xml

<insert id="batchInsert">
    insert into ref_teacher_course (teacher_id,course_id,create_time,update_time)
    values 
    <foreach collection="teacherCourseList.CourseList" item="item" index="index" separator=",">
        (#{teacherCourseList.username},
        #{item},
        #{teacherCourseList.create_time},
        #{teacherCourseList.update_time})
    </foreach>
</insert>

mapper

 void batchInsert(@Param("teacherCourseList") Map map);

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis中,可以使用foreach标签来实现批量插入操作。foreach标签可以用于循环遍历集合或数组,并执行相应的SQL语句。以下是几个示例: 1. 使用foreach批量删除数据: ``` delete from xxx_table where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> => (1,2,3,4) ``` 2. 使用foreach批量插入数据: ``` <insert id="batchInsert"> insert into ref_teacher_course (teacher_id,course_id,create_time,update_time) values <foreach collection="teacherCourseList.CourseList" item="item" index="index" separator=","> (#{teacherCourseList.username}, #{item}, #{teacherCourseList.create_time}, #{teacherCourseList.update_time}) </foreach> </insert> ``` 3. 在Mapper接口中定义批量保存方法: ``` @Mapper public interface UserMapper { void batchSave(List<User> userList); } ``` 4. 使用foreach批量保存数据: ``` <insert id="batchSave"> <foreach collection="list" item="user" separator=";"> insert into user(name, password) values (#{user.name}, #{user.password}) </foreach> </insert> ``` 在测试类中调用批量保存方法: ``` @RunWith(SpringRunner.class) @SpringBootTest public class SbMybatis02ApplicationTests { @Test public void testBatchSave(){ User user1 = new User(); user1.setName("关羽"); user1.setPassword("guanyu"); User user2 = new User(); user2.setName("张飞"); user2.setPassword("zhangfei"); List<User> userList = new ArrayList<>(); userList.add(user1); userList.add(user2); userMapper.batchSave(userList); } } ``` 以上就是使用MyBatis中的foreach实现批量插入的示例。使用foreach可以简化批量操作的编写,提高效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mybatis批量添加foreach标签)](https://blog.csdn.net/m0_51212267/article/details/121641252)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis 使用 foreach 批量插入](https://blog.csdn.net/lizhengyu891231/article/details/125546407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过街的老鼠

感谢你对诗仙女的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值