Why MyBatis-Flex Beats MyBatis-Plus: Features, Benchmarks, and Quick Start
MyBatis-Flex is a lightweight, high-performance MyBatis enhancement offering flexible CRUD, advanced QueryWrapper, extensive database support, and benchmarked speeds up to ten times faster than MyBatis-Plus, with a step-by-step quick-start guide covering table creation, Maven setup, Spring Boot configuration, entity mapping, and usage examples.
Overview
MyBatis-Flex is a lightweight MyBatis enhancement that provides a fluent QueryWrapper, reduces boilerplate SQL, and supports many relational databases.
Key Characteristics
Lightweight : No third‑party dependencies or interceptors; uses SqlProvider for direct SQL generation, which yields high performance and easy debugging.
Fluent API : QueryWrapper enables chainable select, where, join, union and sub‑query construction without writing SQL strings.
Rich Features : Supports CRUD, pagination with total‑count caching, multi‑primary keys, logical deletion, optimistic lock, data masking, audit, automatic field fill, multi‑tenant, dynamic table name/schema, and RPC‑compatible query objects.
Database Compatibility : Works with MySQL, MariaDB, Oracle (11g/12c), DB2, HSQL, SQLite, PostgreSQL, SQL Server, ClickHouse, and many others via custom dialects.
Performance
Internal benchmarks show MyBatis‑Flex is 5‑10× faster than MyBatis‑Plus for single‑record queries, batch queries, pagination and updates. Detailed results are published at https://mybatis-flex.com/zh/intro/benchmark.html.
Quick‑Start (Spring Boot)
1. Create a table
CREATE TABLE IF NOT EXISTS tb_account (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(100),
age INT,
birthday DATETIME
);
INSERT INTO tb_account(id, user_name, age, birthday) VALUES
(1, '张三', 18, '2020-01-11'),
(2, '李四', 19, '2021-03-21');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>
<!-- test only -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>3. Configure datasource
# application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678Add @MapperScan("com.mybatisflex.test.mapper") to the main application class.
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> {}5. Use QueryWrapper in code
@SpringBootTest
class MybatisFlexDemoTest {
@Autowired
private AccountMapper accountMapper;
@Test
void queryByAge() {
QueryWrapper query = QueryWrapper.create()
.select()
.where(ACCOUNT.AGE.eq(18)); // ACCOUNT is a generated column reference class
Account account = accountMapper.selectOneByQuery(query);
System.out.println(account);
}
}Additional Resources
Official documentation: https://mybatis-flex.com/
Code generator guide: https://mybatis-flex.com/zh/others/codegen.html
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
