您现在的位置是:首页 > 文章详情

与 Mybatis-plus 对比,Mybatis-Flex v1.0.9 发布

日期:2023-04-10点击:315

Mybatis-Flex 是一个优雅的 Mybatis 增强框架,它非常轻量、同时拥有极高的性能与灵活性。我们可以轻松的使用 Mybaits-Flex 链接任何数据库,其内置的 QueryWrapper 帮助我们极大的减少了 SQL 编写的工作的同时,减少出错的可能性。

总而言之,Mybatis-Flex 能够极大地提高我们的开发效率和开发体验,让我们有更多的时间专注于自己的事情。

Mybatis-Flex v1.0.9 主要是增加了多租户以及对 Solon 支持的功能,同时继续完善了许多文档,到目前为止,Mybatis-Flex 的文档字数可能要比 Mybatis-Flex 的代码量还要多,我们始终认为:文档大于代码。具体的多租户功能请移步文档:https://mybatis-flex.com/zh/multi-tenancy.html

Mybatis-Flex v1.0.9 具体更新内容如下:

  • 新增:新增 多租户 使用的相关模块
  • 新增:BaseMapper 添加 deleteByCondition 和 updateByCondition 方法
  • 新增:添加 paginate 的更简单易用的相关方法
  • 新增:QueryMethods 添加 column() 方法
  • 新增:ConsoleMessageCollector 用于在控制台输出 SQL 及其执行时间
  • 新增:QueryWrapper 添加 union 和 union all 的支持
  • 新增:mybatis-flex-solon-plugin 插件,方便在 solon 框架下使用
  • 修复:@Table(onSet) 配置在某些场景无法使用的问题
  • 修复:Postgresql 的 limit offset 方言出错的问题
  • 修复:多数据源的场景下,通过 @Table(dataSource) 配置无效的问题
  • 优化:修改错别字 processer 为 processor
  • 优化:优化 DbAutoConfiguration 未正确配置数据源时的错误信息
  • 优化:Row 添加 getString()/getInt() 等等相关方法
  • 优化:代码生成器通过 SqlServer 生成出错的问题
  • 文档:优化 QueryWrapper 的相关文档
  • 文档:优化 SQL 审计的相关文档
  • 文档:添加 SQL 控制台打印输出的相关文档
  • 文档:添加多租户的相关文档
  • 文档:优化逻辑删除的相关文档

和其他框架对比

 

功能对比

MyBatis-Flex 主要是和 MyBatis-Plus  Fluent-Mybatis 对比,内容来源其官网、git 或者 网络文章,若有错误欢迎指正。

  • MyBatis-Plus:老牌的 MyBatis 增强框架
  • Fluent-Mybatis:阿里云开发的 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项目
多租户

以上内容来自第三方相关产品的官方文档或第三方平台,若有错误,欢迎纠正。

基础查询

MyBatis-Flex:

 QueryCondition condition = EMPLOYEE.LAST_NAME.like("B")  .and(EMPLOYEE.GENDER.eq(1))  .and(EMPLOYEE.AGE.gt(24)); List<Employee> employees = employeeMapper.selectListByCondition(condition); 

MyBatis-Plus:

 QueryWrapper<Employee> queryWrapper = new QueryWrapper<>(); queryWrapper  .like("last_name","B")  .eq("gender",1)  .gt("age",24); List<Employee> employees = employeeMapper.selectList(queryWrapper); 

或者使用 MyBatis-Plus 的 LambdaQueryWrapper 写法:

 LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper  .like(Employee::getUserName,"B")  .eq(Employee::getGender,1)  .gt(Employee::getAge,24); List<Employee> employees = employeeMapper.selectList(queryWrapper); 

Fluent-MyBatis:

 EmployeeQuery query = new EmployeeQuery()  .where.lastName().like("B")  .and.gender().eq(1)  .and.age().gt(24)  .end(); List<Employee> employees = employeeMapper.listEntity(query); 

查询集合函数

MyBatis-Flex:

 QueryWrapper query = new QueryWrapper()  .select(  ACCOUNT.ID,  ACCOUNT.USER_NAME,  max(ACCOUNT.BIRTHDAY),  avg(ACCOUNT.SEX).as("sex_avg")  ); List<Employee> employees = employeeMapper.selectListByQuery(query); 

MyBatis-Plus:

 QueryWrapper<Employee> queryWrapper = new QueryWrapper<>(); queryWrapper  .select(  "id"  ,"user_name"  ,"max(birthday)"  ,"avg(birthday) as sex_avg"  ); List<Employee> employees = employeeMapper.selectList(queryWrapper);  

Fluent-MyBatis:

 EmployeeQuery query = new EmployeeQuery()  .select  .id()  .userName()  .max.birthday()  .avg.sex("sex_avg")  .end() List<Employee> employees = employeeMapper.listEntity(query);

and(...) 和 or(...)

假设我们要构建如下的 SQL 进行查询(需要在 SQL 中添加括号)。

 SELECT * FROM tb_account WHERE id >= 100 AND (sex = 1 OR sex = 2) OR (age IN (18,19,20) AND user_name LIKE "%michael%" ) 

MyBatis-Flex:

 QueryWrapper query = QueryWrapper.create()  .where(ACCOUNT.ID.ge(100))  .and(ACCOUNT.SEX.eq(1).or(ACCOUNT.SEX.eq(2)))  .or(ACCOUNT.AGE.in(18,19,20).and(ACCOUNT.USER_NAME.like("michael"))); 

MyBatis-Plus:

 QueryWrapper<Employee> query = new QueryWrapper<>(); queryWrapper.ge("id",100)  .and(i->i.eq("sex",1).or(x->x.eq("sex",2)))  .or(i->i.in("age",{18,19,20}).like("user_name","michael")); 

Fluent-Mybatis:

 AccountQuery query = new AccountQuery()  .where.id().ge(100)  .and(  new AccountQuery().where.sex().eq(1).or(  new AccountQuery().where.sex().eq(2).end()  ).end())  .or(  new AccountQuery().where.age.in(18,19,20)  .and.userName().like("michael").end()  )  .end();

多表查询 1

MyBatis-Flex:

 QueryWrapper query = QueryWrapper.create()  .select().from(ACCOUNT)  .leftJoin(ARTICLE).on(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID))  .where(ACCOUNT.AGE.ge(10)); List<Account> accounts = mapper.selectListByQuery(query); 

MyBatis-Plus:

 // 不支持~~~~ 

Fluent-MyBatis:

 StudentQuery leftQuery = new StudentQuery("a1").selectAll()  .where.age().eq(34)  .end(); HomeAddressQuery rightQuery = new HomeAddressQuery("a2")  .where.address().like("address")  .end(); IQuery query = leftQuery  .join(rightQuery)  .on(l -> l.where.homeAddressId(), r -> r.where.id()).endJoin()  .build(); List<StudentEntity> entities = this.mapper.listEntity(query);

多表查询 2

假设查询的 SQL 如下:

 SELECT a.id, a.user_name, b.id AS articleId, b.title FROM tb_account AS a, tb_article AS b WHERE a.id = b.account_id 

MyBatis-Flex:

 QueryWrapper query = new QueryWrapper() .select(  ACCOUNT.ID  , ACCOUNT.USER_NAME  , ARTICLE.ID.as("articleId")  , ARTICLE.TITLE) .from(ACCOUNT.as("a"), ARTICLE.as("b")) .where(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID)); 

MyBatis-Plus:

 // 不支持~~~~ 

Fluent-MyBatis:

 // 不支持~~~~ 

PS:也有可能是我自己不知道如何支持,而非 Fluent-MyBatis 原因,有知道的同学可以给下示例代码。

部分字段更新

假设一个实体类 Account 中,我们要更新其内容如下:

  • userName 为 "michael"
  • age 为 "18"
  • birthday 为 null

其他字段保持数据库原有内容不变,要求执行的 SQL 如下:

 update tb_account set user_name = "michael", age = 18, birthday = null  where id = 100 

Mybatis-Flex 代码如下:

 Account account = UpdateEntity.of(Account.class); account.setId(100); //设置主键 account.setUserName("michael"); account.setAge(18); account.setBirthday(null); accountMapper.update(account); 

Mybatis-Plus 代码如下:

 UpdateWrapper<Account> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("id", 100); updateWrapper.set("user_name", "michael"); updateWrapper.set("age", 18); updateWrapper.set("birthday", null); accountMapper.update(null, updateWrapper); 

Fluent-Mybatis 代码如下:

 AccountUpdate update = new AccountUpdate() .update.userName().is("michael") .age().is(18) .birthday().is(null) .end() .where.id().eq(100) .end(); accountMapper.updateBy(update); 

进一步了解 MyBatis-Flex 框架,请参考一下链接:

原文链接:https://www.oschina.net/news/236158/mybatis-flex-1-0-9-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章