有了MyBatis-Flex ,再也不用MyBatis-Plus了
一、Mybatis-Flex是什么?
Mybatis-Flex 是一個優(yōu)雅的 Mybatis 增強框架,它非常輕量、同時擁有極高的性能與靈活性。我們可以輕松的使用 Mybaits-Flex 鏈接任何數(shù)據(jù)庫,其內(nèi)置的 QueryWrapper^亮點 幫助我們極大的減少了 SQL 編寫的工作的同時,減少出錯的可能性。
總而言之,MyBatis-Flex 能夠極大地提高我們的開發(fā)效率和開發(fā)體驗,讓我們有更多的時間專注于自己的事情。
圖片
- 官網(wǎng)文檔:https://mybatis-flex.com/
二、Mybatis-Flex的有什么特點?
1、輕量: 除了 MyBatis,沒有任何第三方依賴輕依賴、沒有任何攔截器,其原理是通過 SqlProvider 的方式實現(xiàn)的輕實現(xiàn)。同時,在執(zhí)行的過程中,沒有任何的 Sql 解析(Parse)輕運行。這帶來了幾個好處:1、極高的性能;2、極易對代碼進行跟蹤和調(diào)試;3、把控性更高。
2、靈活: 支持 Entity 的增刪改查、以及分頁查詢的同時,Mybatis-Flex 提供了 Db + Row^靈活 工具,可以無需實體類對數(shù)據(jù)庫進行增刪改查以及分頁查詢。與此同時,Mybatis-Flex 內(nèi)置的 QueryWrapper^靈活 可以輕易的幫助我們實現(xiàn) 多表查詢、鏈接查詢、子查詢 等等常見的 SQL 場景。
3、強大: 支持任意關(guān)系型數(shù)據(jù)庫,還可以通過方言持續(xù)擴展,同時支持 多(復(fù)合)主鍵、邏輯刪除、樂觀鎖配置、數(shù)據(jù)脫敏、數(shù)據(jù)審計、 數(shù)據(jù)填充 等等功能。
三、Mybatis-Flex和同類框架對比
1)功能對比:
功能或特點 | MyBatis-Flex | MyBatis-Plus | Fluent-MyBatis |
對 entity 的基本增刪改查 | ? | ? | ? |
分頁查詢 | ? | ? | ? |
分頁查詢之總量緩存 | ? | ? | ? |
分頁查詢無 SQL 解析設(shè)計(更輕量,及更高性能) | ? | ? | ? |
多表查詢:from 多張表 | ? | ? | ? |
多表查詢:left join、inner join 等等 | ? | ? | ? |
多表查詢:union,union all | ? | ? | ? |
單主鍵配置 | ? | ? | ? |
多種 id 生成策略 | ? | ? | ? |
支持多主鍵、復(fù)合主鍵 | ? | ? | ? |
字段的 typeHandler 配置 | ? | ? | ? |
除了 MyBatis,無其他第三方依賴(更輕量) | ? | ? | ? |
QueryWrapper 是否支持在微服務(wù)項目下進行 RPC 傳輸 | ? | ? | 未知 |
邏輯刪除 | ? | ? | ? |
樂觀鎖 | ? | ? | ? |
SQL 審計 | ? | ? | ? |
數(shù)據(jù)填充 | ? | ?? (收費) | ? |
數(shù)據(jù)脫敏 | ? | ?? (收費) | ? |
字段權(quán)限 | ? | ?? (收費) | ? |
字段加密 | ? | ?? (收費) | ? |
字典回寫 | ? | ?? (收費) | ? |
Db + Row | ? | ? | ? |
Entity 監(jiān)聽 | ? | ? | ? |
多數(shù)據(jù)源支持 | ? | 借助其他框架或收費 | ? |
多數(shù)據(jù)源是否支持 Spring 的事務(wù)管理,比如 @Transactional 和 TransactionTemplate 等 | ? | ? | ? |
多數(shù)據(jù)源是否支持 "非Spring" 項目 | ? | ? | ? |
多租戶 | ? | ? | ? |
動態(tài)表名 | ? | ? | ? |
動態(tài) Schema | ? | ? | ? |
2)性能對比:
這里直接貼測試結(jié)果:
- MyBatis-Flex 的查詢單條數(shù)據(jù)的速度,大概是 MyBatis-Plus 的 5 ~ 10+ 倍。
- MyBatis-Flex 的查詢 10 條數(shù)據(jù)的速度,大概是 MyBatis-Plus 的 5~10 倍左右。
- Mybatis-Flex 的分頁查詢速度,大概是 Mybatis-Plus 的 5~10 倍左右。
- Mybatis-Flex 的數(shù)據(jù)更新速度,大概是 Mybatis-Plus 的 5~10+ 倍。
具體性能對比測試,移步:
- https://mybatis-flex.com/zh/intro/benchmark.html
四、Mybatis-Flex支持的數(shù)據(jù)庫類型
MyBatis-Flex 支持的數(shù)據(jù)庫類型,如下表格所示,我們還可以通過自定義方言的方式,持續(xù)添加更多的數(shù)據(jù)庫支持。
數(shù)據(jù)庫 | 描述 |
mysql | MySQL 數(shù)據(jù)庫 |
mariadb | MariaDB 數(shù)據(jù)庫 |
oracle | Oracle11g 及以下數(shù)據(jù)庫 |
oracle12c | Oracle12c 及以上數(shù)據(jù)庫 |
db2 | DB2 數(shù)據(jù)庫 |
hsql | HSQL 數(shù)據(jù)庫 |
sqlite | SQLite 數(shù)據(jù)庫 |
postgresql | PostgreSQL 數(shù)據(jù)庫 |
sqlserver2005 | SQLServer2005 數(shù)據(jù)庫 |
sqlserver | SQLServer 數(shù)據(jù)庫 |
dm | 達夢數(shù)據(jù)庫 |
xugu | 虛谷數(shù)據(jù)庫 |
kingbasees | 人大金倉數(shù)據(jù)庫 |
phoenix | Phoenix HBase 數(shù)據(jù)庫 |
gauss | Gauss 數(shù)據(jù)庫 |
clickhouse | ClickHouse 數(shù)據(jù)庫 |
gbase | 南大通用(華庫)數(shù)據(jù)庫 |
gbase-8s | 南大通用數(shù)據(jù)庫 GBase 8s |
oscar | 神通數(shù)據(jù)庫 |
sybase | Sybase ASE 數(shù)據(jù)庫 |
OceanBase | OceanBase 數(shù)據(jù)庫 |
Firebird | Firebird 數(shù)據(jù)庫 |
derby | Derby 數(shù)據(jù)庫 |
highgo | 瀚高數(shù)據(jù)庫 |
cubrid | CUBRID 數(shù)據(jù)庫 |
goldilocks | GOLDILOCKS 數(shù)據(jù)庫 |
csiidb | CSIIDB 數(shù)據(jù)庫 |
hana | SAP_HANA 數(shù)據(jù)庫 |
impala | Impala 數(shù)據(jù)庫 |
vertica | Vertica 數(shù)據(jù)庫 |
xcloud | 行云數(shù)據(jù)庫 |
redshift | 亞馬遜 redshift 數(shù)據(jù)庫 |
openGauss | 華為 openGauss 數(shù)據(jù)庫 |
TDengine | TDengine 數(shù)據(jù)庫 |
informix | Informix 數(shù)據(jù)庫 |
greenplum | Greenplum 數(shù)據(jù)庫 |
uxdb | 優(yōu)炫數(shù)據(jù)庫 |
快速開始
第 1 步:創(chuàng)建數(shù)據(jù)庫表
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 中配置數(shù)據(jù)源:
# 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") 設(shè)置實體類與表名的映射關(guān)系
- 使用 @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)導(dǎo)入即可,無需手動編碼。
整體來講,這個框架是Mybatis的增強版,幾乎集成了mybatis plus、jooq、fluent mybatis的所有優(yōu)點,大家可以探索一番,官方網(wǎng)站:
https://mybatis-flex.com/