一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

2021-11-05 10:13、唐城 Java教程

對比原生Mybatis, Mybatis Plus或者其他框架,F(xiàn)luentMybatis提供了哪些便利呢?很多朋友對這一問題不是很清楚,今天小編給大家?guī)硪黄坛剃P(guān)于Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一,一起看看吧

使用fluent mybatis可以不用寫具體的xml文件,通過java api可以構(gòu)造出比較復(fù)雜的業(yè)務(wù)sql語句,做到代碼邏輯和sql邏輯的合一。不再需要在dao中組裝查詢或更新操作,在xml或mapper中再組裝參數(shù)。那對比原生mybatis, mybatis plus或者其他框架,fluentmybatis提供了哪些便利呢?

Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

場景需求設(shè)置

我們通過一個比較典型的業(yè)務(wù)需求來具體實現(xiàn)和對比下,假如有學(xué)生成績表結(jié)構(gòu)如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
create table `student_score`
(
    id           bigint auto_increment comment '主鍵id' primary key,
    student_id bigint            not null comment '學(xué)號',
    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 '成績',
    gmt_create datetime not null comment '記錄創(chuàng)建時間',
    gmt_modified datetime not null comment '記錄最后修改時間',
    is_deleted tinyint default 0 not null comment '邏輯刪除標識'
) engine = innodb default charset=utf8;

現(xiàn)在有需求:

統(tǒng)計2000年三門學(xué)科('英語', '數(shù)學(xué)', '語文')及格分數(shù)按學(xué)期,學(xué)科統(tǒng)計最低分,最高分和平均分, 且樣本數(shù)需要大于1條,統(tǒng)計結(jié)果按學(xué)期和學(xué)科排序

我們可以寫sql語句如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 ('英語', '數(shù)學(xué)', '語文')
  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來實現(xiàn)一番。

三者對比

使用fluent mybatis 來實現(xiàn)上面的功能

Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

我們可以看到fluent api的能力,以及ide對代碼的渲染效果。

代碼:

換成mybatis原生實現(xiàn)效果

1. 定義mapper接口

?
1
2
3
public interface mystudentscoremapper {
    list<map<string, object>> summaryscore(summaryquery paras);
}

2. 定義接口需要用到的參數(shù)實體 summaryquery

?
1
2
3
4
5
6
7
8
@data
@accessors(chain = true)
public class summaryquery {
    private integer schoolterm;
    private list<string> subjects;
    private integer score;
    private integer mincount;
}

3. 定義實現(xiàn)業(yè)務(wù)邏輯的mapper xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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>

4. 實現(xiàn)業(yè)務(wù)接口(這里是測試類, 實際應(yīng)用中應(yīng)該對應(yīng)dao類)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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("英語", "數(shù)學(xué)", "語文"))
            .setscore(60)
            .setmincount(1);
 
        list<map<string, object>> summary = mapper.summaryscore(paras);
        system.out.println(summary);
    }
}

總之,直接使用mybatis,實現(xiàn)步驟還是相當?shù)姆爆崳侍汀D菗Q成mybatis plus的效果怎樣呢?

換成mybatis plus實現(xiàn)效果

mybatis plus的實現(xiàn)比mybatis會簡單比較多,實現(xiàn)效果如下:

Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

如紅框圈出的,寫mybatis plus實現(xiàn)用到了比較多字符串的硬編碼(可以用entity的get lambda方法部分代替字符串編碼)。字符串的硬編碼,會給開發(fā)同學(xué)造成不小的使用門檻,個人覺的主要有2點:

1. 字段名稱的記憶和敲碼困難

2. entity屬性跟隨數(shù)據(jù)庫字段發(fā)生變更后的運行時錯誤

其他框架,比如tkmybatis在封裝和易用性上比mybatis plus要弱,就不再比較了。

生成代碼編碼比較

fluent mybatis生成代碼設(shè)置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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ù)庫連接信息 **/
        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",
        /** 需要生成文件的表 ( 表名稱:對應(yīng)的entity名稱 ) **/
        tables = @table(value = {"student_score"})
    )
    static class abc {
    }
}

mybatis plus代碼生成設(shè)置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();
    }
}

Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

看完3個框架對同一個功能點的實現(xiàn), 各位看官肯定會有自己的判斷,筆者這里也總結(jié)了一份比較。

Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

作者:稻草江南

鏈接:juejin.cn/post/6886019929519177735

Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一

到此這篇關(guān)于fluent mybatis如何做到代碼邏輯和sql邏輯的合一的文章就介紹到這了,更多相關(guān)fluent mybatis代碼邏輯和sql邏輯的合一內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/qq_41570658/article/details/119327923

延伸 · 閱讀

精彩推薦
  • Java教程20個非常實用的Java程序代碼片段

    20個非常實用的Java程序代碼片段

    這篇文章主要為大家分享了20個非常實用的Java程序片段,對java開發(fā)項目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程升級IDEA后Lombok不能使用的解決方法

    升級IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級,尋思已經(jīng)有好久沒有升過級了。升級完畢重啟之后,突然發(fā)現(xiàn)好多錯誤,本文就來介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程Java實現(xiàn)搶紅包功能

    Java實現(xiàn)搶紅包功能

    這篇文章主要為大家詳細介紹了Java實現(xiàn)搶紅包功能,采用多線程模擬多人同時搶紅包,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發(fā)現(xiàn)了對于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點,所以這篇文章主要給大家介紹了關(guān)于Java8中S...

    阿杜7482021-02-04
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學(xué)習使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程xml與Java對象的轉(zhuǎn)換詳解

    xml與Java對象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對象的轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關(guān)于小米推送Java代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧...

    富貴穩(wěn)中求8032021-07-12
主站蜘蛛池模板: 亚洲网站在线播放 | 美日毛片| 国产ab| freesex性欧美炮机喷潮 | 日本黄视频在线播放 | 亚洲激情视频在线 | 国产精品夜夜爽张柏芝 | 久草在线福利资站免费视频 | 穆挂英风流艳史小说 | 色色色色网站 | 睡男神的这件小事小说在线阅读 | 日韩精品一二三区 | 日韩一级片在线免费观看 | 男女视频在线观看网站 | 窝窝影院午夜色在线视频 | 欧美亚洲一区二区三区 | 久久青草费线频观看国产 | 亚洲男人天堂a | 国产精品久久久久久久久久久搜索 | 视频网站入口在线看 | 欧美一区二区三区四区五区六区 | 四虎色影院 | 俄罗斯妈妈235 | 免费xxxx日本大片在线观看 | 国产精品久线观看视频 | 欧美帅老头oldmangay | 性free非洲老妇 | 久久99亚洲热最新地址获取 | 4444kk在线看片 | 91成| 奇米777狠狠| 99久热只有精品视频免费看 | 国产日韩欧美视频 | 高跟丝袜麻麻求我调教 | 91国语自产拍在线观看 | 美女黄金大片视频免费看 | pregnant欧美孕交xxx | 91麻豆精品国产91久久久 | 91看片淫黄大片在看 | 午夜想想爱| 亚洲嫩模吧粉嫩粉嫩冒白浆 |