[英]How to translate dynamic query from QueryDSL into JOOQ?
我將 QueryDSL僅用於 Spring Boot 2+ 中的動態查詢,其中 Spring 數據 JPA 應用程序采用以下方式:
@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;
}
我想以類似透明的方式使用 JOOQ,但我不知道如何優雅地做到這一點。 我也喜歡 JOOQ 中沒有類似 QBooks 的生成結構,盡管我認為 JOOQ 也會生成一些表。 無論如何,我很困惑,我無法在網上找到答案,所以我問它是否可以完成以及如何完成。
謝謝
jOOQ 沒有特定的“構建器”來構建您的謂詞。 所有的“建築”API都直接位於謂詞類型上,即Condition
@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)您已經注入了正確配置的DSLContext
2)您正在使用 jOOQ 的代碼生成器
有關更多信息,另請參閱https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.