Fluent Mybatis實(shí)際開發(fā)中的優(yōu)勢對比
之前文章介紹過了Fluent基本框架等,其中有幾個重要的方法用到了IQuery和IUpdate對象。 這2個對象是FluentMybatis實(shí)現(xiàn)復(fù)雜和動態(tài)sql的構(gòu)造類,通過這2個對象fluent mybatis可以不用寫具體的xml文件, 直接通過java api可以構(gòu)造出比較復(fù)雜的業(yè)務(wù)sql語句,做到代碼邏輯和sql邏輯的合一。下面接著介紹如何通過IQuery和IUpdate定義強(qiáng)大的動態(tài)SQL語句。
表結(jié)構(gòu) 假如有學(xué)生成績表結(jié)構(gòu)如下:
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 '邏輯刪除標(biāo)識'
) engine = InnoDB default charset=utf8;
統(tǒng)計2000年到2019年, 三門學(xué)科(‘英語', ‘?dāng)?shù)學(xué)', ‘語文')分?jǐn)?shù)按學(xué)期,學(xué)科統(tǒng)計最低分,最高分和平均分,統(tǒng)計結(jié)果按學(xué)期和學(xué)科排序
SQL:
select school_term, subject, count(score), min(score), max(score), avg(score)
from student_score
where school_term between 2000 and 2019
and subject in ('英語', '數(shù)學(xué)', '語文')
and is_deleted = 0
group by school_term, subject
order by school_term, subject
- 通過FluentMybatis來具體實(shí)現(xiàn)
- 在StudentScoreDao類上定義接口
@Data
public class ScoreStatistics {
private int schoolTerm;
private String subject;
private long count;
private Integer minScore;
private Integer maxScore;
private BigDecimal avgScore;
}
public interface StudentScoreDao extends IBaseDao<StudentScoreEntity> {
/**
* 統(tǒng)計從fromYear到endYear年間學(xué)科subjects的統(tǒng)計數(shù)據(jù)
*
* @param fromYear 統(tǒng)計年份區(qū)間開始
* @param endYear 統(tǒng)計年份區(qū)間結(jié)尾
* @param subjects 統(tǒng)計的學(xué)科列表
* @return 統(tǒng)計數(shù)據(jù)
*/
List<ScoreStatistics> statistics(int fromYear, int endYear, String[] subjects);
}
在StudentScoreDaoImpl上實(shí)現(xiàn)業(yè)務(wù)邏輯
@Repository
public class StudentScoreDaoImpl extends StudentScoreBaseDao implements StudentScoreDao {
@Override
public List<ScoreStatistics> statistics(int fromSchoolTerm, int endSchoolTerm, String[] subjects) {
return super.listPoJos(ScoreStatistics.class, super.query()
.select.schoolTerm().subject()
.count("count")
.min.score("min_score")
.max.score("max_score")
.avg.score("avg_score")
.end()
.where.isDeleted().isFalse()
.and.schoolTerm().between(fromSchoolTerm, endSchoolTerm)
.and.subject().in(subjects)
.end()
.groupBy.schoolTerm().subject().end()
.orderBy.schoolTerm().asc().subject().asc().end()
);
}
}
- DaoImpl實(shí)現(xiàn)中,除了根據(jù)條件返回統(tǒng)計結(jié)果,還講結(jié)果按照下劃線轉(zhuǎn)駝峰的規(guī)則自動轉(zhuǎn)換為ScoreStatistics對象返回。
- 測試
@RunWith(SpringRunner.class)
@SpringBootTest(classes = QuickStartApplication.class)
public class StudentScoreDaoImplTest {
@Autowired
private StudentScoreDao dao;
@Test
public void statistics() {
List<ScoreStatistics> list = dao.statistics(2000, 2019, new String[]{"語文", "數(shù)學(xué)", "英語"});
System.out.println(list);
}
}
查看控制臺輸出結(jié)果:
DEBUG - ==> Preparing: SELECT school_term, subject, count(*) AS count, MIN(score) AS min_score, MAX(score) AS max_score, AVG(score) AS avg_score
FROM student_score
WHERE is_deleted = ?
AND school_term BETWEEN ? AND ?
AND subject IN (?, ?, ?)
GROUP BY school_term, subject
ORDER BY school_term ASC, subject ASC
DEBUG - ==> Parameters: false(Boolean), 2000(Integer), 2019(Integer), 語文(String), 數(shù)學(xué)(String), 英語(String)
DEBUG - <== Total: 30
[ScoreStatistics(schoolTerm=2000, subject=數(shù)學(xué), count=17, minScore=1, maxScore=93, avgScore=36.0588),
...
ScoreStatistics(schoolTerm=2009, subject=語文, count=24, minScore=3, maxScore=100, avgScore=51.2500)]
到此這篇關(guān)于Fluent Mybatis實(shí)際開發(fā)中的優(yōu)勢的文章就介紹到這了,更多相關(guān)Fluent Mybatis開發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot 整合fluent mybatis的過程,看這篇夠了
- Fluent MyBatis實(shí)現(xiàn)動態(tài)SQL
- Fluent Mybatis快速入門詳細(xì)教程
- Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
- Fluent Mybatis讓你擺脫Xml文件的技巧
- FluentMybatis實(shí)現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法
- Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對比
- Fluent Mybatis 批量更新的使用
相關(guān)文章
基于SpringBoot項目實(shí)現(xiàn)Docker容器化部署的主要步驟
部署SpringBoot項目到Docker容器涉及選擇Java運(yùn)行時環(huán)境的基礎(chǔ)鏡像、構(gòu)建包含應(yīng)用程序的Docker鏡像、編寫Dockerfile、使用docker build命令構(gòu)建鏡像和使用docker run命令運(yùn)行Docker容器等步驟2024-10-10
Java實(shí)現(xiàn)將String轉(zhuǎn)化為Int
這篇文章主要介紹了Java實(shí)現(xiàn)將String轉(zhuǎn)化為Int方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
使用ByteArrayOutputStream寫入字符串方式
這篇文章主要介紹了使用ByteArrayOutputStream寫入字符串方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot如何整合redis實(shí)現(xiàn)過期key監(jiān)聽事件
這篇文章主要介紹了SpringBoot如何整合redis實(shí)現(xiàn)過期key監(jiān)聽事件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
SpringBoot項目打成War布署在Tomcat的詳細(xì)步驟
這篇文章主要介紹了SpringBoot項目打成War布署在Tomcat,本文分步驟結(jié)合圖文實(shí)例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
SpringBoot整合GitLab-CI實(shí)現(xiàn)持續(xù)集成的過程
這篇文章主要介紹了SpringBoot整合GitLab-CI實(shí)現(xiàn)持續(xù)集成,本文詳細(xì)講述了 GitLab-CI 持續(xù)集成的安裝、部署、以及配置,需要的朋友可以參考下2022-12-12
springboot + mybatis-plus實(shí)現(xiàn)多表聯(lián)合查詢功能(注解方式)
這篇文章主要介紹了springboot + mybatis-plus實(shí)現(xiàn)多表聯(lián)合查詢功能,是最簡單的一種注解方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

