我想知道是否可以有条件地忽略查询的一部分(部分不完整)。我在JPA中有查询
@Query("SELECT DISTINCT new it.akademija.wizards.models.document.DocumentGetReviewCommand(" +
"d.author.firstname, d.author.lastname, d.id, d.title, d.description, d.documentType.title, d.submissionDate)" +
" FROM Document d" +
" JOIN d.documentType dt" +
" JOIN dt.reviewUserGroups rug" +
" JOIN rug.users u WHERE u.username = :username" +
" AND d.documentState = it.akademija.wizards.enums.DocumentState.SUBMITTED" +
" AND u <> d.author" +
" **AND (lower(CONCAT(d.author.firstname, ' ', d.author.lastname)) like %:searchFor% " +
" OR lower(d.title) like %:searchFor%" +
" OR lower(d.description) like %:searchFor%" +
" OR lower(d.id) like %:searchFor%" +
" OR lower(dt.title) like %:searchFor%)")
Page<DocumentGetReviewCommand> getDocumentsForReview(@Param(value = "username") String username,
@Param(value = "searchFor") String searchFor,
Pageable pageable);
如果searchFor参数为null或空白(“”),如何忽略下面的部分
AND (lower(CONCAT(d.author.firstname, ' ', d.author.lastname)) like %:searchFor% " +
" OR lower(d.title) like %:searchFor%" +
" OR lower(d.description) like %:searchFor%" +
" OR lower(d.id) like %:searchFor%" +
" OR lower(dt.title) like %:searchFor%)
答案 0 :(得分:0)
您可以尝试添加逻辑,以便在搜索条件为空或空时可以跳过这些条件:
AND
(COALESCE(:searchFor, '') = '' OR
(LOWER(CONCAT(d.author.firstname, ' ', d.author.lastname)) LIKE %:searchFor% OR
LOWER(d.title) LIKE %:searchFor%" OR
LOWER(d.description) LIKE %:searchFor% OR
LOWER(d.id) LIKE %:searchFor% OR
LOWER(dt.title) like %:searchFor%))