Mybatis之foreach


一、foreach属性

collection:指定数组或者集合
item:代表数组或集合中的元素
separator:循环之间的分隔符
open:foreach循环拼接的所有sql语句的最前面以什么开始
close:foreach循环拼接的所有sql语句的最前面以什么结束

二、使用foreach批量删除(法一)

delete from t_car where id in(……)

1.接口

    /**
     * foreach标签 批量删除
     * @param ids
     * @return
     */
    int deleteByIds(Long[] ids);

2.mapper文件

使用foreach标签时,collection这个属性的值应该是什么?
假设先使用接口中传进来的参数。

如果不想写where id in "()"这两个括号 可以使用open、close属性

<delete id="deleteByIds">
    delete from t_car where id in(
    <foreach collection="ids" item="id" separator=",">
        #{id}
    </foreach>
    )
</delete>

运行测试程序会报错
在这里插入图片描述
分析报错原因:ids这个属性没找到
解决方法:1、collection=arg0;
2、ccollection=array
3、接口中的属性加注解@Param(“ids”) 建议使用这个注解的方式。

3.测试类

    @Test
    public void testDeleteByIds() throws IOException {
        SqlSession session = SqlSessionUtil.openSession();
        CarMapper mapper = session.getMapper(CarMapper.class);
        Long[] ids ={12L,13L,15L};
        int count = mapper.deleteByIds(ids);
        System.out.println(count);
        session.commit();
        session.close();
    }

4.运行结果

删除了3条数据
在这里插入图片描述

三、使用foreach批量删除(法二)

delete from t_car where id=1 or id=2 ……

1.mapper文件

separator 分隔符这个属性的值改为 or即可

<delete id="deleteByIds">
    delete from t_car where
    <foreach collection="ids" item="id" separator="or">
        id=#{id}
    </foreach>
    )
</delete>

四、使用foreach批量插入

一次向数据库表中插入多条记录。
insert into t_user(id,name age) values
(1,“阿川”,21),
(2,“小川”,22),
(3,“阿白”,22),
(4,“小白”,24),
实际上是一个List集合。

1.接口

    /**
     * 一次插入多条记录
     * @param cars
     * @return
     */
    int insertBatch(@Param("cars") List<Car> cars);

2.mapper文件

<insert id="insertBatch">
    insert into t_car values
    <foreach collection="cars" item="car" separator=",">
        (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
    </foreach>
</insert>

3.测试类

    @Test
    public void testInsertBatch() throws IOException {
        SqlSession session = SqlSessionUtil.openSession();
        CarMapper mapper = session.getMapper(CarMapper.class);
        Car car1 = new Car(null,"111","奔奔",32.0,"2022-11-14","代步车");
        Car car2 = new Car(null,"112","奥迪",62.0,"2022-10-14","新能源");
        Car car3 = new Car(null,"113","比亚迪",72.0,"2022-11-15","电车");
        Car car4 = new Car(null,"114","大众",82.0,"2022-11-10","电动车");
        Car car5 = new Car(null,"115","QQ",92.0,"2022-11-4","燃油车");
        List<Car> cars = new ArrayList<>();
        cars.add(car1);
        cars.add(car2);
        cars.add(car3);
        cars.add(car4);
        cars.add(car5);
        int count = mapper.insertBatch(cars);
        session.commit();
        session.close();
        System.out.println(count);
    }

4.运行结果

5条记录插入成功
在这里插入图片描述

执行前:
在这里插入图片描述
执行后:

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值