Example 查询

2020-03-13  本文已影响0人  你说这个谁懂啊
TestEntity entity = new TestEntity();
entity.setFieldA(str);
Example<TestEntity> 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 实例查询

上一篇下一篇

猜你喜欢

热点阅读