Why MyBatis-Flex Outperforms MyBatis-Plus: Speed, Flexibility, and Advanced Features

MyBatis-Flex is a lightweight, high‑performance MyBatis enhancement offering built‑in query wrappers, flexible relation mapping, multi‑datasource support, data masking, caching, and audit capabilities, delivering 5‑10× faster queries than MyBatis‑Plus while eliminating third‑party dependencies for more stable, flexible backend development.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Why MyBatis-Flex Outperforms MyBatis-Plus: Speed, Flexibility, and Advanced Features

MyBatis-Flex is an elegant MyBatis enhancement framework that is extremely lightweight, highly performant, and very flexible, allowing developers to easily connect to any database without third‑party dependencies.

Features

Lightweight

MyBatis-Flex has no external dependencies beyond MyBatis itself, providing greater autonomy, controllability, and stability compared to frameworks that rely on many third‑party libraries.

More Flexible

It offers a powerful QueryWrapper that supports association queries, multi‑table queries, multiple primary keys, logical deletion, optimistic locking, data filling, data desensitization, and many other features.

Higher Performance

Through a unique architecture without any MyBatis interceptor or SQL parsing during execution, MyBatis-Flex can achieve exponential performance gains.

Single‑record queries are roughly 5‑10 times faster than MyBatis‑Plus.

Batch queries of 10 records are also about 5‑10 times faster.

Pagination queries and data updates enjoy similar speed improvements.

Code Practice

Add the following Maven dependencies to use MyBatis-Flex:

<dependency>
    <groupId>com.mybatis-flex</groupId>
    <artifactId>mybatis-flex-spring-boot-starter</artifactId>
    <version>1.5.3</version>
</dependency>
<dependency>
    <groupId>com.mybatis-flex</groupId>
    <artifactId>mybatis-flex-processor</artifactId>
    <version>1.5.3</version>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.31</version>
</dependency>
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

Relation Queries

One‑to‑One

Define entities with @RelationOneToOne to map a one‑to‑one relationship:

@Data
public class Account implements Serializable {
    @Id(keyType = KeyType.Auto)
    private Long id;
    private String userName;
    @RelationOneToOne(selfField = "id", targetField = "accountId")
    private IDCard idCard;
}

@Data
@Table(value = "tb_idcard")
public class IDCard implements Serializable {
    private Long accountId;
    private String cardNo;
    private String content;
}

One‑to‑Many

Use @RelationOneToMany for a one‑to‑many association:

@Data
public class Account implements Serializable {
    @Id(keyType = KeyType.Auto)
    private Long id;
    private String userName;
    @RelationOneToMany(selfField = "id", targetField = "accountId")
    private List<Book> books;
}

@Data
@Table(value = "tb_book")
public class Book implements Serializable {
    @Id(keyType = KeyType.Auto)
    private Long id;
    private Long accountId;
    private String title;
}

Many‑to‑One

Map the inverse relationship with @RelationManyToOne:

@RelationManyToOne(selfField = "accountId", targetField = "id")
private Account account;

Many‑to‑Many

Define a many‑to‑many relationship through a join table using @RelationManyToMany:

@RelationManyToMany(
    joinTable = "tb_role_mapping",
    selfField = "id", joinSelfColumn = "account_id",
    targetField = "id", joinTargetColumn = "role_id"
)
private List<Role> roles;

Parent‑Child (Recursive) Query

Configure recursive depth (default 3) with RelationManager.setMaxDepth(10) for deeper hierarchy queries.

Chain Operations

MyBatis-Flex provides QueryChain, UpdateChain, and DbChain for fluent CRUD operations. Example:

List<Article> articles = articleService.queryChain()
    .select(ARTICLE.ALL_COLUMNS)
    .from(ARTICLE)
    .where(ARTICLE.ID.ge(100))
    .list();

Data Masking

Use @ColumnMask with built‑in masking rules (e.g., Masks.CHINESE_NAME) to protect sensitive fields such as ID numbers, phone numbers, and bank cards.

Phone number masking

Fixed‑line masking

ID card masking

License‑plate masking

Address masking

Email masking

Password masking

Bank‑card masking

Custom masks via MaskManager.registerMaskProcessor Temporarily disable masking with MaskManager.skipMask() and restore it with MaskManager.restoreMask() when raw data is required.

Data Caching

While MyBatis provides a second‑level cache, MyBatis-Flex recommends using Spring Cache for distributed environments. Example service methods are annotated with @Cacheable, @CacheEvict, and @CacheConfig to manage cache entries.

SQL Audit

Enable audit logging with AuditManager.setAuditEnable(true). Implement MessageFactory to customize audit messages and register a MessageReporter (e.g., console, HTTP, Kafka) to deliver the logs.

Multi‑Datasource Support

Configure multiple data sources in application.yml under mybatis-flex.datasource. Choose a data source via:

Programmatic call: DataSourceKey.use("ds2") Annotation on mapper class or method: @UseDataSource("ds2") Annotation on entity: @Table(dataSource="ds2") Priority order: DataSourceKey.use() > method‑level @UseDataSource > class‑level @UseDataSource > entity @Table(dataSource="…").

图片
图片
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.

BackendJavaORMMyBatis-Flex
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.