建议使用批处理执行器,但是您需要正确执行。
我注意到了两个问题。
- 设置适当的批次大小很重要。该时效率不高非常多端发送的所有数据。
- 使用
${}引用参数会使每个语句唯一,并防止驱动程序重新使用该语句(基本上失去了批处理执行器的好处)。有关和之间的区别,请参见此常见问题解答。#{}``${}
这是使用MyBatis的典型批处理操作。
由于最好
batchSize取决于各种因素,因此应使用实际数据来衡量性能。
int batchSize = 1000;try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { YourMapper mapper = sqlSession.getMapper(YourMapper.class); int size = list.size(); for (int i = 0; i < size;) { mapper.update(list.get(i)); i++; if (i % batchSize == 0 || i == size) { sqlSession.flushStatements(); sqlSession.clearCache(); } } sqlSession.commit();}这是update语句的有效版本。
<update id="update"> UPDATE <include refid="tableName" /> SET item_price = #{item.price}, update_time = #{item.updateTime} WHERe id = #{item.id}</update>


