Why MyBatis‑Flex Beats 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 tutorial with code examples.
Core Characteristics of MyBatis‑Flex
MyBatis‑Flex is a lightweight enhancement of MyBatis that eliminates third‑party dependencies and runtime SQL parsing by using SqlProvider. This design yields higher execution speed, easier tracing and debugging, and tighter control over generated SQL.
Lightweight design
All SQL is generated at compile time; no interceptor chain or SQL parsing occurs at runtime.
Flexibility
CRUD and pagination can be performed directly on entity classes. The Db and Row utilities also allow operations without entity definitions. QueryWrapper simplifies multi‑table joins, sub‑queries and other complex SQL patterns.
Feature richness
Supports any relational database and can be extended with custom dialects. Built‑in capabilities include composite primary keys, logical deletion, optimistic locking, data masking, audit logging, automatic field filling, multi‑datasource handling with Spring transaction management, dynamic table names, dynamic schemas and multi‑tenant isolation.
Feature‑by‑Feature Comparison
Basic CRUD on entities – supported by MyBatis‑Flex, MyBatis‑Plus and Fluent‑MyBatis.
Pagination – supported by all three, but only MyBatis‑Flex provides cache‑aware pagination.
SQL‑free pagination (no runtime parsing) – MyBatis‑Flex ✅, MyBatis‑Plus ❌, Fluent‑MyBatis ✅.
Multi‑table queries (joins, unions) – MyBatis‑Flex ✅, MyBatis‑Plus ❌, Fluent‑MyBatis ✅.
Composite primary keys – MyBatis‑Flex ✅, others ❌.
Field typeHandler configuration – supported by all.
Zero third‑party dependencies – MyBatis‑Flex ✅, others ❌.
QueryWrapper RPC support (micro‑service serialization) – MyBatis‑Flex ✅, MyBatis‑Plus ❌, Fluent‑MyBatis status unknown.
Logical deletion, optimistic lock, SQL audit – MyBatis‑Flex ✅, MyBatis‑Plus ✅, Fluent‑MyBatis ❌.
Data filling, masking, encryption, field permissions – MyBatis‑Flex ✅ (some features require a paid license), MyBatis‑Plus limited, Fluent‑MyBatis ❌.
Multi‑datasource with Spring transaction management – MyBatis‑Flex ✅, MyBatis‑Plus ❌, Fluent‑MyBatis ❌.
Multi‑tenant, dynamic table name, dynamic schema – supported only by MyBatis‑Flex.
Performance Comparison
Benchmark tests (refer to the official benchmark page) show a consistent 5‑10× speed advantage for MyBatis‑Flex over MyBatis‑Plus across the following scenarios:
Single‑record query.
Batch query of 10 records.
Pagination query.
Data‑update operation.
Exact benchmark results are published at: https://mybatis-flex.com/zh/intro/benchmark.html
Supported Databases
MyBatis‑Flex works out‑of‑the‑box with a wide range of databases. Additional databases can be added by implementing custom dialects.
MySQL / MariaDB
Oracle 11g and 12c
DB2
HSQL
SQLite
PostgreSQL
SQL Server (2005 and later)
DM, Xugu, KingbaseES, Phoenix, Gauss, ClickHouse, GBase, GBase‑8s, Oscar, Sybase ASE, OceanBase, Firebird, Derby, HighGo, CUBRID, Goldilocks, CSIIDB, SAP HANA, Impala, Vertica, Redshift, OpenGauss, TDengine, Informix, Greenplum, UXDB
Quick‑Start Guide
Step 1 – Create the 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 to a Spring Boot project
<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 (application.yml)
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678Step 4 – 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<Account> { }The static ACCOUNT reference used in queries is generated automatically by MyBatis‑Flex’s annotation processor, eliminating manual mapping code.
Step 5 – Use the mapper in a test
@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);
}
}Console output demonstrates successful retrieval:
Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)Conclusion
MyBatis‑Flex combines the strengths of MyBatis‑Plus, jOOQ and Fluent‑MyBatis while delivering superior performance through a compile‑time, zero‑dependency architecture. It is well‑suited for Java projects that require high‑throughput ORM capabilities, extensive multi‑table query support, and advanced features such as logical deletion, optimistic locking and multi‑tenant isolation.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
