Extending MyBatis-Plus for Efficient Batch Insert Operations in Spring Boot

This article explains how to enhance MyBatis-Plus by adding Maven dependencies, creating custom injector and mapper interfaces, and implementing a true batch‑insert method for MySQL, enabling developers to persist collections of entities efficiently in a Spring Boot backend.

Top Architect
Top Architect
Top Architect
Extending MyBatis-Plus for Efficient Batch Insert Operations in Spring Boot

MyBatis-Plus (MP) is an enhancement tool for MyBatis that provides non‑intrusive features to simplify development.

To use MP, add the dependencies mybatis-plus-boot-starter and mybatis-plus-extension (version 3.4.0) to the Maven pom.xml:

<!--mybatis-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<!--mybatis plus extension,包含了mybatis plus core-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.4.0</version>
</dependency>

The built‑in batch‑insert API actually loops over records, so the article shows how to extend MP to perform true batch inserts.

First, a custom EasySqlInjector bean is defined in a @Configuration class together with the pagination interceptor:

@Configuration
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
    @Bean
    public EasySqlInjector easySqlInjector() {
        return new EasySqlInjector();
    }
}

Then an EasyBaseMapper interface extends BaseMapper and declares a MySQL‑only batch‑insert method:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;
/**
 * 扩展通用 Mapper,支持数据批量插入
 */
public interface EasyBaseMapper<T> extends BaseMapper<T> {
    /**
     * 批量插入 仅适用于mysql
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}

A concrete mapper TestMapper extends this interface, and a service implementation calls the new method to persist a collection of entities:

@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);
    }
}

After adding the configuration and mapper, the batch‑insert functionality works without manual loops, as demonstrated with Postman testing.

The article concludes with a reminder to like and share the post.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring BootORMmybatis-plusBatch Insert
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.