- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能对比由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
使用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来实现一番.
。
。
具体代码 。
我们可以看到fluent api的能力,以及IDE对代码的渲染效果.
。
定义Mapper接口 。
public interface MyStudentScoreMapper { List<Map<String, Object>> summaryScore(SummaryQuery paras);}
定义接口需要用到的参数实体 SummaryQuery 。
@Data@Accessors(chain = true)public class SummaryQuery { private Integer schoolTerm; private List<String> subjects; private Integer score; private Integer minCount;}
定义实现业务逻辑的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>
实现业务接口(这里是测试类, 实际应用中应该对应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会简单比较多,实现效果如下 。
但正如红框圈出的,写mybatis plus实现用到了比较多字符串的硬编码(可以用Entity的get lambda方法部分代替字符串编码)。字符串的硬编码,会给开发同学造成不小的使用门槛,个人觉的主要有2点:
其他框架,比如TkMybatis在封装和易用性上比mybatis plus要弱,就不再比较了.
。
。
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 { }}
。
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(); }}
。
。
看完3个框架对同一个功能点的实现, 各位看官肯定会有自己的判断,笔者这里也总结了一份比较.
。
- | Mybatis Plus | Fluent Mybatis |
---|---|---|
代码生成 | 生成 Entity, Mapper, Wrapper等文件, 并且Generator很好用 | 只生成Entity, 再通过编译生成 Mapper, Query, Update 和 SqlProvider |
和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 Mybatis文档&示例 。
Fluent Mybatis源码, github 。
到此这篇关于Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能对比的文章就介绍到这了,更多相关Fluent Mybatis,原生Mybatis,Mybatis Plus内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://juejin.cn/post/6886019929519177735 。
最后此篇关于Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能对比的文章就讲到这里了,如果你想了解更多关于Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能对比的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
这是我的 Fluent 模型 struct Ailment: PostgreSQLModel { enum Frequency: String , Content { case regular
我正在尝试使用 Fluent Nhibernate 自动映射一个简单的继承层次结构,并且我需要为每个表使用与其类稍有不同的名称(下划线而不是 Pascal 大小写)。这似乎是一个使用约定的明显地方。我
如何为没有标识列的表指定流畅的 NHibernate 映射? 我想要这样的东西: public sealed class CustomerNewMap : ClassMap, IMap { p
使用 FluentMigrator,有没有办法找出 MigrateUp() 函数是否确实会迁移某些东西,或者它是否已经是最新的? 最佳答案 没有简单的方法可以使用公共(public) api 判断 M
我正在使用 Fluent NHibernate,我喜欢它! 我有一个小问题:启动时间大约是 10 秒,我不知道如何优化 Fluent nHibernate 为了减少这个启动时间的问题,我把它放在一个线
我在 Fluent NHIbernate 中使用 AutoPersistenceModel 来映射我的所有实体,并且一切正常:D 但是,我的几个对象有 public virtual IList Com
我有一个数据库,我正在运行多个应用程序。我喜欢通过为每个应用程序创建模式来分隔表。对于我最新的应用程序,我使用的是 FluentNHibernate。似乎我的大部分管道都是正确的,但是当我尝试查询其中
应用程序有许多扩展程序集,它们包含其类的映射。我需要为这些映射中的所有(基本、连接、多对多等)表名添加前缀。 例如 Assembly: ~/bin/Extensions/Foo.dll
您好,我很好奇 DDD 是如何使用 Fluent Nhibernate 真正实现的。例如,我有一个名为 User 的实体类和另一个名为 UserProfile 的类,就我而言,UserProfile
是否可以在 Fluent NHibernate 中映射来自多个程序集的实体? 我试过了 AutoPersistenceModel .MapEntitiesFromAssemblyOf() .AddEn
我有一个看起来像这样的基类: public abstract class MyBaseClass { public virtual DateTime UpdatedOn { get; set;
我有 Post 和 Comment 类,它们有一对多的关系,其中 Post 有一个评论列表。我如何将其映射为与 Fluent NHibernate 的单向关系,因为评论不需要知道其父 Post?目前,
我对如何查询模型对象的子对象并立即使用它感到困惑。我的Client包含数量Station child final class Client: PostgreSQLModel { var sta
目前我有一个表“ComponentAnalysis”和一个表“HistoryOfUse”,我正试图在 Fluent NHibernate 中进行映射。 一个成分分析应该只有1个使用历史,一个使用历史应
正如标题所说,我想知道我是否应该避免将 fluent nhibernate 用于生产代码,或者它是否足够成熟,可以“深入研究”? :) 最佳答案 FluentNHibernate API 尚未稳定下来
我正在尝试使用 Fluent NHibernate,我有几个问题。我发现缺少文档。 我知道 Fluent NHibernate/NHibernate 允许您自动生成数据库模式。人们通常只对测试/开发数
我正在使用 fluent-nhibernate 约定来映射我的实体: public class HasManyConvention : IHasManyConvention {
如何更改多列索引中的列顺序? IE: mapping.References(x => x.SomeReference).SetAttribute("index", "IX_index"); mappi
我需要像下面的代码一样创建一个外键: Create.ForeignKey().FromTable("TCGDocFiscalOpMedItem").ForeignColumn("IDCabecalho
我正在使用 Sharp 架构,并且在许多情况下都在实体中使用了值对象。这是一个明显的简单示例: public class Person : Entity { protected Person(
我是一名优秀的程序员,十分优秀!