需求场景设置 三者实现对比 使用fluent mybatis 来实现上面的功能 换成mybatis原生实现效果 换成mybatis plus实现效果 生成代码编码比较 fluent mybatis生成代码设置 mybatis plus代码生成设置 fluentmybatis特性一览 三者对比总结 使用fluent mybatis可以不用写具体的xml文件,通过java api可以构造出比较复杂的业务sql语句,做到代码逻辑和sql逻辑的合一。不再需要在dao中组装查询或更新操作,在xml或mapper中再组装参数。那对比原生mybatis, mybatis plus或者其他框架,fluentmybatis提供了哪些便利呢?
需求场景设置 我们通过一个比较典型的业务需求来具体实现和对比下,假如有学生成绩表结构如下:
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_scorefrom student_scorewhere school_term >= 2000 and subject in ('英语', '数学', '语文') and score >= 60 and is_deleted = 0group by school_term, subjecthaving count(score) > 1order by school_term, subject; 那上面的需求,分别用fluent mybatis, 原生mybatis 和 mybatis plus来实现一番。
基于 spring boot + mybatis plus + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://gitee.com/zhijiantianya/ruoyi-vue-pro 视频教程:https://doc.iocoder.cn/video/ 三者实现对比 使用fluent mybatis 来实现上面的功能 具体代码
我们可以看到fluent api的能力,以及ide对代码的渲染效果。
换成mybatis原生实现效果 定义mapper接口 public interface mystudentscoremapper { list summaryscore(summaryquery paras);} 定义接口需要用到的参数实体 summaryquery @data@accessors(chain = true)public class summaryquery { private integer schoolterm; private list subjects; private integer score; private integer mincount;} 定义实现业务逻辑的mapper xml文件 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 >= #{schoolterm} and subject in #{item} and score >= #{score} and is_deleted = 0 group by school_term, subject having count(score) > #{mincount} order by school_term, subject 实现业务接口(这里是测试类, 实际应用中应该对应dao类) @runwith(springrunner.class)@springboottest(classes = quickstartapplication.class)public class mybatisdemo { @autowired private mystudentscoremapper mapper; @test public void mybatis_demo() { // 构造查询参数 summaryquery paras = new summaryquery() .setschoolterm(2000) .setsubjects(arrays.aslist(英语, 数学, 语文)) .setscore(60) .setmincount(1); list summary = mapper.summaryscore(paras); system.out.println(summary); }} 总之,直接使用mybatis,实现步骤还是相当的繁琐,效率太低。那换成mybatis plus的效果怎样呢?
换成mybatis plus实现效果 mybatis plus的实现比mybatis会简单比较多,实现效果如下
如红框圈出的,写mybatis plus实现用到了比较多字符串的硬编码(可以用entity的get lambda方法部分代替字符串编码)。字符串的硬编码,会给开发同学造成不小的使用门槛,个人觉的主要有2点:
字段名称的记忆和敲码困难 entity属性跟随数据库字段发生变更后的运行时错误 其他框架,比如tkmybatis在封装和易用性上比mybatis plus要弱,就不再比较了。
基于 spring cloud alibaba + gateway + nacos + rocketmq + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://gitee.com/zhijiantianya/yudao-cloud 视频教程:https://doc.iocoder.cn/video/ 生成代码编码比较 fluent mybatis生成代码设置 public class appentitygenerator { static final string url = jdbc//localhost:3306/fluent_mybatis_demo?usessl=false&useunicode=true&characterencoding=utf-8; public static void main(string[] args) { filegenerator.build(abc.class); } @tables( /** 数据库连接信息 **/ url = url, username = root, password = password, /** entity类parent package路径 **/ basepack = cn.org.fluent.mybatis.springboot.demo, /** entity代码源目录 **/ srcdir = spring-boot-demo/src/main/java, /** dao代码源目录 **/ daodir = spring-boot-demo/src/main/java, /** 如果表定义记录创建,记录修改,逻辑删除字段 **/ gmtcreated = gmt_create, gmtmodified = gmt_modified, logicdeleted = is_deleted, /** 需要生成文件的表 ( 表名称:对应的entity名称 ) **/ tables = @table(value = {student_score}) ) static class abc { }} mybatis plus代码生成设置 public class codegenerator { static string dburl = jdbc//localhost:3306/fluent_mybatis_demo?usessl=false&useunicode=true&characterencoding=utf-8; @test public void generatecode() { globalconfig config = new globalconfig(); datasourceconfig datasourceconfig = new datasourceconfig(); datasourceconfig.setdbtype(dbtype.mysql) .seturl(dburl) .setusername(root) .setpassword(password) .setdrivername(driver.class.getname()); strategyconfig strategyconfig = new strategyconfig(); strategyconfig .setcapitalmode(true) .setentitylombokmodel(false) .setnaming(namingstrategy.underline_to_camel) .setcolumnnaming(namingstrategy.underline_to_camel) .setentitytablefieldannotationenable(true) .setfieldprefix(new string[]{test_}) .setinclude(new string[]{student_score}) .setlogicdeletefieldname(is_deleted) .settablefilllist(arrays.aslist( new tablefill(gmt_create, fieldfill.insert), new tablefill(gmt_modified, fieldfill.insert_update))); config .setactiverecord(false) .setidtype(idtype.auto) .setoutputdir(system.getproperty(user.dir) + /src/main/java/) .setfileoverride(true); new autogenerator().setglobalconfig(config) .setdatasource(datasourceconfig) .setstrategy(strategyconfig) .setpackageinfo( new packageconfig() .setparent(com.mp.demo) .setcontroller(controller) .setentity(entity) ).execute(); }} fluentmybatis特性一览 三者对比总结 看完3个框架对同一个功能点的实现, 各位看官肯定会有自己的判断,笔者这里也总结了一份比较。
- mybatis plus fluent mybatis
代码生成 生成 entity 生成entity, 再通过编译生成 mapper, query, update 和 sqlprovider
generator易用性 低 高
和mybatis的共生关系 需替换原有的sqlsessionfactorybean 对mybatis没有任何修改,原来怎么用还是怎么用
动态sql构造方式 应用启动时, 根据entity注解信息构造动态xml片段,注入到mybatis解析器 应用编译时,根据entity注解,编译生成对应方法的sqlprovider,利用mybatis的mapper上@insertprovider @selectprovider @updateprovider注解关联
动态sql结果是否容易debug跟踪 不容易debug 容易,直接定位到sqlprovider方法上,设置断点即可
动态sql构造 通过硬编码字段名称, 或者利用entity的get方法的lambda表达式 通过编译手段生成对应的方法名,直接调用方法即可
字段变更后的错误发现 通过get方法的lambda表达的可以编译发现,通过字段编码的无法编译发现 编译时便可发现
不同字段动态sql构造方法 通过接口参数方式 通过接口名称方式, fluentapi的编码效率更高
语法渲染特点 无 通过关键变量select, update, set, and, or可以利用ide语法渲染, 可读性更高
用普通元件制作廉价胆机,vacuum tube power amplifier
在操作工业机器人时所需要注意的细节
智能家居自动化市场快速增长的原因是什么?
东芝发布全球首个QLC闪存:寿命将与TLC闪存旗鼓相当
向工程师咨询Griswold式显示器
Fluent Mybatis、原生Mybatis和Mybatis Plus对比
P60系列Z向大行程直线微运动平台的详细介绍
聚合物分散液晶薄膜的性能研究
中国移动发布了2020-2021年母线槽产品集中采购中标结果
汽车线束检测系统具有什么优势
土壤重金属元素分析仪的功能特点是什么
六类屏蔽网线能够抗电磁干扰吗
新唐科技N572F072简介
利用藻类和磁粒子开发微型机器人,可传递药物和远距引导
n沟道mos管和p沟道mos管详解
浅谈电子连接器对尼龙材质的要求
中芯国际可能研发出14nm工艺 台媒:三星在坏中芯国际的好事
为什么使用spring-authorization-server?
点成动态丨 2023中国(南京)国际科教技术及装备博览会(CESEE)精彩回顾
电子线对我们有着非常巨大的实际性价值