Why MyBatis-Flex Outperforms MyBatis-Plus: Features, Benchmarks, and Quick Start Guide

This article analyzes MyBatis-Flex, highlighting its lightweight, flexible, and powerful design, compares its feature set and performance against MyBatis-Plus and Fluent-MyBatis, lists supported databases, and provides a step‑by‑step quick‑start guide with code examples for Spring Boot integration.

Java Web Project
Java Web Project
Java Web Project
Why MyBatis-Flex Outperforms MyBatis-Plus: Features, Benchmarks, and Quick Start Guide

Core Characteristics

MyBatis-Flex is a lightweight extension of MyBatis that adds no third‑party dependencies and avoids runtime SQL parsing. This design yields high performance, easy debugging, and fine‑grained control over generated SQL.

Key capabilities include:

CRUD and pagination directly on entity classes.

Db + Row API for operations without entity definitions.

QueryWrapper for building multi‑table joins, sub‑queries, and complex conditions without manual SQL.

Support for any relational database with extensible dialects, composite primary keys, logical deletion, optimistic locking, data masking, audit logging, and automatic data filling.

Feature Comparison with Peer Frameworks

The following matrix highlights functional differences among MyBatis-Flex, MyBatis‑Plus and Fluent‑MyBatis (✓ = supported, ✗ = not supported, ✔ = supported as a paid add‑on in the other frameworks):

Feature                     MyBatis-Flex   MyBatis-Plus   Fluent-MyBatis
-------------------------------------------------------------------
Basic CRUD on entity        ✓              ✓               ✓
Pagination                  ✓              ✓               ✓
Total‑count caching for pagination ✓      ✓               ✗
SQL‑parsing‑free pagination  ✓              ✗               ✓
Multi‑table FROM clause      ✓              ✗               ✓
LEFT/INNER JOIN support     ✓              ✗               ✓
UNION / UNION ALL support    ✓              ✗               ✓
Single‑column primary key   ✓              ✓               ✓
Composite primary key        ✓              ✗               ✗
Custom TypeHandler           ✓              ✓               ✓
Zero third‑party deps        ✓              ✗               ✗
RPC‑compatible QueryWrapper  ✓              ✗               ?
Logical deletion             ✓              ✓               ✓
Optimistic lock              ✓              ✓               ✓
SQL audit                   ✓              ✗               ✗
Data filling                ✓              ✔ (paid)        ✓
Data masking                ✓              ✔ (paid)        ✗
Field encryption            ✓              ✔ (paid)        ✗
Dictionary write‑back       ✓              ✔ (paid)        ✗
Db + Row API                ✓              ✗               ✗
Entity listener             ✓              ✗               ✗
Multi‑datasource support    ✓              ✗               ✗
Spring @Transactional support ✓           ✗               ✗
Non‑Spring datasource support ✓           ✗               ✗
Multi‑tenant                ✓              ✓               ✗
Dynamic table name          ✓              ✓               ✗
Dynamic schema              ✓              ✗               ✗

Performance Benchmarks

Independent tests show MyBatis‑Flex delivering roughly a 5–10× speed advantage over MyBatis‑Plus for the following operations:

Single‑record query.

Batch query of ten records.

Paginated query.

Data update.

Full benchmark details are published at the following URL (plain text, no hyperlink): https://mybatis-flex.com/zh/intro/benchmark.html

Supported Databases

Out‑of‑the‑box support includes MySQL, MariaDB, Oracle 11g/12c, DB2, HSQL, SQLite, PostgreSQL, SQLServer (2005+), ClickHouse, Gauss, OceanBase, TDengine, and many others. Additional databases can be added by implementing a custom dialect.

Quick‑Start Walk‑through

Step 1 – Create a Table

CREATE TABLE IF NOT EXISTS `tb_account` (
  `id` INTEGER PRIMARY KEY AUTO_INCREMENT,
  `user_name` VARCHAR(100),
  `age` INTEGER,
  `birthday` DATETIME
);

INSERT INTO `tb_account` (id, user_name, age, birthday) VALUES
  (1, '张三', 18, '2020-01-11'),
  (2, '李四', 19, '2021-03-21');

Step 2 – Add Maven Dependencies

<dependencies>
  <dependency>
    <groupId>com.mybatis-flex</groupId>
    <artifactId>mybatis-flex-spring-boot-starter</artifactId>
    <version>1.5.3</version>
  </dependency>
  <dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
  </dependency>
  <!-- test only -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

Step 3 – Configure the DataSource

# DataSource Config
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/flex_test
    username: root
    password: 12345678

Step 4 – Define Entity and Mapper

@Data
@Table("tb_account")
public class Account {
    @Id(keyType = KeyType.Auto)
    private Long id;
    private String userName;
    private Integer age;
    private Date birthday;
}

public interface AccountMapper extends BaseMapper<Account> {}

The @Table annotation binds the class to tb_account. The @Id(keyType = KeyType.Auto) marks the primary key as auto‑increment.

Step 5 – Write a Test Case

@SpringBootTest
class MybatisFlexTestApplicationTests {
    @Autowired
    private AccountMapper accountMapper;

    @Test
    void contextLoads() {
        QueryWrapper queryWrapper = QueryWrapper.create()
            .select()
            .where(ACCOUNT.AGE.eq(18));
        Account account = accountMapper.selectOneByQuery(queryWrapper);
        System.out.println(account);
    }
}

The static import ACCOUNT is generated automatically by MyBatis‑Flex’s annotation processor, eliminating manual mapping code. Running the test prints:

Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)

Code Generation

MyBatis‑Flex provides a code‑generator that can produce entity classes, mapper interfaces, and the corresponding table definition classes (e.g., ACCOUNT) from an existing schema. Documentation is available at:

https://mybatis-flex.com/zh/others/codegen.html

Conclusion

MyBatis‑Flex combines the ergonomics of MyBatis‑Plus, the type‑safe query construction of jOOQ, and the flexibility of Fluent‑MyBatis while remaining lighter and faster. Its extensive feature set, benchmark‑proven performance, and straightforward integration steps make it a strong candidate for Java backend projects that require high‑throughput data access with minimal boilerplate.

JavaPerformancedatabaseSpring BootORMMyBatis-Flex
Java Web Project
Written by

Java Web Project

Focused on Java backend technologies, trending internet tech, and the latest industry developments. The platform serves over 200,000 Java developers, inviting you to learn and exchange ideas together. Check the menu for Java learning resources.

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.