springboot执行批量插入_Springboot 批量插入优化

本文介绍了SpringBoot中使用MyBatis-Plus进行批量插入的默认实现,并分享了一个优化方案,通过调整批量插入的大小和控制flushStatements时机,提高批量插入的效率。详细探讨了DefaultSqlSession和SimpleExecutor在批量操作中的作用,以及如何通过修改源码实现性能提升。

//com.baomidou.mybatisplus.extension.service.impl ServiceImpl

/**

* 批量插入

*

* @param entityList

* @param batchSize

* @return

*/

@Transactional(rollbackFor = Exception.class)

@Override

public boolean saveBatch(Collection entityList, int batchSize) {

int i = 0;

String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);

try (SqlSession batchSqlSession = sqlSessionBatch()) {

for (T anEntityList : entityList) {

batchSqlSession.insert(sqlStatement, anEntityList); //每条数据都会执行,

if (i >= 1 && i % batchSize == 0) {

batchSqlSession.flushStatements();

}

i++;

}

batchSqlSession.flushStatements(); //预写入

}

return true;

}

//org.apache.ibatis.session.defaults ; DefaultSqlSession

@Override

public int insert(String statement, Object parameter) {

return update(statement, parameter);

}

@Override

public int update(String statement, Object parameter) {

try {

dirty = true;

MappedStatement ms = configuration.getMappedStatement(statement);

return executor.update(ms, wrapCollection(parameter));

} catch (Exception e) {

throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e);

} finally {

ErrorContext.instance().reset();

}

}

//优化前 mybatisplus saveBatch(...) 方法

//org.apache.ibatis.executor; BatchExecutor

@Override

public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {

final Configuration configuration = ms.getConfiguration();

final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);

final BoundSql boundSql = handler.getBoundSql();

final String sql = boundSql.getSql();

final Statement stmt;

if (sql.equals(currentSql) && ms.equals(currentStatement)) {

int last = statementList.size() - 1;

stmt = statementList.get(last);

applyTransactionTimeout(stmt);

handler.parameterize(stmt);//fix Issues 322

BatchResult batchResult = batchResultList.get(last);

batchResult.addParameterObject(parameterObject);

} else {

Connection connection = getConnection(ms.getStatementLog());

stmt = handler.prepare(connection, transaction.getTimeout());

handler.parameterize(stmt); //fix Issues 322

currentSql = sql;

currentStatement = ms;

statementList.add(stmt);

batchResultList.add(new BatchResult(ms, sql, parameterObject));

}

// handler.parameterize(stmt);

handler.batch(stmt);

return BATCH_UPDATE_RETURN_VALUE;

}

//优化后

// org.apache.ibatis.executor; SimpleExecutor

@Override

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {

Statement stmt = null;

try {

Configuration configuration = ms.getConfiguration();

StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);

stmt = prepareStatement(handler, ms.getStatementLog());

return handler.update(stmt);

} finally {

closeStatement(stmt);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值