Introduction to MyBatis-Flex: Features, Comparison, and Quick‑Start Guide
This article introduces MyBatis‑Flex, a lightweight yet high‑performance MyBatis enhancement framework, outlines its key features, compares it with similar tools, lists supported databases, and provides a step‑by‑step Spring Boot quick‑start tutorial with complete code examples.
MyBatis‑Flex is an elegant, lightweight extension of MyBatis that offers high performance and great flexibility for Java backend development. It allows developers to connect to any database with minimal configuration and reduces the amount of hand‑written SQL through its built‑in QueryWrapper.
Key Features
Lightweight : No third‑party dependencies, no interceptors, and no SQL parsing at runtime, resulting in high performance and easy debugging.
Flexible : Supports CRUD operations, pagination, multi‑table joins, sub‑queries, and provides Db+Row utilities that work without entity classes.
Powerful : Handles any relational database, multiple primary keys, logical deletion, optimistic locking, data masking, auditing, and more.
Comparison with Similar Frameworks
The following table highlights functional differences between MyBatis‑Flex, MyBatis‑Plus, and Fluent‑MyBatis (✓ = supported, ✗ = not supported, ❌ = unknown):
Feature
MyBatis‑Flex
MyBatis‑Plus
Fluent‑MyBatis
Basic CRUD
✓
✓
✓
Pagination
✓
✓
✓
Pagination cache
✓
✓
✗
SQL‑free pagination
✓
✗
✓
Multi‑table join
✓
✗
✗
Union/Union All
✓
✗
✓
Composite primary key
✓
✓
✓
Field typeHandler
✓
✓
✓
Logical delete
✓
✓
✓
Optimistic lock
✓
✓
✓
SQL audit
✓
✗
✗
Data fill (paid)
✓
✔️ (paid)
✓
Data masking (paid)
✓
✔️ (paid)
✗
Field encryption (paid)
✓
✔️ (paid)
✗
Performance Comparison
Benchmark results show that MyBatis‑Flex is roughly 5–10× faster than MyBatis‑Plus for single‑record queries, batch queries, pagination, and data updates.
Supported Databases
MyBatis‑Flex works with a wide range of databases, including MySQL, MariaDB, Oracle (both 11g and 12c), DB2, PostgreSQL, SQL Server, ClickHouse, Hive, and many others. Custom dialects can be added to support additional databases.
Quick Start Guide
Step 1: Create a 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>
</dependencies>Step 3: Configure Spring Boot
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678Annotate the main class with @MapperScan("com.mybatisflex.test.mapper") to scan mapper interfaces.
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
{ }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);
}
}The console will output the retrieved record, e.g., Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020) . The generated ACCOUNT class is provided by MyBatis‑Flex’s annotation processor.
Overall, MyBatis‑Flex combines the strengths of MyBatis‑Plus, jOOQ, and Fluent‑MyBatis, offering a highly efficient, feature‑rich ORM solution for Java backend projects.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.