wwz_ henu 2022-02-13 05:19:47 阅读数:490
// select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.name=? and userentity0_.id=0
UserEntity userEntity = new UserEntity();
userEntity.setName("admin");
Example<UserEntity> example = Example.of(userEntity);
List<UserEntity> list = userRepository.findAll(example);
With findAll Methods as an example , Acceptable Example object
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
*/
@Override
<S extends T> List<S> findAll(Example<S> example);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
*/
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
Example The interface structure is as follows , You can use of Method to get the instance
of(T probe) Equate to of(T probe, ExampleMatcher.matching())
/**
* Create a new {@link Example} including all non-null properties by default.
*
* @param probe must not be {@literal null}.
* @return
*/
static <T> Example<T> of(T probe) {
return new TypedExample<>(probe, ExampleMatcher.matching());
}
/**
* Create a new {@link Example} using the given {@link ExampleMatcher}.
*
* @param probe must not be {@literal null}.
* @param matcher must not be {@literal null}.
* @return
*/
static <T> Example<T> of(T probe, ExampleMatcher matcher) {
return new TypedExample<>(probe, matcher);
}
ExampleMatcher The interface structure is as follows
Example
// select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.id=1
UserEntity userEntity = new UserEntity();
userEntity.setId(1);
Example<UserEntity> example = Example.of(userEntity);
List<UserEntity> list = userRepository.findAll(example);
ExampleMatcher.matching() Create a new ExampleMatcher, By default, include all non NULL attribute , To match all predicates derived from this example . because UserEntity in id The initial value is 0, Not for NULL, So there will be id Query of field
// select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.name=? and userentity0_.id=0
UserEntity userEntity = new UserEntity();
userEntity.setName("admin");
Example<UserEntity> example = Example.of(userEntity);
List<UserEntity> list = userRepository.findAll(example);
have access to ExampleMatcher Specify the fields that do not participate in the query
// select userentity0_.id as id1_0_, userentity0_.email as email2_0_, userentity0_.name as name3_0_ from user userentity0_ where userentity0_.name=?
UserEntity userEntity = new UserEntity();
userEntity.setName("admin");
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnorePaths("id");
Example<UserEntity> example = Example.of(userEntity,exampleMatcher);
List<UserEntity> list = userRepository.findAll(example);
have access to ExampleMatcher Other ways to implement slightly more complex queries
copyright:author[wwz_ henu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130519457887.html