DSL request custom binding - more than comparison
What you can do is define your own QueryDSL binding in your repository by expanding QueryDslPredicateExecutor and QuerydslBinderCustomizer :
public interface FooRepository extends CrudRepository<Foo, Integer>, QueryDslPredicateExecutor<Foo>, QuerydslBinderCustomizer<QFoo> { default void customize(final QuerydslBindings bindings, final QFoo foo) { SingleValueBinding<NumberPath<Integer>, Integer> singleBinding = new SingleValueBinding<NumberPath<Integer>,Integer>(){ @Override public Predicate bind(NumberPath<Integer> path, Integer ageValue) { return path.gt(ageValue); } }; bindings.bind(foo.age).first(singleBinding); } }
I am not an expert on DSL request, but I understand the following:
the binding determines how a particular field should be compared with its database column.
Same binding with java 8 lambda: (path, ageValue) -> path.gt(ageValue) . You should read the code in configuration mode in terms of the URL:
select Foos for which the age provided as a parameter is greater than the database value.
DSL request custom binding - between comparisons
Another option is to provide a lower and upper bound for your parameter, for example ?age=10&age=30 . Then define the following binding:
default void customize(final QuerydslBindings bindings, final QFoo foo) { bindings.bind(foo.age).all((path, ageValue) -> { Iterator<? extends Long> it = value.iterator(); return path.between(it.next(), it.next()); }); }
source share