Databases 11 min read

MyBatis-Flex: A Lightweight High‑Performance MyBatis Enhancement Framework – Features, Comparison, and Quick‑Start Guide

This article introduces MyBatis‑Flex, a lightweight and high‑performance MyBatis enhancement framework, outlines its key features, compares it with similar tools, presents benchmark results, lists supported databases, and provides a step‑by‑step quick‑start tutorial with complete code examples for Spring Boot integration.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
MyBatis-Flex: A Lightweight High‑Performance MyBatis Enhancement Framework – Features, Comparison, and Quick‑Start Guide

MyBatis‑Flex is an elegant MyBatis enhancement framework that is lightweight, highly performant, and flexible, allowing developers to connect to any database with minimal configuration and reducing the amount of hand‑written SQL.

The framework offers three main advantages: lightweight (no third‑party dependencies, no SQL parsing at runtime, resulting in high performance and easy debugging), flexibility (supports CRUD, pagination, multi‑table joins, union queries, and provides a powerful QueryWrapper ), and powerful features such as multi‑primary‑key support, logical deletion, optimistic locking, data auditing, data masking, field encryption, and more.

Feature Highlights

Lightweight core with only MyBatis as a dependency.

Flexible Db + Row utilities for CRUD without entity classes.

Rich QueryWrapper for complex queries, including joins and unions.

Support for any relational database and extensible dialect system.

Advanced capabilities: multi‑primary‑key, logical delete, optimistic lock, data audit, data masking, field encryption, dictionary write‑back, dynamic table name, dynamic schema, multi‑tenant, and more.

Comparison with Similar Frameworks

Feature

MyBatis‑Flex

MyBatis‑Plus

Fluent‑MyBatis

Basic CRUD

Pagination

Pagination total cache

No SQL parsing design

Multi‑table join

Union/Union All

Composite primary key

Logical delete

Optimistic lock

SQL audit

Data fill

✔️ (paid)

Data masking

✔️ (paid)

Field encryption

✔️ (paid)

Multi‑data source support

requires other frameworks or paid

Multi‑tenant

Dynamic table name

Dynamic schema

Performance Benchmark

Single‑record query: 5–10× faster than MyBatis‑Plus.

10‑record query: 5–10× faster than MyBatis‑Plus.

Pagination query: 5–10× faster than MyBatis‑Plus.

Data update: 5–10× faster than MyBatis‑Plus.

Detailed benchmark results can be found at https://mybatis-flex.com/zh/intro/benchmark.html .

Supported Databases

MyBatis‑Flex supports a wide range of databases, including MySQL, MariaDB, Oracle (11g and 12c), DB2, HSQL, SQLite, PostgreSQL, SQL Server, DM, Xugu, KingbaseES, Phoenix, Gauss, ClickHouse, GBase, GBase‑8s, Oscar, Sybase ASE, OceanBase, Firebird, Derby, HighGo, CUBRID, Goldilocks, CSIIDB, SAP HANA, Impala, Vertica, XCloud, Redshift, OpenGauss, TDengine, Informix, Greenplum, and UXDB.

Quick Start Guide

Step 1: Create database tables

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 a 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 the data source in application.yml

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

Step 4: Add @MapperScan to the Spring Boot main class

@SpringBootApplication
@MapperScan("com.mybatisflex.test.mapper")
public class MybatisFlexTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisFlexTestApplication.class, args);
    }
}

Step 5: Define the 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
{}

Step 6: Write a test case to verify functionality

import static com.mybatisflex.test.entity.table.AccountTableDef.ACCOUNT;

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

Running the test prints:

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

The ACCOUNT class is automatically generated by MyBatis‑Flex's annotation processor, allowing static imports without manual coding.

Overall, MyBatis‑Flex combines the strengths of MyBatis‑Plus, jOOQ, and Fluent‑MyBatis, offering a comprehensive, high‑performance ORM solution for Java backend development.

JavaPerformanceDatabaseSpring BootORMtutorialMyBatis-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

login 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.