mybatis批量插入
时间: 2023-09-29 18:02:27 浏览: 45
在MyBatis中执行批量插入操作可以使用`foreach`标签。以下是一个示例:
```xml
<insert id="batchInsert" parameterType="java.util.List">
INSERT INTO your_table (column1, column2, column3)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.value1}, #{item.value2}, #{item.value3})
</foreach>
</insert>
```
在上面的示例中,我们使用`foreach`标签来迭代传入的列表并生成批量插入语句。`list`是传入的参数,`item`是迭代过程中的当前元素。在`VALUES`子句中,我们使用`#{item.value1}`、`#{item.value2}`和`#{item.value3}`来引用当前元素的属性值。
然后,在Java代码中调用这个批量插入的方法:
```java
List<YourObject> dataList = new ArrayList<>();
// 添加数据到dataList...
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
YourMapper yourMapper = sqlSession.getMapper(YourMapper.class);
yourMapper.batchInsert(dataList);
sqlSession.commit();
} finally {
sqlSession.close();
}
```
请确保替
阅读全文
相关推荐















