MyBatis Flex全解析
Mybatis-Flex 是一個優(yōu)雅的 Mybatis 增強框架,它非常輕量、同時擁有極高的性能與靈活性。我們可以輕松的使用 Mybaits-Flex 鏈接任何數(shù)據(jù)庫,其內(nèi)置的 QueryWrapper^亮點 幫助我們極大的減少了 SQL 編寫的工作的同時,減少出錯的可能性。
總而言之,MyBatis-Flex 能夠極大地提高我們的開發(fā)效率和開發(fā)體驗,讓我們有更多的時間專注于自己的事情。
Mybatis-Flex的特點
輕量
除了 MyBatis,沒有任何第三方依賴輕依賴、沒有任何攔截器,其原理是通過 SqlProvider 的方式實現(xiàn)的輕實現(xiàn)。同時,在執(zhí)行的過程中,沒有任何的 Sql 解析(Parse)輕運行。這帶來了幾個好處:
1、極高的性能;
2、極易對代碼進行跟蹤和調(diào)試;
3、把控性更高。
如果你近期準備面試跳槽,建議在ddkk.com在線刷題,涵蓋 一萬+ 道 Java 面試題,幾乎覆蓋了所有主流技術(shù)面試題,還有市面上最全的技術(shù)五百套,精品系列教程,免費提供。
靈活
支持 Entity 的增刪改查、以及分頁查詢的同時,Mybatis-Flex 提供了 Db + Row^靈活 工具,可以無需實體類對數(shù)據(jù)庫進行增刪改查以及分頁查詢。與此同時,Mybatis-Flex 內(nèi)置的 QueryWrapper^靈活 可以輕易的幫助我們實現(xiàn) 多表查詢、鏈接查詢、子查詢 等等常見的 SQL 場景。
強大
支持任意關(guān)系型數(shù)據(jù)庫,還可以通過方言持續(xù)擴展,同時支持 多(復(fù)合)主鍵、邏輯刪除、樂觀鎖配置、數(shù)據(jù)脫敏、數(shù)據(jù)審計、 數(shù)據(jù)填充 等等功能。
Mybatis-Flex和同類框架對比
1)功能對比


2)性能對比
- 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+ 倍。
Mybatis-Flex支持的數(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 | lmpala 數(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`user_info`
(
`id` INTEGER PRIMARY KEY auto_increment,
`user_name`VARCHAR(100),
`age` INTEGER,
`birthday` DATETIME
);
INSERTINTO tb_account(id, user_name, age, birthday)
VALUES(1, '張三', 18, '2020-01-11'),
(2, '李四', 19, '2021-03-21');第 2 步:創(chuàng)建 Spring Boot 項目,并添加 Maven 依賴
tips:可以使用 Spring Initializer 快速初始化一個 Spring Boot 工程。
<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>
<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")
publicclassMybatisFlexTestApplication{
publicstaticvoidmain(String[] args){
SpringApplication.run(MybatisFlexTestApplication.class, args);
}
}第 4 步:編寫實體類和 Mapper 接口
這里使用了 Lombok 來簡化代碼。
@Data
@Table("user_info")
public class UserInfo{
@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) 標(biāo)識主鍵為自增
Mapper 接口繼承 BaseMapper 接口:
public interface UserInfoMapperex tends BaseMapper<UserInfo> {
}這部分也可以使用 MyBatis-Flex 的代碼生成器來生成。
第 5 步:開始使用
添加測試類,進行功能測試:
import static com.mybatisflex.test.entity.table.UserInfo;
@SpringBootTest
class MybatisFlexTestApplicationTests{
@Autowired
private AccountMapper accountMapper;
@Test
voidcontextLoads(){
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.where(ACCOUNT.AGE.eq(18));
Account account = accountMapper.selectOneByQuery(queryWrapper);
System.out.println(account);
}
}控制臺輸出:
UserInfo(id=1, userName=張三, age=18, birthday=Sat Jan 1100:00:00 CST 2020)
上面的示例 中, UserInfo為MyBatis-Flex通過API自動生成,只需通過靜態(tài)導(dǎo)入即可,無需手動編碼。
整體來講,這個框架是Mybatis的增強版,幾乎集成了mybatis plus、jooq、fluent mybatis的所有優(yōu)點。
到此這篇關(guān)于MyBatis Flex全解析的文章就介紹到這了,更多相關(guān)MyBatis Flex詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合RabbitMQ發(fā)送短信的實現(xiàn)
本文會和SpringBoot做整合,實現(xiàn)RabbitMQ發(fā)送短信,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
springboot項目mysql-connector-java默認版本如何查看
這篇文章主要介紹了springboot項目mysql-connector-java默認版本如何查看問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)
這篇文章主要介紹了IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝),本文通過截圖給大家展示的非常詳細,需要的朋友可以參考下2021-09-09
全面理解java中的構(gòu)造方法以及this關(guān)鍵字的用法
本篇文章主要概述了如何用構(gòu)造方法初始化對象,this屬性名訪問成員變量方法,和this()的用法,感興趣的小伙伴一起來學(xué)習(xí)吧2023-03-03
java 中newInstance()方法和new關(guān)鍵字的區(qū)別
這篇文章主要介紹了java 中newInstance()方法和new關(guān)鍵字的區(qū)別的相關(guān)資料,希望通過本文大家能掌握他們之家的區(qū)別與用法,需要的朋友可以參考下2017-09-09
java加載properties文件的六種方法總結(jié)
這篇文章主要介紹了java加載properties文件的六種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-05-05

