Copied from https://github.com/ZhongjunTian/spring-repository-plus This is actually a good implementation of spring JPA Specification interface, it makes front-end having full control of filtering data with very little back-end code.
Querying with spring-repository-plus is as simple as:
or
Here is ALL your back-end code
@PostMapping("/persons")
public List<Person> filter(@RequestBody Filter filter){
return selectFrom(personRepository).leftJoin("address").where(filter).findAll();
}
public interface PersonRepository extends JpaRepository<Person, Long>,JpaSpecificationExecutor {
}
But with such little code, you can also filter data by ANY complex logic (and, or) and ANY operations (equal, startswith, endswith, greaterthan...)
The example below is equivalent to lastName='Tian' AND (address.city='Dallas' OR address.city like 'San%')
or
Table definition
CREATE TABLE PERSON (
id BIGINT GENERATED BY DEFAULT AS IDENTITY,
first_name varchar(255) not null,
last_name varchar(255) not null,
address_id int null
);
CREATE TABLE ADDRESS (
id BIGINT GENERATED BY DEFAULT AS IDENTITY,
city varchar(255) not null
);
Maven Build:
mvn clean package deploy clean