Why MyBatis-Flex Outperforms MyBatis-Plus: Features, Benchmarks, and Quick Start
MyBatis-Flex is a lightweight, high‑performance MyBatis enhancement offering flexible query building, extensive database support, and features like multi‑table joins, logical deletion, and data auditing; the article compares it with MyBatis‑Plus, presents benchmark results, lists supported databases, and provides a step‑by‑step Spring Boot integration guide.
1. What is MyBatis-Flex?
MyBatis-Flex is an elegant MyBatis enhancement framework that is lightweight, highly performant, and flexible. It allows easy connection to any database and its built‑in QueryWrapper greatly reduces the amount of SQL you need to write and the chance of errors.
In short, MyBatis‑Flex can significantly improve development efficiency and experience.
2. Key Features of MyBatis-Flex
1. Lightweight: No third‑party dependencies or interceptors; implemented via SqlProvider with no SQL parsing at runtime, resulting in high performance, easy debugging, and better control.
2. Flexible: Supports CRUD and pagination on entities; provides Db + Row tools for operations without entity classes; built‑in QueryWrapper enables multi‑table, join, and sub‑query scenarios.
3. Powerful: Supports any relational database, extensible dialects, composite primary keys, logical deletion, optimistic locking, data masking, auditing, data filling, and more.
3. Comparison with Similar Frameworks
3.1 Feature Comparison
Feature
MyBatis-Flex
MyBatis-Plus
Fluent-MyBatis
Basic CRUD on entity
✅
✅
✅
Pagination query
✅
✅
✅
Pagination total cache
✅
✅
❌
Pagination without SQL parsing
✅
❌
✅
Multi‑table query (multiple tables)
✅
❌
❌
Multi‑table join (left, inner, etc.)
✅
❌
✅
Union / Union All
✅
❌
✅
Single primary key
✅
✅
✅
Multiple ID generation strategies
✅
✅
✅
Composite primary key support
✅
❌
❌
Field typeHandler configuration
✅
✅
✅
No third‑party dependencies
✅
❌
❌
QueryWrapper RPC transmission support
✅
❌
Unknown
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‑data source support
✅
Requires other frameworks or paid
❌
Spring transaction management support
✅
❌
❌
Non‑Spring project support
✅
❌
❌
Multi‑tenant
✅
✅
❌
Dynamic table name
✅
✅
❌
Dynamic schema
✅
❌
❌
3.2 Performance Comparison
Single‑record query speed of MyBatis‑Flex is about 5‑10× that of 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
4. Supported Databases
MyBatis‑Flex supports the following databases and can be extended with custom dialects.
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
SQLServer2005 database
sqlserver
SQLServer 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
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
5. 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
Tip: Use Spring Initializr to quickly generate 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>
<!-- for test only -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>Step 3: Configure Spring Boot
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678Add @MapperScan("com.mybatisflex.test.mapper") to the main application class.
@SpringBootApplication
@MapperScan("com.mybatisflex.test.mapper")
public class MybatisFlexTestApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisFlexTestApplication.class, args);
}
}Step 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: Use the Framework
@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 example:
Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)Overall, MyBatis‑Flex combines the advantages of MyBatis‑Plus, jOOQ, and Fluent‑MyBatis, providing a powerful and flexible ORM solution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
