再見MybatisPlus,阿里推出新ORM框架!
300本計(jì)算機(jī)編程的經(jīng)典書籍下載
AI全套:Python3+TensorFlow打造人臉識(shí)別智能小程序
最新人工智能資料-Google工程師親授 Tensorflow-入門到進(jìn)階
黑馬頭條項(xiàng)目 - Java Springboot2.0(視頻、資料、代碼和講義)14天完整版
來源:juejin.cn/post/6886019929519177735
需求場(chǎng)景設(shè)置
我們通過一個(gè)比較典型的業(yè)務(wù)需求來具體實(shí)現(xiàn)和對(duì)比下,假如有學(xué)生成績(jī)表結(jié)構(gòu)如下:
create table `student_score`(id bigint auto_increment comment '主鍵ID' primary key,student_id bigint not null comment '學(xué)號(hào)',gender_man tinyint default 0 not null comment '性別, 0:女; 1:男',school_term int null comment '學(xué)期',subject varchar(30) null comment '學(xué)科',score int null comment '成績(jī)',gmt_create datetime not null comment '記錄創(chuàng)建時(shí)間',gmt_modified datetime not null comment '記錄最后修改時(shí)間',is_deleted tinyint default 0 not null comment '邏輯刪除標(biāo)識(shí)') engine = InnoDB default charset=utf8;現(xiàn)在有需求:
我們可以寫 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 >= 2000and subject in ('英語', '數(shù)學(xué)', '語文')and score >= 60and is_deleted = 0group by school_term, subjecthaving count(score) > 1order by school_term, subject;那上面的需求,分別用fluent mybatis, 原生mybatis和Mybatis plus來實(shí)現(xiàn)一番。
三者實(shí)現(xiàn)對(duì)比
使用fluent mybatis 來實(shí)現(xiàn)上面的功能
我們可以看到fluent api的能力,以及 IDE 對(duì)代碼的渲染效果。
換成mybatis原生實(shí)現(xiàn)效果
定義Mapper接口 public interface MyStudentScoreMapper {List, Object>> summaryScore(SummaryQuery paras);}
定義接口需要用到的參數(shù)實(shí)體 SummaryQuery (chain = true)public class SummaryQuery {private Integer schoolTerm;private List<String> subjects;private Integer score;private Integer minCount;}
定義實(shí)現(xiàn)業(yè)務(wù)邏輯的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_scorefrom student_scorewhere school_term >=and subject in<foreach collection="subjects" item="item" open="(" close=")" separator=",">foreach>and score >=and is_deleted = 0group by school_term, subjecthaving count(score) > #{minCount}order by school_term, subjectselect>
實(shí)現(xiàn)業(yè)務(wù)接口(這里是測(cè)試類,實(shí)際應(yīng)用中應(yīng)該對(duì)應(yīng) Dao 類) (SpringRunner.class)(classes = QuickStartApplication.class)public class MybatisDemo {private MyStudentScoreMapper mapper;public void mybatis_demo() {// 構(gòu)造查詢參數(shù)SummaryQuery paras = new SummaryQuery().setSchoolTerm(2000).setSubjects(Arrays.asList("英語", "數(shù)學(xué)", "語文")).setScore(60).setMinCount(1);List, Object>> summary = mapper.summaryScore(paras);System.out.println(summary);}}總之,直接使用 mybatis,實(shí)現(xiàn)步驟還是相當(dāng)?shù)姆爆崳侍汀D菗Q成mybatis plus的效果怎樣呢?
換成mybatis plus實(shí)現(xiàn)效果
mybatis plus的實(shí)現(xiàn)比mybatis會(huì)簡(jiǎn)單比較多,實(shí)現(xiàn)效果如下
如紅框圈出的,寫mybatis plus實(shí)現(xiàn)用到了比較多字符串的硬編碼(可以用 Entity 的 get lambda 方法部分代替字符串編碼)。字符串的硬編碼,會(huì)給開發(fā)同學(xué)造成不小的使用門檻,個(gè)人覺的主要有 2 點(diǎn):
其他框架,比如TkMybatis在封裝和易用性上比mybatis plus要弱,就不再比較了。
生成代碼編碼比較
fluent mybatis生成代碼設(shè)置
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(/** 數(shù)據(jù)庫(kù)連接信息 **/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",/** 如果表定義記錄創(chuàng)建,記錄修改,邏輯刪除字段 **/gmtCreated = "gmt_create", gmtModified = "gmt_modified", logicDeleted = "is_deleted",/** 需要生成文件的表 ( 表名稱:對(duì)應(yīng)的Entity名稱 ) **/tables = @Table(value = {"student_score"}))static class Abc {}}mybatis plus代碼生成設(shè)置
public class CodeGenerator {static String dbUrl = "jdbc:mysql://localhost:3306/fluent_mybatis_demo?useSSL=false&useUnicode=true&characterEncoding=utf-8";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特性一覽
三者對(duì)比總結(jié)
看完 3 個(gè)框架對(duì)同一個(gè)功能點(diǎn)的實(shí)現(xiàn), 各位看官肯定會(huì)有自己的判斷,筆者這里也總結(jié)了一份比較。
對(duì) Fluent Mybatis 感興趣的網(wǎng)友,支持大家去閱讀官方源碼,發(fā)現(xiàn)更多新大陸!
全棧架構(gòu)社區(qū)交流群
?「全棧架構(gòu)社區(qū)」建立了讀者架構(gòu)師交流群,大家可以添加小編微信進(jìn)行加群。歡迎有想法、樂于分享的朋友們一起交流學(xué)習(xí)。
看完本文有收獲?請(qǐng)轉(zhuǎn)發(fā)分享給更多人
往期資源:

