Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢
使用Fluent Mybatis, 不用手寫一行xml文件或者M(jìn)apper文件,在dao類中即可使用java api構(gòu)造中比較復(fù)雜的嵌套查詢。讓dao的代碼邏輯和sql邏輯合二為一。
前置準(zhǔn)備,maven工程設(shè)置
參考文章 使用FluentMybatis實(shí)現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法
in (select 子查詢)嵌套查詢表和主查詢表一樣的場景
.column().in( query-> {對query設(shè)置條件})
只需要在in里面引用一個lambda表達(dá)式,lambda表達(dá)式入?yún)⑹且粋€同名的Query。對這個入?yún)⒖梢栽O(shè)置where參數(shù)。
@DisplayName('嵌套查詢和主查詢的表是同一個')@Testvoid test_in_same_table_query() { UserQuery query = new UserQuery().where.id().in(q -> q.selectId() .where.id().eq(3L).end()).and.userName().like('user').and.age().gt(23).end(); List list = mapper.listEntity(query);// 通過Test4J工具,驗證sql語句 db.sqlList().wantFirstSql().eq('SELECT id, gmt_create, gmt_modified, is_deleted, account, age, avatar, birthday, bonus_points, e_mail, password, phone, status, user_name ' + 'FROM user WHERE id IN (SELECT id FROM user WHERE id = ?) ' + 'AND user_name LIKE ? ' + 'AND age > ?');}
嵌套查詢表是另外表的場景.column().in(queryClass, query-> {對query設(shè)置條件})
如果嵌套查詢的不是同表一張表,需要在in方法里面顯式聲明一下Query對象的class類型, 后面用法同方法一。
@DisplayName('嵌套查詢和主查詢的表是不同')@Testvoid test_in_difference_table_query() { UserQuery query = new UserQuery().selectId().where.addressId().in(ReceivingAddressQuery.class, q -> q.selectId() .where.id().in(new int[]{1, 2}).end()).end(); mapper.listEntity(query);// 通過Test4J工具,驗證sql語句 db.sqlList().wantFirstSql().eq('SELECT id ' + 'FROM user ' + 'WHERE address_id IN (SELECT id FROM receiving_address WHERE id IN (?, ?))');}
not in嵌套查詢: 使用方法同 in 嵌套查詢
exists (select子查詢)嵌套查詢表和主查詢表一樣的場景Exists查詢不需要指定字段,直接在query where中可以引用exists方法。
exists( query-> {對query設(shè)置條件})如果exists查詢的表和主查詢一致,直接在lambada表達(dá)式中使用同類型query參數(shù)即可,參數(shù)用法同in方法。
exists(queryClass, query-> {對query設(shè)置條件})如果exists查詢的表和主查詢不一致,在exists方法第一個參數(shù)指定query類型,第二個參數(shù)同方法1。
具體示例
@DisplayName('EXISTS查詢')@Testvoid test_exists_query() { UserQuery query = new UserQuery().where.exists(ReceivingAddressQuery.class, q -> q .where.detailAddress().like('杭州') .and.id().apply(' = user.address_id').end()).end(); mapper.listEntity(query);// 通過Test4J工具,驗證sql語句 db.sqlList().wantFirstSql().eq('SELECT id, gmt_create, gmt_modified, is_deleted, account, address_id, age, avatar, birthday, bonus_points, e_mail, password, phone, status, user_name ' +'FROM user ' +'WHERE EXISTS (SELECT *' +' FROM receiving_address' +' WHERE detail_address LIKE ?' +' AND id = user.address_id)', StringMode.SameAsSpace);}
注:示例中的測試,是使用H2內(nèi)存數(shù)據(jù)庫,你可以直接運(yùn)行,不需要你額外建表。但使用Test4J執(zhí)行測試,你需要在加入vm參數(shù):-javaagent:/這里是你本地maven倉庫地址/org/jmockit/jmockit/1.48/jmockit-1.48.jar
以我本機(jī)為例,具體參數(shù)如圖:
Fluent Mybatis文檔&示例
Fluent Mybatis源碼, github
到此這篇關(guān)于Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢的文章就介紹到這了,更多相關(guān)Fluent Mybatis 復(fù)雜嵌套查詢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 在AIX 5L上快速部署Oracle2. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫的備份與恢復(fù)3. 使用MySqldump命令導(dǎo)出數(shù)據(jù)時的注意4. MySQL全文搜索之布爾搜索5. 在SQL Server 2005數(shù)據(jù)庫中修改存儲過程6. 啟動MYSQL出錯 Manager of pid-file quit without updating file.7. MYSQL(電話號碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)8. 如何單獨(dú)抽取SQL server 2000幫助文件9. MySql遠(yuǎn)程連接的實(shí)現(xiàn)方法10. oracle觸發(fā)器介紹
