干掉XMLMapper,新出的FluentMybatis真香
使用fluentmybatis可以不用写具体的xml文件,通过javaapi可以构造出比较复杂的业务sql语句,做到代码逻辑和sql逻辑的合一。
不再需要在Dao中组装查询或更新操作,在xml或mapper中再组装参数。
那对比原生Mybatis,MybatisPlus或者其他框架,FluentMybatis提供了哪些便利呢?需求场景设置
我们通过一个比较典型的业务需求来具体实现和对比下,假如有学生成绩表结构如下:
现在有需求:
统计2000年三门学科(英语,数学,语文)及格分数按学期,学科统计最低分,最高分和平均分,且样本数需要大于1条,统计结果按学期和学科排序
我们可以写SQL语句如下
那上面的需求,分别用fluentmybatis,原生mybatis和Mybatisplus来实现一番。三者实现对比使用fluentmybatis来实现上面的功能
具体代码:
https:gitee。comfluentmybatisfluentmybatisdocstreemasterspringbootdemo
我们可以看到fluentapi的能力,以及IDE对代码的渲染效果。换成mybatis原生实现效果
1。定义Mapper接口publicinterfaceMyStudentScoreMapper{ListMapString,ObjectsummaryScore(SummaryQueryparas);}
2。定义接口需要用到的参数实体SummaryQuery
3。定义实现业务逻辑的mapperxml文件selectidsummaryScoreresultTypemapparameterTypecn。org。fluent。mybatis。springboot。demo。mapper。SummaryQueryselectschoolterm,subject,count(score)ascount,min(score)asminscore,max(score)asmaxscore,avg(score)asmaxscorefromstudentscorewhereschoolterm{schoolTerm}andsubjectinforeachcollectionsubjectsitemitemopen(close)separator,{item}foreachandscore{score}andisdeleted0groupbyschoolterm,subjecthavingcount(score){minCount}orderbyschoolterm,subjectselect
4。实现业务接口(这里是测试类,实际应用中应该对应Dao类)
总之,直接使用mybatis,实现步骤还是相当的繁琐,效率太低。换成mybatisplus的效果怎样呢?换成mybatisplus实现效果
mybatisplus的实现比mybatis会简单比较多,实现效果如下
如红框圈出的,写mybatisplus实现用到了比较多字符串的硬编码(可以用Entity的getlambda方法部分代替字符串编码)。
字符串的硬编码,会给开发同学造成不小的使用门槛,个人觉得主要有2点:字段名称的记忆和敲码困难Entity属性跟随数据库字段发生变更后的运行时错误
其他框架,比如TkMybatis在封装和易用性上比mybatisplus要弱,就不再比较了。生成代码编码比较fluentmybatis生成代码设置publicclassAppEntityGenerator{staticfinalStringurljdbc:mysql:localhost:3306fluentmybatisdemo?useSSLfalseuseUnicodetruecharacterEncodingutf8;publicstaticvoidmain(String〔〕args){FileGenerator。build(Abc。class);}Tables(数据库连接信息urlurl,usernameroot,passwordpassword,Entity类parentpackage路径basePackcn。org。fluent。mybatis。springboot。demo,Entity代码源目录srcDirspringbootdemosrcmainjava,Dao代码源目录daoDirspringbootdemosrcmainjava,如果表定义记录创建,记录修改,逻辑删除字段gmtCreatedgmtcreate,gmtModifiedgmtmodified,logicDeletedisdeleted,需要生成文件的表(表名称:对应的Entity名称)tablesTable(value{studentscore}))staticclassAbc{}}mybatisplus代码生成设置publicclassCodeGenerator{staticStringdbUrljdbc:mysql:localhost:3306fluentmybatisdemo?useSSLfalseuseUnicodetruecharacterEncodingutf8;TestpublicvoidgenerateCode(){GlobalConfigconfignewGlobalConfig();DataSourceConfigdataSourceConfignewDataSourceConfig();dataSourceConfig。setDbType(DbType。MYSQL)。setUrl(dbUrl)。setUsername(root)。setPassword(password)。setDriverName(Driver。class。getName());StrategyConfigstrategyConfignewStrategyConfig();strategyConfig。setCapitalMode(true)。setEntityLombokModel(false)。setNaming(NamingStrategy。underlinetocamel)。setColumnNaming(NamingStrategy。underlinetocamel)。setEntityTableFieldAnnotationEnable(true)。setFieldPrefix(newString〔〕{test})。setInclude(newString〔〕{studentscore})。setLogicDeleteFieldName(isdeleted)。setTableFillList(Arrays。asList(newTableFill(gmtcreate,FieldFill。INSERT),newTableFill(gmtmodified,FieldFill。INSERTUPDATE)));config。setActiveRecord(false)。setIdType(IdType。AUTO)。setOutputDir(System。getProperty(user。dir)srcmainjava)。setFileOverride(true);newAutoGenerator()。setGlobalConfig(config)。setDataSource(dataSourceConfig)。setStrategy(strategyConfig)。setPackageInfo(newPackageConfig()。setParent(com。mp。demo)。setController(controller)。setEntity(entity))。execute();}}FluentMybatis特性一览
三者对比总结
看完3个框架对同一个功能点的实现,各位看官肯定会有自己的判断,笔者这里也总结了一份比较。
来源:https:juejin。cnpost6886019929519177735