当前位置:首页 > 实用技巧 >

fluent报告完成后怎么看是否合格(fluent边界条件怎么没有入口了)

来源:原点资讯(www.yd166.com)时间:2024-01-12 23:40:57作者:YD166手机阅读>>

使用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_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来实现一番。

三者实现对比使用fluent mybatis 来实现上面的功能

fluent报告完成后怎么看是否合格,fluent边界条件怎么没有入口了(1)

我们可以看到fluent api的能力,以及IDE对代码的渲染效果。

换成mybatis原生实现效果
  1. 定义Mapper接口

public interface MyStudentScoreMapper { List<Map<String, Object>> summaryScore(SummaryQuery paras); }

  1. 定义接口需要用到的参数实体 SummaryQuery

@Data @Accessors(chain = true) public class SummaryQuery { private Integer schoolTerm; private List<String> subjects; private Integer score; private Integer minCount; }

  1. 定义实现业务逻辑的mapper xml文件

<select id="summaryScore" resultType="map" parameterType="cn.org.fluent.mybatis.springboot.demo.mapper.SummaryQuery"> 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 <foreach collection="subjects" item="item" open="(" close=")" separator=","> #{item} </foreach> and score >= #{score} and is_deleted = 0 group by school_term, subject having count(score) > #{minCount} order by school_term, subject </select>

  1. 实现业务接口(这里是测试类, 实际应用中应该对应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<Map<String, Object>> summary = mapper.summaryScore(paras); System.out.println(summary); } }

总之,直接使用mybatis,实现步骤还是相当的繁琐,效率太低。那换成mybatis plus的效果怎样呢?

换成mybatis plus实现效果

mybatis plus的实现比mybatis会简单比较多,实现效果如下

fluent报告完成后怎么看是否合格,fluent边界条件怎么没有入口了(2)

如红框圈出的,写mybatis plus实现用到了比较多字符串的硬编码(可以用Entity的get lambda方法部分代替字符串编码)。字符串的硬编码,会给开发同学造成不小的使用门槛,个人觉的主要有2点:

  1. 字段名称的记忆和敲码困难
  2. Entity属性跟随数据库字段发生变更后的运行时错误

其他框架,比如TkMybatis在封装和易用性上比mybatis plus要弱,就不再比较了。

生成代码编码比较fluent mybatis生成代码设置

public class AppEntityGenerator { static final String url = "jdbc:mysql://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:mySQL://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特性一览

fluent报告完成后怎么看是否合格,fluent边界条件怎么没有入口了(3)

三者对比总结

看完3个框架对同一个功能点的实现, 各位看官肯定会有自己的判断,笔者这里也总结了一份比较。

-Mybatis PlusFluent Mybatis代码生成生成 Entity生成Entity, 再通过编译生成 Mapper, Query, Update 和 SqlProviderGenerator易用性低高和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语法渲染, 可读性更高

,

栏目热文

fluent速度积分怎么看(fluent结果图怎么看)

fluent速度积分怎么看(fluent结果图怎么看)

作者:胡坤转自公众号:CFD之道发表日期:2019-11-06关键词:本文从软件功能角度描述了STAR CCM...

2024-01-12 23:42:22查看全文 >>

fluent开检测点步骤(fluent怎样设置监测点)

fluent开检测点步骤(fluent怎样设置监测点)

伴随方法是一种专门的数学工具,提供流体系统在特定边界条件下性能的详细敏感性数据。伴随求解器可用于计算一个工程量对所有输入...

2024-01-12 23:23:53查看全文 >>

小刀调速是捏左还是右(小刀减震器怎么调)

小刀调速是捏左还是右(小刀减震器怎么调)

您在阅读前请点击上面的“关注”二字,后续会第一时间为您提供更多有价值的相关内容,感谢您的支持。虽然说现在很多城市只允许新...

2024-01-12 23:57:25查看全文 >>

dnf勋章和守护珠怎么获得(dnf勋章守护珠选项在哪里)

dnf勋章和守护珠怎么获得(dnf勋章守护珠选项在哪里)

大家好,我是十年,听说有人2周勋章就满级了有没有这么夸张,应该只是强化满级吧本期是勋章守护珠快速升满级的方法,简单说就是...

2024-01-12 23:27:09查看全文 >>

dnf完美的守护图腾勋章怎么得(dnf完美的守护图腾勋章怎么改)

dnf完美的守护图腾勋章怎么得(dnf完美的守护图腾勋章怎么改)

缪斯升级活动开始新的攻略了,时装护石勋章该如何做出选择呢?新职业活动角色两个多小时,可以轻松3.4名望,而且一级就可以领...

2024-01-12 23:50:26查看全文 >>

fluent怎么输出平均速度(fluent中输出设置)

fluent怎么输出平均速度(fluent中输出设置)

Fluent中的用户自定义函数(user defined function)UDF功能是非常强大灵活的技术,它可以帮助流...

2024-01-12 23:36:41查看全文 >>

fluent怎么检测阻力(fluent阻力监测图怎么获得)

fluent怎么检测阻力(fluent阻力监测图怎么获得)

主要内容1. 有限体积法2. Fluent中的多相流动模型3. 流场中颗粒的受力分析4. 单颗粒及颗粒群的阻力5. 气-...

2024-01-12 23:53:31查看全文 >>

fluent设置检测点(fluent入口压力波动怎么设置)

fluent设置检测点(fluent入口压力波动怎么设置)

来源:Ansys售后工程师整理的用户FAQ1.Q:FLUENT Meshing划分体网格时出现重叠节点错误问题描述:面网...

2024-01-12 23:34:40查看全文 >>

fluent怎么设置速度(fluent怎么设置周期性)

fluent怎么设置速度(fluent怎么设置周期性)

1. 简介今天我们接着说FLUENT UDF功能,我们经常使用的UDF宏主要有以下几种:DEFINE_PROFILE: ...

2024-01-12 23:18:38查看全文 >>

光头强去火锅店救小狗(光头强去火锅店救狗视频)

光头强去火锅店救小狗(光头强去火锅店救狗视频)

在熊出没中很多动漫迷们一直在争论光头强的身份,很多动漫迷们认为光头强是一个富二代,因为光头强开的是皮卡车,而且光头强还喜...

2024-01-12 23:27:01查看全文 >>

文档排行