Why MyBatis-Flex Outperforms MyBatis-Plus: Features, Performance & Quick Start
This article introduces MyBatis-Flex, a lightweight yet powerful MyBatis enhancement framework that boosts development efficiency with type‑safe QueryWrapper, extensive features like multi‑tenant and data encryption, supports dozens of databases, and delivers 5‑10× performance gains over MyBatis‑Plus, complete with a step‑by‑step quick‑start guide.
In the Java persistence layer ecosystem, MyBatis is popular for its flexibility and strong SQL control, but developers often write repetitive CRUD code. MyBatis-Flex is an elegant, lightweight, high‑performance enhancement framework designed to dramatically improve development efficiency.
Key Characteristics of MyBatis-Flex
Lightweight : No third‑party dependencies, no interceptors, implemented via SqlProvider without SQL parsing.
Flexible : Supports multi‑primary keys, multi‑table queries, logical deletion, optimistic lock, data masking, encryption, multiple data sources, sharding, field permissions, multi‑tenant, transaction management, SQL auditing, and more—all for free.
High Performance : Architecture yields 5‑10× faster CRUD, pagination, and batch operations compared to many similar frameworks.
Only Enhances : Retains full compatibility with native MyBatis features.
Comparison with Similar Frameworks
MyBatis-Flex is compared against MyBatis‑Plus (established since 2016) and Fluent‑MyBatis (developed by Alibaba Cloud). The comparison highlights that MyBatis-Flex provides full CRUD, pagination, multi‑table queries, field encryption, data masking, and many enterprise‑grade features without the licensing costs that some alternatives charge.
Performance Comparison
Single‑record query speed: ~5‑10× faster than MyBatis‑Plus.
Querying 10 records: ~5‑10× faster.
Pagination queries: ~5‑10× faster.
Data update speed: ~5‑10× faster.
Detailed benchmark results are available at https://mybatis-flex.com/zh/intro/benchmark.html .
Supported Databases
MyBatis-Flex supports a wide range of databases, including MySQL, MariaDB, Oracle (both 11g and 12c), DB2, HSQL, SQLite, PostgreSQL, SQL Server, DM, Xugu, KingbaseES, Phoenix (HBase), GaussDB, ClickHouse, GBase, OceanBase, Firebird, Derby, HighGo, CUBRID, GOLDILOCKS, CSIIDB, SAP HANA, Impala, Vertica, XCloud, Redshift, OpenGauss, TDengine, Informix, Greenplum, and UXDB. 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: Initialize 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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>Step 3: Configure Data Source in application.yml
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: Use QueryWrapper for Type‑Safe Queries
@SpringBootTest
class MybatisFlexTestApplicationTests {
@Autowired
private AccountMapper accountMapper;
@Test
void testSelect() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.where(ACCOUNT.AGE.eq(18));
Account account = accountMapper.selectOneByQuery(queryWrapper);
System.out.println(account);
}
}The ACCOUNT class is auto‑generated by MyBatis‑Flex via annotation processing, allowing static imports without manual coding.
Conclusion
MyBatis-Flex combines extreme lightweight design, powerful features, and unmatched performance while preserving full MyBatis compatibility. It offers enterprise‑grade capabilities such as multi‑data‑source support, data masking, and field encryption for free, making it an excellent choice for developers seeking a modern, efficient MyBatis enhancement framework.
Official documentation: https://mybatis-flex.com/ GitHub repository: https://github.com/mybatis-flex/mybatis-flex
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.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow 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.
