在MyBatis中,可以使用foreach标签来实现批量插入数据。具体步骤如下:
创建一个Mapper接口,定义一个insertBatch方法用于批量插入数据。public interface UserMapper { void insertBatch(List<User> userList);}在Mapper接口对应的XML文件中,编写insertBatch方法的SQL语句,并使用foreach标签来循环插入数据。<insert id="insertBatch" parameterType="java.util.List"> insert into user (name, age) values <foreach collection="list" item="item" index="index" separator=","> (#{item.name}, #{item.age}) </foreach></insert>在代码中调用insertBatch方法,传入需要插入的数据列表。List<User> userList = new ArrayList<>();userList.add(new User("Alice", 25));userList.add(new User("Bob", 30));userMapper.insertBatch(userList);通过以上步骤,就可以实现在MyBatis中批量插入数据。




