2014-08-29 231 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