An Introduction to MyBatis-Flex: Features, Comparison, Supported Databases, and Quick Start Guide
This article introduces MyBatis-Flex, a lightweight high‑performance MyBatis enhancement framework, outlines its key features, compares it with MyBatis‑Plus and Fluent‑MyBatis, lists supported databases, and provides a step‑by‑step quick‑start tutorial with code examples.
What is MyBatis-Flex?
MyBatis-Flex is a lightweight, high‑performance enhancement framework for MyBatis that simplifies database access and reduces SQL writing errors through its built‑in QueryWrapper.
Key Features
Lightweight : No third‑party dependencies, no interceptors, and no SQL parsing at runtime, resulting in high performance, easy debugging, and better controllability.
Flexible : Supports CRUD, pagination, multi‑table joins, sub‑queries, and provides Db+Row utilities for operations without entity classes.
Powerful : Supports any relational database, multiple primary keys, logical deletion, optimistic locking, data masking, auditing, data filling, and more.
Comparison with Similar Frameworks
Feature Comparison
Feature
MyBatis-Flex
MyBatis-Plus
Fluent-MyBatis
Basic CRUD on entity
✅
✅
✅
Pagination
✅
✅
✅
Pagination total cache
✅
✅
❌
Pagination without SQL parsing
✅
❌
✅
Multi‑table query (FROM multiple tables)
✅
❌
❌
Multi‑table join (LEFT, INNER, etc.)
✅
❌
✅
UNION / UNION ALL
✅
❌
✅
Single primary key
✅
✅
✅
Multiple ID generation strategies
✅
✅
✅
Composite primary key
✅
❌
❌
Field typeHandler configuration
✅
✅
✅
No third‑party dependencies
✅
❌
❌
QueryWrapper RPC support
✅
❌
未知
Logical deletion
✅
✅
✅
Optimistic lock
✅
✅
✅
SQL audit
✅
❌
❌
Data filling
✅
✔️ (paid)
✅
Data masking
✅
✔️ (paid)
❌
Field permission
✅
✔️ (paid)
❌
Field encryption
✅
✔️ (paid)
❌
Dictionary rewrite
✅
✔️ (paid)
❌
Db + Row
✅
❌
❌
Entity listener
✅
❌
❌
Multi‑datasource support
✅
Depends on other frameworks / paid
❌
Spring transaction management
✅
❌
❌
Non‑Spring project support
✅
❌
❌
Multi‑tenant
✅
✅
❌
Dynamic table name
✅
✅
❌
Dynamic schema
✅
❌
❌
Performance Comparison
Single‑record query is about 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.
Detailed benchmark: https://mybatis-flex.com/zh/intro/benchmark.html
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
Virtual G database
kingbasees
KingbaseES database
phoenix
Phoenix HBase database
gauss
Gauss database
clickhouse
ClickHouse database
gbase
GBase database
gbase-8s
GBase 8s database
oscar
Oscar 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
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
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678Step 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: Write entity class and mapper interface
@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 class to use QueryWrapper
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);
}
}Console output shows the retrieved Account object, e.g. Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020) .
Conclusion
The framework integrates the advantages of MyBatis‑Plus, jOOQ, and Fluent‑MyBatis, offering a powerful, flexible ORM solution for backend development.
For more details, visit the official site: https://mybatis-flex.com/ .
Author's Note: If this article helped you, please like, watch, share, or bookmark it. The author also runs a knowledge community (price ¥199) offering advanced projects and tutorials. Contact special_coder on WeChat for access.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.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.