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

This article introduces MyBatis-Flex, a lightweight yet powerful MyBatis enhancement, compares its features and performance against MyBatis-Plus and Fluent-MyBatis, lists supported databases, and provides a step‑by‑step guide to integrate it into a Spring Boot project.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Why MyBatis-Flex Beats MyBatis-Plus: Features, Benchmarks, and Quick Start

What Is MyBatis-Flex?

MyBatis-Flex is an elegant MyBatis enhancement framework that is extremely lightweight, highly performant, and flexible. It allows seamless connection to any database and its built‑in QueryWrapper dramatically reduces the amount of SQL you need to write while minimizing errors.

Overall, MyBatis-Flex can greatly improve development efficiency and experience, giving developers more time to focus on business logic.

Key Characteristics of MyBatis-Flex

1. Lightweight : Apart from MyBatis, there are no third‑party dependencies, no interceptors, and it operates without SQL parsing, resulting in high performance, easy debugging, and strong control.

2. Flexible : Supports CRUD operations and pagination on entities, and provides Db+Row tools for operations without entity classes. The built‑in QueryWrapper simplifies multi‑table joins, sub‑queries, and other common SQL scenarios.

3. Powerful : Supports any relational database with extensible dialects, and offers features such as composite primary keys, logical deletion, optimistic locking, data masking, and data auditing.

Comparison with Similar Frameworks

1) Feature Comparison

MyBatis-Flex matches MyBatis‑Plus and Fluent‑MyBatis on basic CRUD, pagination, and multi‑primary‑key support, while offering additional capabilities like pagination cache, SQL‑less pagination, and RPC‑compatible QueryWrapper.

2) Performance Comparison

Single‑record query speed is about 5‑10× faster than MyBatis‑Plus.

Querying 10 records is roughly 5‑10× faster.

Pagination queries are about 5‑10× faster.

Data update speed is also 5‑10× faster.

Detailed benchmark results can be found at the official benchmark page.

Supported Databases

MyBatis-Flex supports a wide range of databases, including MySQL, MariaDB, Oracle (11g and 12c), DB2, HSQL, SQLite, PostgreSQL, SQLServer, ClickHouse, OceanBase, and many others. Additional databases can be added via custom dialects.

Quick Start Guide

Step 1: Create Database 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: Create Spring Boot Project and 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>
    <!-- for test only -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Step 3: Configure DataSource in application.yml

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

Step 4: Write 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> {}

Use @Table("tb_account") to map the entity to the table and @Id(keyType = KeyType.Auto) for auto‑increment primary keys.

Step 5: Use the Mapper

@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 console will output the retrieved record, demonstrating the simplicity of queries with MyBatis‑Flex.

Overall, MyBatis‑Flex combines the strengths of MyBatis‑Plus, jOOQ, and Fluent‑MyBatis, offering a comprehensive solution for Java backend development. For more details, visit the official website.

Official site: https://mybatis-flex.com

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.

Performancespring-boot
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

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.