2014-08-29 235 views
1

我使用jooq進行數據庫操作。我想用自定義where語句編寫select語句。意義條件以地圖形式出現,我通過迭代地圖中的map.data更改時間來創建地點原因。這就是爲什麼我迭代地圖,並創建原因如何在jooq中寫入sql條件

Map =>{fromDate=>2014-05-10,toDate=>2014-06-10,userId=25,type=>STAFF} 

然後創建一個像下面的原因。

where (fromDate="2014-05-10" and toDate="2014-06-10" and userId=25 and type="STAFF") 

所以,我可以寫一個原因內聯。

謝謝 Amila

+0

[爲了記錄在案,這個問題是交叉發佈在jOOQ用戶組](https://groups.google.com/d/msg/jooq-user/Vp69hWJ3G1E/r7naq8DjBpkJ) – 2014-08-29 06:25:08

回答

1

我不是100%肯定,通過創建一個where子句「內聯」的意思。但也許,最好的解決辦法是寫一個效用函數是這樣的:

public static Condition condition(Map<Field<?>, Object> map) { 
    Condition result = DSL.trueCondition(); 

    for (Entry<Field<?>, Object> entry : map.entrySet()) { 
     result = result.and(((Field) entry.getKey()).eq(entry.getValue())); 
    } 

    return result; 
} 

然後,可以「內聯」這一條件到您的jOOQ聲明:

DSL.using(configuration) 
    .select(...) 
    .from(...) 
    .where(condition(map)) 
    .fetch(); 
+0

謝謝盧卡斯。其實這是我想要的 – Amila 2014-09-08 08:03:57