您好,登錄后才能下訂單哦!
這篇文章主要介紹了Fluent Mybatis實際開發中的優勢對比,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
之前文章介紹過了Fluent基本框架等,其中有幾個重要的方法用到了IQuery和IUpdate對象。 這2個對象是FluentMybatis實現復雜和動態sql的構造類,通過這2個對象fluent mybatis可以不用寫具體的xml文件, 直接通過java api可以構造出比較復雜的業務sql語句,做到代碼邏輯和sql邏輯的合一。
如何通過IQuery和IUpdate定義強大的動態SQL語句。
表結構 假如有學生成績表結構如下:
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年到2019年, 三門學科(‘英語', ‘數學', ‘語文')分數按學期,學科統計最低分,最高分和平均分,統計結果按學期和學科排序
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 ('英語', '數學', '語文') and is_deleted = 0 group by school_term, subject order by school_term, subject
通過FluentMybatis來具體實現
在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> { /** * 統計從fromYear到endYear年間學科subjects的統計數據 * * @param fromYear 統計年份區間開始 * @param endYear 統計年份區間結尾 * @param subjects 統計的學科列表 * @return 統計數據 */ List<ScoreStatistics> statistics(int fromYear, int endYear, String[] subjects); }
在StudentScoreDaoImpl上實現業務邏輯
@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實現中,除了根據條件返回統計結果,還講結果按照下劃線轉駝峰的規則自動轉換為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[]{"語文", "數學", "英語"}); System.out.println(list); } }
查看控制臺輸出結果:
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), 數學(String), 英語(String)
DEBUG - <== Total: 30
[ScoreStatistics(schoolTerm=2000, subject=數學, count=17, minScore=1, maxScore=93, avgScore=36.0588),
...
ScoreStatistics(schoolTerm=2009, subject=語文, count=24, minScore=3, maxScore=100, avgScore=51.2500)]
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Fluent Mybatis實際開發中的優勢對比”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。