Example 1
@Test
public void testSingle() {
// tag::example-mono[]
Employee e = new Employee();
e.setFirstName("Bilbo");
Example<Employee> example = Example.of(e);
// end::example-mono[]
// tag::query-mono[]
Mono<Employee> singleEmployee = repository.findOne(example);
// end::query-mono[]
StepVerifier.create(singleEmployee)
.expectNextMatches(employee -> {
assertThat(employee).hasNoNullFieldsOrProperties();
assertThat(employee.getFirstName()).isEqualTo("Bilbo");
assertThat(employee.getLastName()).isEqualTo("Baggins");
assertThat(employee.getRole()).isEqualTo("burglar");
return true;
})
.expectComplete()
.verify();
}
Example 2
@Test
public void testSingle() {
// tag::example-mono[]
Employee e = new Employee();
e.setFirstName("Bilbo");
Example<Employee> example = Example.of(e);
// end::example-mono[]
// tag::query-mono[]
Mono<Employee> singleEmployee = repository.findOne(example);
// end::query-mono[]
StepVerifier.create(singleEmployee)
.expectNextMatches(employee -> {
assertThat(employee).hasNoNullFieldsOrProperties();
assertThat(employee.getFirstName()).isEqualTo("Bilbo");
assertThat(employee.getLastName()).isEqualTo("Baggins");
assertThat(employee.getRole()).isEqualTo("burglar");
return true;
})
.expectComplete()
.verify();
}
Example 3
@Test
public void testSingleWithTemplate() {
// tag::mono-template[]
Employee e = new Employee();
e.setFirstName("Bilbo");
Example<Employee> example = Example.of(e);
Mono<Employee> singleEmployee = operations.findOne(
new Query(byExample(example)), Employee.class);
// end::mono-template[]
StepVerifier.create(singleEmployee)
.expectNextMatches(employee -> {
assertThat(employee).hasNoNullFieldsOrProperties();
assertThat(employee.getFirstName()).isEqualTo("Bilbo");
assertThat(employee.getLastName()).isEqualTo("Baggins");
assertThat(employee.getRole()).isEqualTo("burglar");
return true;
})
.expectComplete()
.verify();
}
Example 4
@Test
public void substringMatching() {
Example<Person> example = Example.of(new Person("er", null, null), matching().//
withStringMatcher(StringMatcher.ENDING));
assertThat(repository.findAll(example), hasItems(skyler, walter));
}
Example 5
@Test
public void testFindByFoodNameWithExampleMatchers(){
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnorePaths("id")
.withIgnorePaths("transactionDate");
Example<Eats> example = Example.of(eat, matcher);
List<Eats> response = eatsRepository.findByFoodName(testFood);
Eats responseExample = Example.of(response.get(0), matcher).getProbe();
log.info("response: " + responseExample.toString());
log.info("repo: " + eatsRepository.findOne(example));
assert(eatsRepository.findOne(example).equals(responseExample));
}
Example 6
@Test
public void testMultiple() {
// tag::example-flux[]
Employee e = new Employee();
e.setLastName("baggins"); // Lowercase lastName
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnoreCase()
.withMatcher("lastName", startsWith())
.withIncludeNullValues();
Example<Employee> example = Example.of(e, matcher);
// end::example-flux[]
// tag::query-flux[]
Flux<Employee> multipleEmployees = repository.findAll(example);
// end::query-flux[]
StepVerifier.create(multipleEmployees.collectList())
.expectNextMatches(employees -> {
assertThat(employees).hasSize(2);
assertThat(employees).extracting("firstName")
.contains("Frodo", "Bilbo");
return true;
})
.expectComplete()
.verify();
}
Example 7
@Test
public void matchStartingStringsIgnoreCase() {
Example<User> example = Example.of(new User("Walter", "WHITE", null),
matching().//
withIgnorePaths("age").//
withMatcher("firstname", startsWith()).//
withMatcher("lastname", ignoreCase()));
assertThat(repository.findAll(example), hasItems(flynn, walter));
}
Example 8
@Test
public void matchStartingStringsIgnoreCase() {
Example<Person> example = Example.of(new Person("Walter", "WHITE", null),
matching().//
withIgnorePaths("age").//
withMatcher("firstname", startsWith()).//
withMatcher("lastname", ignoreCase()));
assertThat(repository.findAll(example), hasItems(flynn, walter));
}
Example 9
@Override
public SysUserBaseInfo findByName(String userName) {
SysUserBaseInfo info = new SysUserBaseInfo();
info.setUserName(userName);
Example<SysUserBaseInfo> example = Example.of(info);
SysUserBaseInfo sourceCodes = sysUserBaseDao.findOne(example);
return sourceCodes;
}
Example 10
@Test
public void configuringMatchersUsingLambdas() {
Example<Person> example = Example.of(new Person("Walter", "WHITE", null),
matching().//
withIgnorePaths("age"). //
withMatcher("firstname", matcher -> matcher.startsWith()). //
withMatcher("lastname", matcher -> matcher.ignoreCase()));
assertThat(operations.find(query(byExample(example)), Person.class), hasItems(flynn, walter));
}
Example 11
@Test
public void substringMatching() {
Example<User> example = Example.of(new User("er", null, null), matching().//
withStringMatcher(StringMatcher.ENDING));
assertThat(repository.findAll(example), hasItems(skyler, walter));
}
Example 12
@Test
public void countBySimpleExample() {
Example<User> example = Example.of(new User(null, "White", null));
assertThat(repository.count(example), is(3L));
}
Example 13
@Test
public void ignorePropertiesAndMatchByAge() {
Example<User> example = Example.of(flynn, matching().//
withIgnorePaths("firstname", "lastname"));
assertThat(repository.findOne(example), is(flynn));
}
Example 14
@Test
public void valueTransformer() {
Example<User> example = Example.of(new User(null, "White", 99), matching(). //
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))));
assertThat(repository.findAll(example), hasItems(walter));
}
Example 15
@Override
public List<SysColumnInfo> findAllBySysColumnInfo(SysColumnInfo info) {
Example<SysColumnInfo> example = Example.of(info);
return sysColumnDao.findAll(example);
}
Example 16
@Override
public List<SysTableInfo> findAll(SysTableInfo sysTableInfo) {
Example<SysTableInfo> example = Example.of(sysTableInfo);
return sysTableDao.findAll(example);
}
Example 17
public ExampleFilter(T probe, ExampleMatcher exampleMatcher) {
example = Example.of(probe, exampleMatcher);
}
Example 18
@GraphQLQuery(name = "people")
public List<Person> getPeople(@GraphQLArgument(name = "Person", description = "Find people by id, first name, or last name") Person person) {
if(person == null) person = new Person();
Example<Person> example = Example.of(person);
List<Person> people = personRepository.findAll(example);
return StreamSupport.stream(people.spliterator(), false).collect(Collectors.toList());
}
Example 19
@Override
public Page<SysUserRolesInfo> findAllBySearchText(int pageNumber, int pageSize, SysUserRolesInfo info) {
Example<SysUserRolesInfo> example = Example.of(info);
Sort sort = new Sort(new Order(Direction.ASC, "createTime"));
PageRequest request = new PageRequest(pageNumber - 1, pageSize, sort);
Page<SysUserRolesInfo> sourceCodes = sysUserRolesDao.findAll(example, request);
return sourceCodes;
}
Example 20
@Override
public Page<SysDepartmentInfo> findAllBySearchText(int pageNumber, int pageSize, SysDepartmentInfo info) {
Example<SysDepartmentInfo> example = Example.of(info);
Sort sort = new Sort(new Order(Direction.DESC, "createTime"));
PageRequest request = new PageRequest(pageNumber - 1, pageSize, sort);
Page<SysDepartmentInfo> sourceCodes = sysDepartmentDao.findAll(example, request);
return sourceCodes;
}
matching / matchingAll
Item item = new Item();
item.setName("kitchen");
item.setPrice(800);
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("name", match -> match.ignoreCase().startsWith());
Example<Item> example = Example.of(item, matcher);
List<Item> items = repository.findAll(example);
items.forEach(System.out::println);
matchingAny
Item item = new Item();
item.setName("kitchen");
item.setPrice(800);
ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withMatcher("name", match -> match.ignoreCase().startsWith());
Example<Item> example = Example.of(probe, matcher);
List<Item> items = repository.findAll(example);
items.forEach(System.out::println);
withIgnorePaths
ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withMatcher("name", match -> match.ignoreCase().startsWith())
.withIgnorePaths("id", "price", "standardType");
withIncludeNullValues
ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withMatcher("name", match -> match.ignoreCase().startsWith())
.withIncludeNullValues();
本文介绍如何利用Spring Data的Example特性进行灵活查询,包括单个对象查询、多个对象查询及复杂匹配条件设置等。
851

被折叠的 条评论
为什么被折叠?



