Mybatis enhanced framework Mybatis-Flex

1. What is Mybatis-Flex?

Mybatis-Flex is an elegant Mybatis enhancement framework that is very lightweight and has extremely high performance and flexibility. We can easily use Mybaits-Flex to connect to any database, and its built-in QueryWrapper^ highlights help us greatly reduce the work of SQL writing and reduce the possibility of errors.

MyBatis-Flex can greatly improve our development efficiency and development experience, allowing us to have more time to focus on our own things.

picture

  • Official website documentation: https://mybatis-flex.com/

2. What are the characteristics of Mybatis-Flex?

1. Lightweight: Except for MyBatis, there are no third-party dependencies or interceptors. The principle is light implementation through SqlProvider. At the same time, during the execution process, no SQL parsing (Parse) is performed lightly. This brings several benefits: 1. Extremely high performance; 2. Easily track and debug the code; 3. Higher control.

2. Flexible: While supporting Entity additions, deletions, modifications, and paging queries, Mybatis-Flex provides Db + Row^ flexible tools that can perform additions, deletions, modifications, and paging queries on the database without entity classes. At the same time, Mybatis-Flex's built-in QueryWrapper^ is flexible and can easily help us implement common SQL scenarios such as multi-table queries, link queries, subqueries, etc.

3. Powerful: It supports any relational database and can be continuously expanded through dialects. It also supports multiple (composite) primary keys, logical deletion, optimistic lock configuration, data desensitization, data auditing, data filling and other functions.

A front-end and back-end separated blog based on Spring Boot + MyBatis Plus + Vue 3.2 + Vite + Element Plus, including a back-end management system that supports articles, categories, tag management, dashboards and other functions.

  • GitHub address: https://github.com/weiwosuoai/WeBlog

  • Gitee address: https://gitee.com/AllenJiang/WeBlog

3. Comparison between Mybatis-Flex and similar frameworks

1) Function comparison:

function or feature MyBatis-Flex MyBatis-Plus Fluent-MyBatis
Basic addition, deletion, modification and query of entities
Paging query
Total cache of paging query
Paging query without SQL parsing design (more lightweight, and higher performance)
Multi-table query: from multiple tables
Multi-table query: left join, inner join, etc.
Multi-table query: union, union all
Single primary key configuration
Multiple ID generation strategies
Support multiple primary keys and composite primary keys
Field typeHandler configuration
Except MyBatis, no other third-party dependencies (more lightweight)
Does QueryWrapper support RPC transmission under microservice projects? unknown
Tombstone
optimistic lock
SQL Audit
data filling ✔️ (for a fee)
Data desensitization ✔️ (for a fee)
Field permissions ✔️ (for a fee)
Field encryption ✔️ (for a fee)
dictionary write back ✔️ (for a fee)
Db + Row
Entity monitoring
Multiple data source support With the help of other frameworks or charges
Whether multiple data sources support Spring's transaction management, such as @Transactional and TransactionTemplate, etc.
Does multiple data sources support "non-Spring" projects?
multi-tenant
dynamic table name
Dynamic Schema

2) Performance comparison:

Post the test results directly here:

  • MyBatis-Flex's query speed for a single piece of data is about 5 ~ 10+ times that of MyBatis-Plus.

  • MyBatis-Flex's query speed for 10 pieces of data is about 5 to 10 times that of MyBatis-Plus.

  • The paging query speed of Mybatis-Flex is about 5 to 10 times that of Mybatis-Plus.

  • The data update speed of Mybatis-Flex is about 5~10+ times that of Mybatis-Plus.

Specific performance comparison test, move to:

  • https://mybatis-flex.com/zh/intro/benchmark.html

4. Database types supported by Mybatis-Flex

The database types supported by MyBatis-Flex are shown in the table below. We can also continue to add more database support through custom dialects.

database describe
mysql MySQL database
mariadb MariaDB 数据库
oracle Oracle11g 及以下数据库
oracle12c Oracle12c 及以上数据库
db2 DB2 数据库
hsql HSQL 数据库
sqlite SQLite 数据库
postgresql PostgreSQL 数据库
sqlserver2005 SQLServer2005 数据库
sqlserver SQLServer 数据库
dm 达梦数据库
xugu 虚谷数据库
kingbasees 人大金仓数据库
phoenix Phoenix HBase 数据库
gauss Gauss 数据库
clickhouse ClickHouse 数据库
gbase 南大通用(华库)数据库
gbase-8s 南大通用数据库 GBase 8s
oscar 神通数据库
sybase Sybase ASE 数据库
OceanBase OceanBase 数据库
Firebird Firebird 数据库
derby Derby 数据库
highgo 瀚高数据库
cubrid CUBRID 数据库
goldilocks GOLDILOCKS 数据库
csiidb CSIIDB 数据库
hana SAP_HANA 数据库
impala Impala 数据库
vertica Vertica 数据库
xcloud 行云数据库
redshift 亚马逊 redshift 数据库
openGauss 华为 openGauss 数据库
TDengine TDengine 数据库
informix Informix 数据库
greenplum Greenplum 数据库
uxdb 优炫数据库

快速开始

第 1 步:创建数据库表

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');

第 2 步:创建 Spring Boot 项目,并添加 Maven 依赖

TIP:可以使用 Spring Initializer 快速初始化一个 Spring Boot 工程。

需要添加的 Maven 主要依赖示例:

<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>

第 3 步:对 Spring Boot 项目进行配置

在 application.yml 中配置数据源:

# DataSource Config
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/flex_test
    username: root
    password: 12345678

Add annotations to the Spring Boot startup class  @MapperScan and scan the Mapper folder:

@SpringBootApplication
@MapperScan("com.mybatisflex.test.mapper")
public class MybatisFlexTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisFlexTestApplication.class, args);
    }

}

Step 4: Write entity classes and Mapper interfaces

Lombok is used here to simplify the code. In addition, search the top python background of the public account and reply "Advanced" to get a surprise gift package.

@Data
@Table("tb_account")
public class Account {

    @Id(keyType = KeyType.Auto)
    private Long id;
    private String userName;
    private Integer age;
    private Date birthday;

}
  • Use to  @Table("tb_account") set the mapping relationship between entity classes and table names

  • Use  @Id(keyType = KeyType.Auto) identification primary key as auto-increment

The Mapper interface inherits the BaseMapper interface:

public interface AccountMapper extends BaseMapper<Account> {

}

This part can also be generated using the code generator of MyBatis-Flex, which is very powerful. Details enter:

  • https://mybatis-flex.com/zh/others/codegen.html

Step 5: Get started

Add a test class for functional testing:

import static com.mybatisflex.test.entity.table.AccountTableDef.ACCOUNT;

@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:

Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)

In the above example,  ACCOUNT MyBatis-Flex is automatically generated through APT and can be imported statically without manual coding.

A front-end and back-end separated blog based on Spring Boot + MyBatis Plus + Vue 3.2 + Vite + Element Plus, including a back-end management system that supports articles, categories, tag management, dashboards and other functions.

  • GitHub address: https://github.com/weiwosuoai/WeBlog

  • Gitee address: https://gitee.com/AllenJiang/WeBlog

Overall, this framework is an enhanced version of Mybatis, which integrates almost all the advantages of mybatis plus, jooq, and fluent mybatis. You can explore it. The official website:

https://mybatis-flex.com/

Guess you like

Origin blog.csdn.net/WXF_Sir/article/details/132022036