Boost Java Persistence with MyBatis-Flex: Features, Comparison, and Quick Start

This article introduces MyBatis-Flex, a lightweight yet powerful MyBatis enhancement framework, outlines its key characteristics, compares it with MyBatis-Plus and Fluent-MyBatis, presents performance benchmarks, lists supported databases, and provides a step‑by‑step quick‑start guide for Spring Boot integration.

macrozheng
macrozheng
macrozheng
Boost Java Persistence with MyBatis-Flex: Features, Comparison, and Quick Start

MyBatis-Flex is a lightweight, high‑performance enhancement framework for MyBatis that provides a type‑safe QueryWrapper, full compatibility with native MyBatis, and many enterprise‑grade features without adding third‑party dependencies.

Key Characteristics

Lightweight – no third‑party dependencies, no interceptors, and no SQL parsing at runtime.

Flexible – supports multi‑primary keys, multi‑table queries, logical delete, optimistic lock, data masking, encryption, multi‑datasource, sharding, field permissions, multi‑tenant, transaction management, SQL audit, and more.

High performance – CRUD operations are typically 5‑10× faster than competing frameworks.

Only enhancement – adds CRUD, pagination, batch operations, etc., while preserving every native MyBatis feature.

Feature Comparison with MyBatis‑Plus and Fluent‑MyBatis

Feature

MyBatis‑Flex

MyBatis‑Plus

Fluent‑MyBatis

Basic CRUD

Pagination query

Pagination total cache

Pagination without SQL parsing

Multi‑table query (FROM)

Multi‑table join (LEFT/INNER)

UNION / UNION ALL

Single primary‑key config

Multiple ID generation strategies

Support composite primary keys

Field typeHandler config

No third‑party dependencies

QueryWrapper RPC support

未知

Logical delete

Optimistic lock

SQL audit

Data fill

✔️(收费)

Data masking

✔️(收费)

Field permission

✔️(收费)

Field encryption

✔️(收费)

Dictionary rewrite

✔️(收费)

Db + Row

Entity listener

Multi‑datasource support

借助其他框架或收费

Spring transaction management

Non‑Spring project datasource

Multi‑tenant

Dynamic table name

Dynamic schema

Performance Benchmarks

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

Querying 10 records is about 5‑10× faster.

Pagination query speed is about 5‑10× faster.

Data update speed is about 5‑10× faster.

Supported Databases

Database

Description

mysql

MySQL database

mariadb

MariaDB database

oracle

Oracle 11g and below

oracle12c

Oracle 12c and above

db2

DB2 database

hsql

HSQL database

sqlite

SQLite database

postgresql

PostgreSQL database

sqlserver2005

SQL Server 2005 database

sqlserver

SQL Server database

dm

DaMeng database

xugu

XuGu database

kingbasees

KingbaseES database

phoenix

Phoenix HBase database

gauss

Gauss database

clickhouse

ClickHouse database

gbase

GBase (Nanda General) database

gbase-8s

GBase 8s database

oscar

ShenTong database

sybase

Sybase ASE database

OceanBase

OceanBase database

Firebird

Firebird database

derby

Derby database

highgo

HighGo database

cubrid

CUBRID database

goldilocks

Goldilocks database

csiidb

CSIIDB database

hana

SAP HANA database

impala

Impala database

vertica

Vertica database

xcloud

XCloud database

redshift

Amazon Redshift database

openGauss

Huawei openGauss database

TDengine

TDengine database

informix

Informix database

greenplum

Greenplum database

uxdb

UXDB database

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: 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

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

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

Step 6: Test Usage

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

Conclusion

MyBatis-Flex delivers extreme lightweight design, powerful functionality, and unmatched performance while staying fully compatible with native MyBatis, making it an excellent choice for developers seeking a modern, efficient, and developer‑friendly persistence solution.

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.

performanceBackend DevelopmentSpring BootORMMyBatis-Flex
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.