Spring Data Criteria
extends Spring Data JDBC & Spring Data R2DBC & Spring Data JPA to support dynamic sql.
to see DATAJDBC-319
, DATAJPA-2724
.
The usage is as follows:
public interface UserDao extends ListCrudRepository<User, Long>, CriteriaExecutor<User> {
default Page<User> searchByQuery(UserQuery query, Pageable pageable) {
return findAll(Criteria.from(eq(User_.province, query.province))
.and(eq(User_.city, query.city))
.and(like(User_.area, query.area))
.and(like(User_.name, query.nick))
.and(between(User_.created, query.createFrom, query.createTo))
, pageable);
}
}
It will dynamically generate sql based on whether the query field is empty.