FilfetDtoを使用して動的クエリを構築しています ユーザーがUIの一部のフィールドを埋めると、 だから私は唯一の(空でない)充填のクエリフィルタを構築するために、各プロパティをテストする必要がフィールド:動的QueryDSLで構文を単純化し、複数の "if"を避ける方法
JPAQuery dslQuery = new JPAQuery(em);
dslQuery.from(book);
dslQuery.join(book.author, author);
String title = StringUtils.upperCase(StringUtils.trim(_filter.getTitle()));
if (StringUtils.isNotBlank(title)) {
dslQuery.where(book.title.upper().like(title));
}
String isbn = StringUtils.trim(_filter.getIsbn());
if (StringUtils.isNotBlank(isbn)) {
dslQuery.where(book.isbn.like(isbn));
}
if (_filter.getAuthorId() != null) {
dslQuery.where(author.id.eq(_filter.getAuthorId()));
}
は別の、より読みやすい構文で「もし」を抽象化する方法はありますか?
私が何か希望:
JPAQuery dslQuery = new JPAQuery(em);
dslQuery.from(book);
dslQuery.join(book.author, author);
dslQuery.where(book.title.upperIfNotBlank().like(title));
dslQuery.where(book.isbn.likeIfNotNull(isbn));
dslQuery.where(author.id.eqIfNotNull(_filter.getAuthorId()));
「IfNotNull」はオンになって、あるいはデフォルトの動作もすることができればそれはいいだろうが...
ので、それはこのように終わるでしょう。
dslQuery.where(book.title.upper().like(title));
dslQuery.where(book.isbn.like(isbn));
dslQuery.where(author.id.eq(_filter.getAuthorId()));