Fast Testing of MyBatis SQL Without Starting Spring

This article explains how to quickly test MyBatis SQL statements by bypassing the Spring container, using a minimal MyBatis configuration, adding support for PageHelper pagination and MyBatis‑Plus plugins, and provides code examples and a plugin recommendation for efficient backend development.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Fast Testing of MyBatis SQL Without Starting Spring

When you finish writing SQL you need to verify that it works as expected, especially for complex queries, but starting the Spring container can be slow, sometimes taking over 30 seconds.

For isolated SQL testing you can start MyBatis directly with a simple configuration file, avoiding Spring entirely; this dramatically speeds up test execution to about 1–2 seconds.

To add PageHelper pagination support, modify the setUp method to register a PageInterceptor and obtain the mapper from the built SqlSessionFactory:

SqlSessionFactory builder = new SqlSessionFactoryBuilder().build(UserMapperTest.class.getClassLoader().getResourceAsStream("mybatisTestConfiguration/UserMapperTestConfiguration.xml"));
// you can use builder.openSession(false) to not commit to database
PageInterceptor interceptor = new PageInterceptor();
builder.getConfiguration().addInterceptor(interceptor);
mapper = builder.getConfiguration().getMapper(UserMapper.class, builder.openSession(true));

To test MyBatis‑Plus features, replace SqlSessionFactoryBuilder with MybatisSqlSessionFactoryBuilder in the setup method.

For MyBatis‑Plus pagination and optimistic locking, add the corresponding interceptors in a setUpMybatisDatabase method:

@org.junit.BeforeClass
public static void setUpMybatisDatabase() {
    SqlSessionFactory builder = new MybatisSqlSessionFactoryBuilder().build(SymphonyClientMapperTest.class.getClassLoader().getResourceAsStream("mybatisTestConfiguration/SymphonyClientMapperTestConfiguration.xml"));
    final MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    builder.getConfiguration().addInterceptor(interceptor);
    // you can use builder.openSession(false) to not commit to database
    mapper = builder.getConfiguration().getMapper(SymphonyClientMapper.class, builder.openSession(true));
}

These steps allow rapid testing of MyBatis SQL without the overhead of a full Spring startup.

The article also recommends the MybatisCodeHelperPro IntelliJ plugin, which can automatically generate test Java classes and configuration files, further simplifying the testing workflow.

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.

BackendJavaperformancespringMyBatisSQL testing
Java Architect Essentials
Written by

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.

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.