Optimizing Batch Insert Performance in MyBatis‑Plus with a Custom SQL Injector
This article analyzes why MyBatis‑Plus's default saveBatch method does not achieve true batch insertion, explains how to enable JDBC batch mode with rewriteBatchedStatements, and demonstrates creating a custom SQL injector (InsertBatchSomeColumn) to perform real batch inserts, including performance test results showing significant speed improvements.
The author describes a high‑frequency IoT data ingestion scenario where MySQL tables receive millions of rows, causing Kafka back‑pressure and long batch insert times (10‑20 seconds per 1500 rows). The default saveBatch() method only loops inserts one by one, even with rewriteBatchedStatements=true enabled.
To achieve true batch insertion, the article introduces MyBatis‑Plus's SQL injector mechanism. It shows the built‑in injectable methods and highlights InsertBatchSomeColumn as the method that generates a single INSERT statement for multiple rows.
Custom injector implementation:
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List
getMethodList(Class
mapperClass) {
List
methodList = super.getMethodList(mapperClass);
// Add real batch insert method, ignoring fields filled only on UPDATE
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
return methodList;
}
}The injector is registered in Spring configuration:
@Configuration
public class MybatisPlusConfig {
@Bean
public MySqlInjector sqlInjector() {
return new MySqlInjector();
}
}A mapper interface extending a custom CommonMapper gains the new method:
public interface CommonMapper
extends BaseMapper
{
int insertBatchSomeColumn(List
entityList);
}Usage example:
@Test
void contextLoads() {
for (int i = 0; i < 5; i++) {
User user = new User();
user.setAge(10);
user.setUsername("zhmsky");
user.setEmail("[email protected]");
userList.add(user);
}
long start = System.currentTimeMillis();
userMapper.insertBatchSomeColumn(userList);
long end = System.currentTimeMillis();
System.out.println("Time:" + (end - start));
userList.clear();
}Performance tests compare the fake batch insert (original saveBatch ) with the real batch insert ( insertBatchSomeColumn ) on 50,000 records. The custom method reduces execution time by about 3 seconds and, when inserting 1,500 rows per batch, completes in roughly 650 ms.
Finally, the author adds a note encouraging readers to like, share, and follow the public account for more technical content.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.