Why MyBatis-Flex Beats MyBatis-Plus: Features, Benchmarks, and Quick Start Guide
MyBatis-Flex is a lightweight, high‑performance enhancement to MyBatis that offers flexible query tools, extensive database support, and impressive speed gains over MyBatis‑Plus, with a step‑by‑step quick‑start guide for Spring Boot integration.
What is MyBatis-Flex?
MyBatis-Flex is an elegant enhancement framework for MyBatis. It is extremely lightweight, has very high performance, and provides great flexibility, allowing developers to connect to any database with minimal SQL writing and reduced error risk.
Overall, MyBatis-Flex greatly improves development efficiency and experience, freeing developers to focus on business logic.
Features of MyBatis-Flex
Lightweight: No third‑party dependencies or interceptors; implemented via SqlProvider with no SQL parsing, resulting in high performance, easy debugging, and strong control.
Flexible: Supports CRUD and pagination directly on entities, and offers Db+Row tools for operations without entity classes. The built‑in QueryWrapper simplifies multi‑table, join, and sub‑query scenarios.
Powerful: Supports any relational database with extensible dialects, multi‑primary keys, logical deletion, optimistic locking, data masking, audit, fill, field encryption, field permissions, multi‑tenant, dynamic table names, dynamic schemas, and more.
Comparison with Similar Frameworks
Feature Comparison
Basic CRUD: supported by MyBatis-Flex, MyBatis‑Plus, Fluent‑MyBatis.
Pagination and total‑count cache: supported by MyBatis-Flex and MyBatis‑Plus, not by Fluent‑MyBatis.
SQL‑free pagination design: supported by MyBatis-Flex, not by MyBatis‑Plus.
Multi‑table queries (joins, unions): fully supported by MyBatis-Flex, limited or unsupported by the others.
Logical deletion, optimistic lock, audit, data fill, data masking, field encryption, field permissions: supported by MyBatis-Flex; some features require paid versions in MyBatis‑Plus.
QueryWrapper RPC transmission: supported by MyBatis-Flex, not by MyBatis‑Plus.
Performance Comparison
Single‑record query speed is about 5‑10× faster than MyBatis‑Plus.
Querying 10 records is about 5‑10× faster than MyBatis‑Plus.
Pagination query speed is about 5‑10× faster than MyBatis‑Plus.
Data update speed is about 5‑10× faster than MyBatis‑Plus.
For detailed benchmark results, see https://mybatis-flex.com/zh/intro/benchmark.html .
Supported Databases
MySQL, MariaDB, Oracle (11g and 12c), DB2, HSQL, SQLite, PostgreSQL, SQLServer (2005 and later)
DM, Xugu, KingbaseES, Phoenix (HBase), Gauss, ClickHouse, GBase, GBase‑8s, OceanBase, Firebird, Derby, HighGo, CUBRID, Goldilocks, CSIIDB, SAP_HANA, Impala, Vertica, Redshift, OpenGauss, TDengine, Informix, Greenplum, UXDB
Quick Start
Step 1: Create a 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 a 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 the data source in application.yml
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678Add @MapperScan("com.mybatisflex.test.mapper") 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 4: Define the 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);
}
}The console output will show the retrieved Account object, confirming that MyBatis‑Flex works as expected.
For more details and code generation tools, visit https://mybatis-flex.com/zh/others/codegen.html and the official site https://mybatis-flex.com/ .
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
