15 取代MybatisPlus?fluent mybatis
2023-07-30 本文已影响0人
滔滔逐浪
使用fluent mybatis 可以不用写具体的xml文件,通过java api 可以构造出比较复杂的业务sql语句,做到代码逻辑和sql逻辑的合一。
不需要在Dao中组装查询的或者更新操作,在xml或者mapper中在组装参数。
那么对比原生Mybatis,Mybatis plus 或者其他框架,FluentMybatis提供了那些便利
1,需求场景设置
我们通过一个比较典型的业务需求来具体实现和对比下,假如有学生成绩表结构如下:
create table student_score
(
id bigint auto_increment comment '主键ID' primary key,
student_id bigint not null comment '学号',
gender_man tinyint default 0 not null comment '性别, 0:女; 1:男',
school_term int null comment '学期',
subject varchar(30) null comment '学科',
score int null comment '成绩',
gmt_create datetime not null comment '记录创建时间',
gmt_modified datetime not null comment '记录最后修改时间',
is_deleted tinyint default 0 not null comment '逻辑删除标识'
) engine = InnoDB default charset=utf8;
现在有需求:
统计2000年三门学科(英语,数学,语文)几个分数,按照学期,学科统计最低分,最高分和平均分,且样本需要大于1条,统计结果按照学期和学科排序。
我们可以写SQL语句如下:
select school_term,
subject,
count(score) as count,
min(score) as min_score,
max(score) as max_score,
avg(score) as max_score
from student_score
where school_term >= 2000
and subject in ('英语', '数学', '语文')
and score >= 60
and is_deleted = 0
group by school_term, subject
having count(score) > 1
order by school_term, subject;
那上面的需求分别使用fluent mybatis,原生mybatis 和mybatis plus来实现一番。
2, 三者实现对比
使用fluent mybatis 来实现上面的功能
![](https://img.haomeiwen.com/i12197462/3582820f24952a30.png)