Example 查询

TestEntity entity = new TestEntity();
entity.setFieldA(str);
Example example = Example.of(entity);
testRepository.findAll(example);

// 当心Entity中的非Null属性都会成为Where条件,属性应全部使用包装类型,并避免赋予初值,或使用ExampleMatcher的withIgnorePaths忽略改属性。
class TestEntity { String fieldA; String fieldB = 'x'; int fieldC; Boolean fieldD }
TestEntity entity = new TestEntity();
entity.setFieldA(str);
// where fieldA = 'str' and fieldB = 'x' and fieldC = 0
testRepository.findAll(Example.of(entity));
// where fieldA = 'str'
testRepository.findAll(Example.of(entity, 
    ExampleMatcher.matching().withIgnorePaths("fieldB").withIgnorePaths("fieldC"));

Query By Example
Spring Data JPA 实例查询

你可能感兴趣的:(Example 查询)