How to Extend MyBatis-Plus for Efficient Batch Inserts in MySQL
This article explains MyBatis-Plus's key features, shows how to add Maven dependencies, demonstrates the built‑in batch‑insert API, and provides step‑by‑step code to extend MyBatis‑Plus with a custom mapper and service for fast MySQL batch inserts.
Features
Non‑intrusive: enhances MyBatis without affecting existing code.
Low overhead: automatic CRUD injection with negligible performance impact.
Powerful CRUD: generic mapper and service support most single‑table operations.
Lambda support: write type‑safe query conditions using Java lambdas.
Primary key generation: four strategies including distributed ID generator.
ActiveRecord mode: entities can extend Model for full CRUD.
Customizable common operations: inject methods once for reuse.
Code generator: generate Mapper, Model, Service, Controller via plugin.
Pagination plugin: transparent paging for many databases.
Performance analysis plugin: log SQL and execution time.
Intercept plugin: prevent accidental full‑table updates or deletes.
Component Dependency
Add the MyBatis‑Plus starter and extension to your pom.xml:
<!--mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!--mybatis‑plus extension, includes core-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.0</version>
</dependency>The API provides a batch‑insert method, as shown in the screenshot below:
When the service runs, Postman displays the request and the backend logs the batch operation:
The underlying implementation is essentially a looped INSERT statement:
INSERT INTO test (a, b, c) VALUES
<foreach collection="list" item="item" separator=",">
(#{item.a}, #{item.b}, #{item.c})
</foreach>Extension Code
Configure the custom bean in a MybatisPlusConfig class:
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
public EasySqlInjector easySqlInjector() {
return new EasySqlInjector();
}
}Create an extended mapper interface that adds a MySQL‑only batch insert method:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;
/**
* Extended mapper supporting batch insert.
*/
public interface EasyBaseMapper<T> extends BaseMapper<T> {
/**
* Batch insert for MySQL.
* @param entityList list of entities
* @return affected rows
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}Define a business mapper that inherits the extended base mapper:
@Mapper
public interface TestMapper extends EasyBaseMapper<Test> {
}Implement the service using the new method:
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Override
public Integer testBatch(Collection<Test> testList) {
return baseMapper.insertBatchSomeColumn(testList);
}
}Since the default BaseMapper does not expose this MySQL‑specific method, the custom interface is required. After adding these components, the batch‑insert capability works as expected.
With the extension in place, developers can efficiently insert large volumes of data into MySQL using MyBatis‑Plus.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
