是否可以将按示例查询与spring数据方法结合使用?

时间:2019-03-28 11:04:38

标签: java spring spring-data query-by-example

我有一个Template类,其中有几个字段(id,name,description,created,modified....)

,并可以按如下所示的创建日期进行过滤:

public interface TemplateRepository extends JpaRepository<Template, Long> {
    Page<Template> findAllByCreatedBetween(OffsetDateTime createdStart, OffsetDateTime createdEnd, Pageable pageable);
}

我的示例如下:

ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll()
                .withIgnoreCase()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreNullValues()
                .withIgnorePaths("id");
Example<Template> templateExample = Example.of(requestTemplate, exampleMatcher);        

是否可以将示例添加到findAllByCreatedBetween方法中?

1 个答案:

答案 0 :(得分:0)

@Test
public void givenPassengers_whenFindByExampleCaseInsensitiveMatcher_thenExpectedReturned() {
    ExampleMatcher caseInsensitiveExampleMatcher = ExampleMatcher.matchingAll().withIgnoreCase();
    Example<Passenger> example = Example.of(Passenger.from("fred", "bloggs", null),
      caseInsensitiveExampleMatcher);

    Optional<Passenger> actual = repository.findOne(example);

    assertTrue(actual.isPresent());
    assertEquals(Passenger.from("Fred", "Bloggs", 22), actual.get());
}

查看此链接:

https://www.baeldung.com/spring-data-query-by-example