MyBatis中怎么批量插入数据
在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中批量插入数据。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:python怎么检测变量是否设置 下一篇:python怎么修改元组中的元素
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。