当前位置: 首页 > 知识库问答 >
问题:

如何将动态查询从QueryDSL转换为JOOQ?

郭远
2023-03-14
@Override
public Iterable<Books> search(String bookTitle, String bookAuthor, String bookGenre) {
  BooleanBuilder where = dynamicWhere(bookTitle, bookAuthor, bookGenre);
  return booksRepository.findAll(where, orderByTitle());
}

public BooleanBuilder dynamicWhere(String bookTitle, String bookAuthor, String bookGenre) {
  QBooks qBooks = QBooks.books;
  BooleanBuilder where = new BooleanBuilder();
  if (bookTitle != null) {
    where.and(qBooks.title.equalsIgnoreCase(bookTitle));
  }
  if (bookAuthor!= null) {
    where.and(qBooks.author.eq(bookAuthor));
  }
  if (bookGenre!= null) {
    where.and(qBooks.genre.eq(bookGenre));
  }
  return where;
}

谢谢

共有1个答案

宋烨烁
2023-03-14

jOOQ没有一个特定的“构建器”来构建谓词。所有“构建”API都直接位于谓词类型上,即条件

@Override
public Iterable<Books> search(String bookTitle, String bookAuthor, String bookGenre) {
  Condition where = dynamicWhere(bookTitle, bookAuthor, bookGenre);
  return dslContext.selectFrom(BOOKS)
                   .where(where)
                   .orderBy(BOOKS.TITLE)
                   .fetchInto(Books.class);
}

public Condition dynamicWhere(String bookTitle, String bookAuthor, String bookGenre) {
  Condition where = DSL.noCondition();
  if (bookTitle != null) {
    where = where.and(BOOKS.TITLE.equalsIgnoreCase(bookTitle));
  }
  if (bookAuthor!= null) {
    where = where.and(BOOKS.AUTHOR.eq(bookAuthor));
  }
  if (bookGenre!= null) {
    where = where.and(BOOKS.GENRE.eq(bookGenre));
  }
  return where;
}

这是假设:

1)您已经注入了正确配置的DSLContext2)您正在使用Jooq的代码生成器

 类似资料:
  • 问题内容: MySQL是否可以将列转换为行,从而动态添加行所需的列数。我认为我的问题可能与数据透视表有关,但是我不确定,除了给出以下示例之外,我不知道如何构造此问题。 给定两个表A和B,它们看起来像 表A 我想编写一个如下查询: 结果表 基本上,我想将表B中的每一行变成结果表中的一列。如果有一个新条目被添加到表B中,id = 1,那么我希望结果表自动扩展一列以容纳这个额外的数据点。 问题答案: 您

  • 假设我有一个生成的实体,如下所示: 我的输入值是字段名称(“可用性”、“生日”、“CVID”...)和一个字符串值,我应该使用它对所有字段执行“like”。 我想从以下字段名开始构建一个查询: null 我试图使用PathBuilder,但似乎要使用“getString或getBoolean”之类的方法,就必须知道要提取的字段的类型。在我的例子中,由于我只是从字段名开始,所以我不能使用这些方法,也

  • 我使用了cakephp Mysql到mongodb查询组件,即将Mysql查询转换到mongodb中,但是当查询有多个括号时代码停止工作,我还尝试将http://www.querymongo.com/site上的查询转换为相同的问题,

  • 我想在JPA 2.1中将“命名查询”转换为“SQL查询”,并在运行之前对其进行更改。 例如,我有一个命名查询:从CU所在的客户中选择CU。代码=?1,我希望在PAR之后获得查询并对其进行转换和编辑(例如添加模式名称)并创建此查询:从db1.cc1cust cu中选择*其中cu.cc1cod=?1. 我该怎么做? 问候

  • 问题内容: 我需要将此查询从MySQL格式转换为SQLite。我正在尝试自己,但发现了一些困难。 在SQLite中,curdate()和interval函数不存在。 问题答案: 这是基本语法: 我忽略了该子句,因为这些日期是过去1000多个日期,因此他们无论如何都不会选择任何内容。

  • 问题内容: 我正在处理数据透视表查询。架构如下 Sno,名称,地区 同一名称可能在许多地区出现,例如以样本数据为例 如您所见,我有一组4个不同的地区(CA,JB,MN,LP)。现在,我想通过将名称映射到区域来获取为其生成的数据透视表 我为此写了以下查询 但是,地区可能会增加,在这种情况下,我将不得不再次手动编辑查询并向其中添加新的地区。 我想知道是否存在一个查询,该查询可以动态获取不同地区的名称并