簡體   English   中英

如何使用分頁和排序存儲庫搜索多個參數?

[英]How to search with multiple parameters using paging and sorting repository?

我在我的spring項目中使用jquery數據表的分頁和排序存儲庫。 我嘗試使用三個參數進行搜索

@Query(value = "Select p from ProfileBaseInfo p where p.fullName = :fullName or p.firstName = :firstName or p.lastName = :lastName") 
Page<ProfileBaseInfo> search(@Param("fullName") String fullName,@Param("firstName") String firstName,@Param("lastName") String lastName,Pageable pageable);

現在我需要另外用不同的組合(即和/或)搜索另外5個參數。 我根據搜索到的參數生成了dynamic query (即)如果存在參數,我將加入表並插入where條件。 如何在@Query引入動態生成的查詢,還是應該以不同的方式處理它? 現在我想要的是我必須建立一個標准,如"SELECT * FROM profile where name=? and fullName=? and title=? and city=? and state=? and country=?" in ***@Query*** "SELECT * FROM profile where name=? and fullName=? and title=? and city=? and state=? and country=?" in ***@Query*** Annotation中。

我想知道是否還有其他方法可以做,因為表中的列數可能很高。

我認為你應該創建一個Spring bean來實現你的搜索方法( Spring Data )。 @Autowire一些bean與DB(EntityManager或類似的)交談並寫下你需要的任何查詢。

我建議你為你的所有參數使用某種包裝器對象,比如

class Parameters {
    String firstName,
    String fullName;
    ...
    Object paramN;

    <<getters-setters ?>>
}

並使用CriteriaQuery根據參數構造查詢,如

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<ProfileBaseInfo> cq = cb.createQuery(ProfileBaseInfo.class);
Root<ProfileBaseInfo> from = cq.from(ProfileBaseInfo.class);
...
List<Predicate> predicates = Lists.newArrayList();
...
if (param.getFullName() != null) {
     predicates.add(cb.like(cb.lower(ProfileBaseInfo_.fullName)), param.getFullName().toLowerCase() + "%"))
}
...
cb.and(predicates.toArray(new Predicate[predicates.size()]));

因此,您可以根據需要擴展參數列表,而無需使用字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM