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

Java 如何将动态查询从QueryDSL转换为JOOQ?,java,spring-data-jpa,jooq,querydsl,Java,Spring Data Jpa,Jooq,Querydsl,我在Spring Boot 2+和Spring Data JPA应用程序中以以下方式使用QueryDSLjust进行动态查询: @Override public Iterable<Books> search(String bookTitle, String bookAuthor, String bookGenre) { BooleanBuilder where = dynamicWhere(bookTitle, bookAuthor, bookGenre); return b

我在Spring Boot 2+和Spring Data JPA应用程序中以以下方式使用QueryDSLjust进行动态查询:

@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;
}
@覆盖
公共可编辑搜索(字符串图书标题、字符串图书作者、字符串图书类型){
BooleanBuilder,其中=dynamicWhere(书名、作者、图书类型);
return booksRepository.findAll(其中,orderByTitle());
}
public BooleanBuilder dynamicWhere(字符串图书标题、字符串图书作者、字符串图书类型){
QBooks QBooks=QBooks.books;
BooleanBuilder,其中=新建BooleanBuilder();
if(bookTitle!=null){
其中和(qBooks.title.equalsIgnoreCase(bookTitle));
}
如果(bookAuthor!=null){
其中和(qBooks.author.eq(bookAuthor));
}
if(bookGenre!=null){
其中。和(qBooks.genre.eq(bookGenre));
}
返回哪里;
}
我想以类似的透明方式使用JOOQ,但我不知道如何优雅地使用它。我也喜欢JOOQ中没有像生成的构造那样的QBooks,尽管我认为JOOQ也生成一些表。总之,我很困惑,我在网上找不到答案,所以我在问,它能不能做,怎么做


谢谢

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

@覆盖
公共可编辑搜索(字符串图书标题、字符串图书作者、字符串图书类型){
条件where=dynamicWhere(书名、书籍作者、书籍类型);
返回上下文。从(书本)中选择
.where(where)
.orderBy(书籍.标题)
.进入(书本、课堂);
}
公共条件动态此处(字符串图书标题、字符串图书作者、字符串图书类型){
条件,其中=DSL.noCondition();
if(bookTitle!=null){
其中=其中和(BOOKS.TITLE.equalsIgnoreCase(bookTitle));
}
如果(bookAuthor!=null){
where=where.和(BOOKS.AUTHOR.eq(bookAuthor));
}
if(bookGenre!=null){
where=where.和(BOOKS.GENRE.eq(bookGenre));
}
返回哪里;
}
这是假设:

1) 您已注入正确配置的 2) 你正在使用jOOQ的代码生成器


有关更多信息,请参见jOOQ没有用于构造谓词的特定“生成器”。所有“building”API都直接位于谓词类型上,即

@覆盖
公共可编辑搜索(字符串图书标题、字符串图书作者、字符串图书类型){
条件where=dynamicWhere(书名、书籍作者、书籍类型);
返回上下文。从(书本)中选择
.where(where)
.orderBy(书籍.标题)
.进入(书本、课堂);
}
公共条件动态此处(字符串图书标题、字符串图书作者、字符串图书类型){
条件,其中=DSL.noCondition();
if(bookTitle!=null){
其中=其中和(BOOKS.TITLE.equalsIgnoreCase(bookTitle));
}
如果(bookAuthor!=null){
where=where.和(BOOKS.AUTHOR.eq(bookAuthor));
}
if(bookGenre!=null){
where=where.和(BOOKS.GENRE.eq(bookGenre));
}
返回哪里;
}
这是假设:

1) 您已注入正确配置的 2) 你正在使用jOOQ的代码生成器

有关更多信息,请参见

@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;
}