Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8 如何在JOOQ中编写可选where子句_Java 8_Jooq - Fatal编程技术网

Java 8 如何在JOOQ中编写可选where子句

Java 8 如何在JOOQ中编写可选where子句,java-8,jooq,Java 8,Jooq,但是,我也希望能够排除任何id的选项 select * from my_table where ID1=5 and ID2=6 and ID3=7 .... etc 或 问题是第一个“where”子句属于id one,其余的都是and,所以如果我做了一个if语句,删除了where,那么剩下的就只有and了 select * from my_table where ID3=7 List result=DSL.using(configuration()) .选择() .从(我的桌子) .和(M

但是,我也希望能够排除任何id的选项

select * from my_table where ID1=5 and ID2=6 and ID3=7 .... etc 

问题是第一个“where”子句属于id one,其余的都是and,所以如果我做了一个if语句,删除了where,那么剩下的就只有and了

select * from my_table where ID3=7
List result=DSL.using(configuration())
.选择()
.从(我的桌子)
.和(MY_TABLE.ID2.equal(fk_ID2))
.和(MY_TABLE.ID3.equal(fk_ID3))
.orderBy(MY_TABLE.ID.asc())
.限额
.fetchInto(我的桌子)
.map(mapper());
这是行不通的


我试着寻找一些像
where id=*
where*基本上没有过滤器之类的东西,但我找不到任何类似的东西。

你要的东西有时被认为是“SQL‘数字’通配符”,或者如果你搜索的话,至少你可以在网上找到评论

SQL不允许您编写“where id=*”,如果您需要DDL保持静态,您可以通过执行如下范围检查来模拟它

List<MyTable> result = DSL.using(configuration())
                .select()
                .from(MY_TABLE)
                        .and(MY_TABLE.ID2.equal(fk_id2))
                        .and(MY_TABLE.ID3.equal(fk_id3))
                .orderBy(MY_TABLE.ID.asc())
                .limit(limit)
                .fetchInto(MY_TABLE)
                .map(mapper());
从表中选择*
哪里

(my_table.id1>=fk_id1_low和my_table.id1=fk_id2_low和my_table.id2=fk_id3_low和my_table.id3jOOQ让编写SQL感觉它是静态的嵌入式SQL。但它不是。每个jOOQ查询都是一个由表达式树组成的动态SQL查询-你不会注意到它

该方法采用
条件
参数,您不必将其与
WHERE
子句放在一起。您可以在查询之前构造它:

select * from table
  where
    (my_table.id1 >= fk_id1_low and my_table.id1 <= fk_id1_high) and
    (my_table.id2 >= fk_id2_low and my_table.id2 <= fk_id2_high) and
    (my_table.id3 >= fk_id3_low and my_table.id3 <= fk_id3_high)
现在可以将其传递到查询:

Condition condition = DSL.noCondition(); // Alternatively, use trueCondition()
if (something)
    condition = condition.and(MY_TABLE.ID1.equal(pk_id1));
if (somethingElse)
    condition = condition.and(MY_TABLE.ID2.equal(fk_id2));
if (somethingOther)
    condition = condition.and(MY_TABLE.ID3.equal(fk_id3));
列表结果=
DSL.using(configuration())
.选择()
.从(我的桌子)
.何处(条件)
.orderBy(MY_TABLE.ID.asc())
.限额
.fetchInto(我的桌子)
.map(mapper());
DSL
中还有一些实用方法,例如:

  • 从字段/值比较映射构造条件
  • 用于使用该模式
  • 或者仅用于从数组(或集合)构造
    连接条件
手册中也记录了这一点:


请注意,通常情况下,确实不需要直接引用
XYZStep
类型。

我正在尝试,但由于某些原因,trueCondition的计算结果总是为true,因此我的查询只返回所有内容。是否要使用其他条件?@LukeXu:是的,这就是trueCondition的目的…:)您确定要将每个新条件重新分配给变量吗?and()操作创建一个新条件。它不会修改左侧
select * from table
  where
    (my_table.id1 >= fk_id1_low and my_table.id1 <= fk_id1_high) and
    (my_table.id2 >= fk_id2_low and my_table.id2 <= fk_id2_high) and
    (my_table.id3 >= fk_id3_low and my_table.id3 <= fk_id3_high)
Condition condition = DSL.noCondition(); // Alternatively, use trueCondition()
if (something)
    condition = condition.and(MY_TABLE.ID1.equal(pk_id1));
if (somethingElse)
    condition = condition.and(MY_TABLE.ID2.equal(fk_id2));
if (somethingOther)
    condition = condition.and(MY_TABLE.ID3.equal(fk_id3));
List<MyTable> result = 
DSL.using(configuration())
   .select()
   .from(MY_TABLE)
   .where(condition)
   .orderBy(MY_TABLE.ID.asc())
   .limit(limit)
   .fetchInto(MY_TABLE)
   .map(mapper());