一、Mybatis-Flex是什麼?
Mybatis-Flex 是一個優雅的 Mybatis 增強架構,它非常輕量、同時擁有極高的性能與靈活性。我們可以輕松的使用 Mybaits-Flex 連結任何資料庫,其内置的 QueryWrapper^亮點 幫助我們極大的減少了 SQL 編寫的工作的同時,減少出錯的可能性。總而言之,MyBatis-Flex 能夠極大地提高我們的開發效率和開發體驗,讓我們有更多的時間專注于自己的事情。
二、Mybatis-Flex的有什麼特點?
1、輕量: 除了 MyBatis,沒有任何第三方依賴輕依賴、沒有任何攔截器,其原理是通過 SqlProvider 的方式實作的輕實作。同時,在執行的過程中,沒有任何的 Sql 解析(Parse)輕運作。這帶來了幾個好處:1、極高的性能;2、極易對代碼進行跟蹤和調試;3、把控性更高。
2、靈活: 支援 Entity 的增删改查、以及分頁查詢的同時,Mybatis-Flex 提供了 Db + Row^靈活 工具,可以無需實體類對資料庫進行增删改查以及分頁查詢。與此同時,Mybatis-Flex 内置的 QueryWrapper^靈活 可以輕易的幫助我們實作 多表查詢、連結查詢、子查詢 等等常見的 SQL 場景。
3、強大: 支援任意關系型資料庫,還可以通過方言持續擴充,同時支援 多(複合)主鍵、邏輯删除、樂觀鎖配置、資料脫敏、資料審計、 資料填充 等等功能。
三、Mybatis-Flex功能
對 entity 的基本增删改查 | ✅ |
分頁查詢 | ✅ |
分頁查詢之總量緩存 | ✅ |
分頁查詢無 SQL 解析設計(更輕量,及更高性能) | ✅ |
多表查詢:from 多張表 | ✅ |
多表查詢:left join、inner join 等等 | ✅ |
多表查詢:union,union all | ✅ |
單主鍵配置 | ✅ |
多種 id 生成政策 | ✅ |
支援多主鍵、複合主鍵 | ✅ |
字段的 typeHandler 配置 | ✅ |
除了 MyBatis,無其他第三方依賴(更輕量) | ✅ |
QueryWrapper 是否支援在微服務項目下進行 RPC 傳輸 | ✅ |
邏輯删除 | ✅ |
樂觀鎖 | ✅ |
SQL 審計 | ✅ |
資料填充 | ✅ |
資料脫敏 | ✅ |
字段權限 | ✅ |
字段加密 | ✅ |
字典回寫 | ✅ |
Db + Row | ✅ |
Entity 監聽 | ✅ |
多資料源支援 | ✅ |
多資料源是否支援 Spring 的事務管理,比如 @Transactional 和 TransactionTemplate 等 | ✅ |
多資料源是否支援 "非Spring" 項目 | ✅ |
多租戶 | ✅ |
動态表名 | ✅ |
動态 Schema | ✅ |
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+ 倍。
四、Mybatis-Flex搭建
第1步:建立 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>
第 2 步:對 Spring Boot 項目進行配置
在 application.yml 中配置資料源:
spring:
datasource:
url: jdbc:mysql://localhost:3306/datebasename
username: root
password: root
在 Spring Boot 啟動類中添加 @MapperScan 注解,掃描 Mapper 檔案夾:
第3 步:編寫實體類和 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 的代碼生成器來生,功能非常強大的
第 4步:開始使用
添加測試類,進行功能測試:
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);
}
}
整體來講,這個架構是Mybatis的增強版,幾乎內建了mybatis plus、jooq、fluent mybatis的所有優點,大家可以探索一番