写一个springboot+mybatis-plus批量insert的代码
时间: 2024-01-15 14:03:00 浏览: 28
好的,下面是示例代码:
```java
@Autowired
private MybatisPlusConfig mybatisPlusConfig;
public void batchInsert(List<MyEntity> list) throws Exception {
if(list != null && list.size() > 0) {
SqlSessionFactory sqlSessionFactory = mybatisPlusConfig.sqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
MyEntityMapper mapper = sqlSession.getMapper(MyEntityMapper.class);
for(MyEntity entity : list) {
mapper.insert(entity);
}
sqlSession.commit();
} catch(Exception e) {
sqlSession.rollback();
throw e;
} finally {
sqlSession.close();
}
}
}
```
希望对您有帮助。