有了MyBatis-Flex ,再也不用MyBatis-Plus了
一、Mybatis-Flex是什么?
Mybatis-Flex 是一個優(yōu)雅的 Mybatis 增強框架,它非常輕量、同時擁有極高的性能與靈活性。我們可以輕松的使用 Mybaits-Flex 鏈接任何數據庫,其內置的 QueryWrapper^亮點 幫助我們極大的減少了 SQL 編寫的工作的同時,減少出錯的可能性。
總而言之,MyBatis-Flex 能夠極大地提高我們的開發(fā)效率和開發(fā)體驗,讓我們有更多的時間專注于自己的事情。
圖片
- 官網文檔:https://mybatis-flex.com/
 
二、Mybatis-Flex的有什么特點?
1、輕量: 除了 MyBatis,沒有任何第三方依賴輕依賴、沒有任何攔截器,其原理是通過 SqlProvider 的方式實現的輕實現。同時,在執(zhí)行的過程中,沒有任何的 Sql 解析(Parse)輕運行。這帶來了幾個好處:1、極高的性能;2、極易對代碼進行跟蹤和調試;3、把控性更高。
2、靈活: 支持 Entity 的增刪改查、以及分頁查詢的同時,Mybatis-Flex 提供了 Db + Row^靈活 工具,可以無需實體類對數據庫進行增刪改查以及分頁查詢。與此同時,Mybatis-Flex 內置的 QueryWrapper^靈活 可以輕易的幫助我們實現 多表查詢、鏈接查詢、子查詢 等等常見的 SQL 場景。
3、強大: 支持任意關系型數據庫,還可以通過方言持續(xù)擴展,同時支持 多(復合)主鍵、邏輯刪除、樂觀鎖配置、數據脫敏、數據審計、 數據填充 等等功能。
三、Mybatis-Flex和同類框架對比
1)功能對比:
功能或特點  | MyBatis-Flex  | MyBatis-Plus  | Fluent-MyBatis  | 
對 entity 的基本增刪改查  | ?  | ?  | ?  | 
分頁查詢  | ?  | ?  | ?  | 
分頁查詢之總量緩存  | ?  | ?  | ?  | 
分頁查詢無 SQL 解析設計(更輕量,及更高性能)  | ?  | ?  | ?  | 
多表查詢:from 多張表  | ?  | ?  | ?  | 
多表查詢:left join、inner join 等等  | ?  | ?  | ?  | 
多表查詢:union,union all  | ?  | ?  | ?  | 
單主鍵配置  | ?  | ?  | ?  | 
多種 id 生成策略  | ?  | ?  | ?  | 
支持多主鍵、復合主鍵  | ?  | ?  | ?  | 
字段的 typeHandler 配置  | ?  | ?  | ?  | 
除了 MyBatis,無其他第三方依賴(更輕量)  | ?  | ?  | ?  | 
QueryWrapper 是否支持在微服務項目下進行 RPC 傳輸  | ?  | ?  | 未知  | 
邏輯刪除  | ?  | ?  | ?  | 
樂觀鎖  | ?  | ?  | ?  | 
SQL 審計  | ?  | ?  | ?  | 
數據填充  | ?  | ?? (收費)  | ?  | 
數據脫敏  | ?  | ?? (收費)  | ?  | 
字段權限  | ?  | ?? (收費)  | ?  | 
字段加密  | ?  | ?? (收費)  | ?  | 
字典回寫  | ?  | ?? (收費)  | ?  | 
Db + Row  | ?  | ?  | ?  | 
Entity 監(jiān)聽  | ?  | ?  | ?  | 
多數據源支持  | ?  | 借助其他框架或收費  | ?  | 
多數據源是否支持 Spring 的事務管理,比如 @Transactional 和 TransactionTemplate 等  | ?  | ?  | ?  | 
多數據源是否支持 "非Spring" 項目  | ?  | ?  | ?  | 
多租戶  | ?  | ?  | ?  | 
動態(tài)表名  | ?  | ?  | ?  | 
動態(tài) Schema  | ?  | ?  | ?  | 
2)性能對比:
這里直接貼測試結果:
- MyBatis-Flex 的查詢單條數據的速度,大概是 MyBatis-Plus 的 5 ~ 10+ 倍。
 - MyBatis-Flex 的查詢 10 條數據的速度,大概是 MyBatis-Plus 的 5~10 倍左右。
 - Mybatis-Flex 的分頁查詢速度,大概是 Mybatis-Plus 的 5~10 倍左右。
 - Mybatis-Flex 的數據更新速度,大概是 Mybatis-Plus 的 5~10+ 倍。
 
具體性能對比測試,移步:
- https://mybatis-flex.com/zh/intro/benchmark.html
 
四、Mybatis-Flex支持的數據庫類型
MyBatis-Flex 支持的數據庫類型,如下表格所示,我們還可以通過自定義方言的方式,持續(xù)添加更多的數據庫支持。
數據庫  | 描述  | 
mysql  | MySQL 數據庫  | 
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  | 優(yōu)炫數據庫  | 
快速開始
第 1 步:創(chuàng)建數據庫表
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 步:創(chuàng)建 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在 Spring Boot 啟動類中添加 @MapperScan 注解,掃描 Mapper 文件夾:
@SpringBootApplication
@MapperScan("com.mybatisflex.test.mapper")
public class MybatisFlexTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisFlexTestApplication.class, args);
    }
}第 4 步:編寫實體類和 Mapper 接口
這里使用了 Lombok 來簡化代碼。
@Data
@Table("tb_account")
public class Account {
    @Id(keyType = KeyType.Auto)
    private Long id;
    private String userName;
    private Integer age;
    private Date birthday;
}- 使用 @Table("tb_account") 設置實體類與表名的映射關系
 - 使用 @Id(keyType = KeyType.Auto) 標識主鍵為自增
 
Mapper 接口繼承 BaseMapper 接口:
public interface AccountMapper extends BaseMapper<Account> {
}這部分也可以使用 MyBatis-Flex 的代碼生成器來生,功能非常強大的。詳情進入:
- https://mybatis-flex.com/zh/others/codegen.html
 
第 5 步:開始使用
添加測試類,進行功能測試:
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);
    }
}控制臺輸出:
Account(id=1, userName=張三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)以上的 示例 中, ACCOUNT 為 MyBatis-Flex 通過 APT 自動生成,只需通過靜態(tài)導入即可,無需手動編碼。
整體來講,這個框架是Mybatis的增強版,幾乎集成了mybatis plus、jooq、fluent mybatis的所有優(yōu)點,大家可以探索一番,官方網站:
https://mybatis-flex.com/















 
 
 














 
 
 
 