Five Common Batch Insert Techniques for MyBatis and MyBatis‑Plus
This article presents and compares five practical batch‑insert methods for MyBatis and MyBatis‑Plus, covering preparation steps, code implementations, configuration details, and performance results, helping developers choose the most efficient approach for inserting large volumes of data in Java backend projects.
Introduction : The article enumerates five frequently used batch‑insert methods for MyBatis and MyBatis‑Plus, providing clear code examples, configuration guidance, and performance measurements.
Preparation : Add Maven dependencies for MySQL, MyBatis‑Spring‑Boot‑Starter, MyBatis‑Plus‑Boot‑Starter, and Lombok; configure application.yml with datasource and MyBatis mapper locations; define a simple User POJO with id, username, and password fields.
1. MyBatis For‑Loop Batch Insert : In UserService, loop 10,000 times, create User objects, and call userMapper.insertUsers(user). The mapper interface declares Integer insertUsers(User user) and the XML mapper defines a single INSERT statement. Unit test shows a total time of about 26348ms.
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public void InsertUsers() {
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("password" + i);
userMapper.insertUsers(user);
}
long end = System.currentTimeMillis();
System.out.println("一万条数据总耗时:" + (end - start) + "ms");
}
}2. MyBatis Manual Batch Commit : Use SqlSessionTemplate to open a batch session with auto‑commit disabled, perform the same loop, then call sqlSession.commit(). Execution time improves to around 24516ms.
@Service
public class UserService {
@Resource
private UserMapper userMapper;
@Resource
private SqlSessionTemplate sqlSessionTemplate;
public void InsertUsers() {
SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory()
.openSession(ExecutorType.BATCH, false);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("password" + i);
mapper.insertUsers(user);
}
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("一万条数据总耗时:" + (end - start) + "ms");
}
}3. MyBatis Collection‑Based Batch Insert (Recommended) : Build a List<User> of 10,000 objects and pass it to a mapper method insertUsers(List<User> userList). The XML uses a <foreach> to generate a bulk INSERT. This approach reduces time dramatically to about 521ms.
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public void InsertUsers() {
long start = System.currentTimeMillis();
List<User> userList = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("password" + i);
userList.add(user);
}
userMapper.insertUsers(userList);
long end = System.currentTimeMillis();
System.out.println("一万条数据总耗时:" + (end - start) + "ms");
}
}4. MyBatis‑Plus SaveBatch Method : Extend ServiceImpl and call saveBatch(userList) after preparing the list. Execution time is similar to the manual collection method, around 24674ms in the provided test.
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IService<User> {
public void InsertUsers() {
long start = System.currentTimeMillis();
List<User> userList = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("password" + i);
userList.add(user);
}
saveBatch(userList);
long end = System.currentTimeMillis();
System.out.println("一万条数据总耗时:" + (end - start) + "ms");
}
}5. MyBatis‑Plus InsertBatchSomeColumn Method (Recommended) : Create a custom EasySqlInjector that adds InsertBatchSomeColumn to the method list, register it as a bean, and use the generated insertBatchSomeColumn method. This yields the best performance, about 575ms.
public class EasySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
return methodList;
}
}
@Configuration
public class MybatisPlusConfig {
@Bean
public EasySqlInjector sqlInjector() {
return new EasySqlInjector();
}
}
public class UserService {
@Resource
private UserMapper userMapper;
public void InsertUsers() {
long start = System.currentTimeMillis();
List<User> userList = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("password" + i);
userList.add(user);
}
userMapper.insertBatchSomeColumn(userList);
long end = System.currentTimeMillis();
System.out.println("一万条数据总耗时:" + (end - start) + "ms");
}
}Conclusion : The article summarizes the batch‑insert options, showing that collection‑based inserts (MyBatis <foreach>), MyBatis‑Plus saveBatch, and the custom InsertBatchSomeColumn method provide the best performance for large data sets.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
