一、mybatis-flex也是一個mybatis的增強框架它非常輕量、同時擁有極高的性能與靈活性。我們可以輕鬆的使用 Mybaits-Flex 鏈接任何數據庫。
二、和MyBatis-Plus
與 Fluent-Mybatis
對比。
功能或特點 | 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 監聽 | ✅ | ❌ | ❌ |
多數據源支持 | ✅ | 藉助其他框架或收費,不支持非Spring項目 | ❌ |
多租戶 | ✅ | ✅ | ❌ |
3、使用教程地址:https://mybatis-flex.com/zh/intro/getting-started.html
4、這裏對springboot相關的說明不是太多,這裏補充一些
1)springboot映入Maven
<dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-spring-boot-starter</artifactId> </dependency>
2)引入驅動和線程池
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
3)多數據源配置,參數和druid相關配置一致。
mybatis-flex: datasource: default: type: druid driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/flex?useSSL=false&useUnicode=true&characterEncoding=utf8 username: root password: root other: type: druid driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/flex2?useSSL=false&useUnicode=true&characterEncoding=utf8 username: root password: root mapper-locations: classpath:/mapper/*.xml
4)多數據源使用
5)數據查詢
a、加入配置mybatis-flex.properties
# 開啓table配置 processor.mappersGenerateEnable=true
b、通過idea工具的build功能可以使用查詢相關,類似lombok.默認生成在target/generated-sources下
c、查詢引用
說明:這裏的USER是通過編譯後的東西生成的,報錯情況下,手動build一下就可以了。
d、分頁查詢,默認是內置了方法的
e、mapper.xml的使用和mybatis一樣,這裏不錯說明。
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xbd.flex.mapper.UserMapper"> <select id="listUser" resultType="com.xbd.flex.entity.User"> select * from user; </select> </mapper>
6)控制檯日誌配置
@Configuration public class XbdConfigurationCustomizer implements ConfigurationCustomizer { public void customize(FlexConfiguration flexConfiguration) { flexConfiguration.setLogImpl(StdOutImpl.class); } }
7)代碼生成,通過表生成java類
public class Codegen { public static void main(String[] args) { //配置數據源 DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/flex?characterEncoding=utf-8"); dataSource.setUsername("root"); dataSource.setPassword("root"); //創建配置內容 GlobalConfig globalConfig = new GlobalConfig(); //設置只生成哪些表 globalConfig.addGenerateTable("test"); //設置 entity 的包名 globalConfig.setEntityPackage("com.xbd.flex.entity"); //父類 globalConfig.setEntitySupperClass(BaseEntity.class); //去除接口 globalConfig.setEntityInterfaces(new Class[0]); //設置 entity 是否使用 Lombok globalConfig.setEntityWithLombok(true); //是否生成 mapper 類,默認爲 false globalConfig.setMapperGenerateEnable(true); //設置 mapper 類的包名 globalConfig.setMapperPackage("com.xbd.flex.mapper"); //通過 datasource 和 globalConfig 創建代碼生成器 Generator generator = new Generator(dataSource, globalConfig); //生成代碼 generator.generate(); } }
5、這裏說明一下,這個框架默認覺得還有很多地方不是很完善。
1)*.xml的分頁方案,這裏是沒有的。還是得通過pagehelper等工具進行。(可能是我沒有發現)
2)代碼生成工具,在有父類時,沒有辦法進行字段過濾,查詢源碼,沒有找到相關配置。
3)baseMapper的查詢限制的比較死,類似JPA的方式,但是沒有JPA靈活。
4)官網的說明不夠細吧。