Extending MyBatis-Plus for Batch Insert Operations in Java Backend
This article introduces MyBatis-Plus, explains its non‑intrusive features, demonstrates how to add Maven dependencies, examines the built‑in batch insert API, and provides step‑by‑step code to extend BaseMapper with a custom EasyBaseMapper for efficient MySQL batch inserts.
MyBatis-Plus (MP) is an enhancement tool for MyBatis that adds features without altering the core, aiming to simplify development and improve efficiency.
Key features include non‑intrusive design, minimal performance overhead, powerful CRUD operations with a generic mapper and service, Lambda query support, multiple primary‑key strategies, ActiveRecord mode, custom method injection, a code generator, a pagination plugin supporting many databases, performance analysis, and interceptors.
Component Dependency
First, add the MyBatis‑Plus starter and extension dependencies to your Maven 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 original API provides a batch insert method that essentially loops over records, which can be observed via Postman logs.
Typical MyBatis XML for batch insertion:
INSERT INTO test (a, b, c) VALUES
<foreach collection="list" item="item" separator=",">
(#{item.a}, #{item.b}, #{item.c})
</foreach>Extension Code
Define a custom mapper interface extending BaseMapper to add a batch insert method:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;
/**
* Extension of the common Mapper to support batch insert.
*/
public interface EasyBaseMapper<T> extends BaseMapper<T> {
/**
* Batch insert (MySQL only).
*
* @param entityList list of entities
* @return affected rows
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}Configure the bean in a @Configuration class:
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
public EasySqlInjector easySqlInjector() {
return new EasySqlInjector();
}
}Define the mapper and service implementation that use the custom method:
@Mapper
public interface TestMapper extends EasyBaseMapper<Test> {}
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Override
public Integer testBatch(Collection<Test> testList) {
return baseMapper.insertBatchSomeColumn(testList);
}
}Since BaseMapper does not expose this method directly, the custom interface is required, especially for MySQL.
The article concludes by encouraging readers to share the content and join the architecture community for further learning.
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 Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
