Spring Data - Multi-column searches

前端 未结 5 1965
心在旅途
心在旅途 2020-12-01 10:05

I am using Spring Data for the paging and the sorting. However, I would like to perform multi-columns searches.

Now, I am using the annotation @Query in my r

5条回答
  •  一整个雨季
    2020-12-01 10:20

    All above the solutions are great, but we can also use Example and ExampleMatcher for multi column search

    1. First define search object with search parameters
    2. Second, define Custom Example Matcher using ExampleMatcher and Example
    3. Third, use customExampleMatcher in findAll() method
    /* Build Search object */
    Employee employee=new Employee();
            employee.setFirstName(requestDTO.getFilterText());
            employee.setLastName(requestDTO.getFilterText());
            employee.setEmail(requestDTO.getFilterText());
    
    /* Build Example and ExampleMatcher object */
    ExampleMatcher customExampleMatcher = ExampleMatcher.matchingAny()
                    .withMatcher("firstName", ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase())
                    .withMatcher("lastName", ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase())
                    .withMatcher("email", ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase());
    
    Example employeeExample= Example.of(employee, customExampleMatcher);
    
    /* Get employees based on search criteria*/
    employeetRepository.findAll(employeeExample, PageRequest.of(requestDTO.getCurrentPageNumber(), requestDTO.getPageSize(), Sort.by(requestDTO.getSortingOrderColumnName()).descending()));
    
    

提交回复
热议问题