使用fluent mybatis可以不用寫具體的xml文件,通過java api可以構(gòu)造出比較復(fù)雜的業(yè)務(wù)sql語句,做到代碼邏輯和sql邏輯的合一。不再需要在dao中組裝查詢或更新操作,在xml或mapper中再組裝參數(shù)。那對比原生mybatis, mybatis plus或者其他框架,fluentmybatis提供了哪些便利呢?
場景需求設(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 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)效果如下:
如紅框圈出的,寫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(); } } |
看完3個框架對同一個功能點的實現(xiàn), 各位看官肯定會有自己的判斷,筆者這里也總結(jié)了一份比較。
作者:稻草江南
鏈接:juejin.cn/post/6886019929519177735
到此這篇關(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