Why MyBatis-Flex Beats MyBatis-Plus: Features, Performance, and Quick Start Guide
This article introduces MyBatis-Flex, a lightweight yet powerful MyBatis enhancement framework, compares its features and performance against MyBatis-Plus and Fluent-MyBatis, lists supported databases, and provides a step‑by‑step Spring Boot quick‑start tutorial with code examples.
What is MyBatis-Flex?
MyBatis-Flex is a lightweight, high‑performance enhancement for MyBatis. It adds a fluent QueryWrapper, Db and Row API and provides advanced capabilities such as multi‑primary‑key support, logical deletion, optimistic locking, data masking, auditing, field filling, encryption, dictionary write‑back, multi‑data‑source handling and tenant isolation.
Key Characteristics
Lightweight : No third‑party dependencies or interceptors; implemented via SqlProvider without SQL parsing, which yields high performance and easy debugging.
Flexible : Supports CRUD on entity classes, pagination, and Db / Row utilities for operations without entities. QueryWrapper enables complex multi‑table, join and sub‑query constructions.
Powerful : Works with any relational database, supports composite primary keys, logical deletion, optimistic lock, data masking, auditing, field permissions, encryption, dynamic table names, dynamic schemas, multi‑data‑source and tenant isolation.
Feature Comparison (MyBatis-Flex vs MyBatis‑Plus vs Fluent‑MyBatis)
All three provide basic CRUD, pagination and total‑count caching.
MyBatis‑Flex offers pagination without SQL parsing, which MyBatis‑Plus lacks.
Multi‑table queries (joins, unions) are fully supported by MyBatis‑Flex and Fluent‑MyBatis; MyBatis‑Plus does not support them out of the box.
Logical deletion, optimistic lock and SQL auditing are built‑in for MyBatis‑Flex; MyBatis‑Plus requires paid extensions for some of these features.
Dynamic table names, dynamic schemas and multi‑tenant support are available only in MyBatis‑Flex.
Performance Benchmarks
Single‑record query: MyBatis‑Flex is approximately 5–10× faster than MyBatis‑Plus.
Querying 10 records: MyBatis‑Flex is about 5–10× faster.
Pagination query: MyBatis‑Flex outperforms MyBatis‑Plus by a factor of 5–10.
Data update: MyBatis‑Flex is 5–10× faster.
Benchmark details: https://mybatis-flex.com/zh/intro/benchmark.html
Supported Databases
MyBatis‑Flex works with a wide range of databases, including MySQL, MariaDB, Oracle (11g/12c), DB2, HSQL, SQLite, PostgreSQL, SQL Server, DM, Xugu, KingbaseES, Phoenix, Gauss, ClickHouse, GBase, GBase‑8s, Oscar, Sybase, OceanBase, Firebird, Derby, HighGo, CUBRID, Goldilocks, CSIIDB, SAP HANA, Impala, Vertica, XCloud, Amazon Redshift, OpenGauss, TDengine, Informix and Greenplum. 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 – Add Maven Dependencies (Spring Boot)
<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 Data Source (application.yml)
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678Step 4 – Define 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 5 – Bootstrap Application
@SpringBootApplication
@MapperScan("com.mybatisflex.test.mapper")
public class MybatisFlexTestApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisFlexTestApplication.class, args);
}
} @SpringBootTest
class MybatisFlexTestApplicationTests {
@Autowired
private AccountMapper accountMapper;
@Test
void contextLoads() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.where(AccountTableDef.ACCOUNT.AGE.eq(18));
Account account = accountMapper.selectOneByQuery(queryWrapper);
System.out.println(account);
}
}The constant ACCOUNT is generated by MyBatis‑Flex’s annotation processor, allowing static imports without manual coding.
Conclusion
MyBatis‑Flex combines the strengths of MyBatis‑Plus, jOOQ and Fluent‑MyBatis while remaining lightweight and highly performant, making it a strong choice for Java backend projects that require flexible, fast and feature‑rich data access.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
