In this tutorial we will learn how to integrate Querydsl into Spring web MVC.
com.querydsl.core.types.Predicate (last tutorial) can be used as controller method parameter which is derived from the attributes contained in a Request query string. For example query string like this:
employees?name=Mike
is equivalent to:
QEmployee.employee.name("Mike")
which can be mapped to Predicate as a controller method parameter:
@RestController
public class EmployeeController{
@Autowired
private EmployeeRepository repository;
@GetMapping("/employee")
public List<Employee> getEmployees(Predicate predicate) {
return repository.findAll(predicate);
}
...
}
Using @QuerydslPredicate annotation
The controller method's return type (in above example List<Employee>) is used to resolve the entity type. In case if return type is different than the entity type (or collection of that) then we should use @QuerydslPredicate . For example:
@RestController
public class EmployeeController{
@Autowired
private EmployeeRepository repository;
@GetMapping("/employee")
public String getEmployees(@QuerydslPredicate(root = Employee.class) Predicate predicate) {
return repository.findAll(predicate).toString();
}
...
}
QuerydslPredicateArgumentResolver
This resolver allows the use of Predicate into Spring MVC controller methods. This is enabled when @EnableSpringDataWebSupport is used. Querydsl should also be on the classpath.
Example
Entity
@Entity
public class Employee {
private @Id
@GeneratedValue
Long id;
private String name;
private String dept;
private int salary;
.............
}
Repository
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long>,
QuerydslPredicateExecutor<Employee> {
}
MVC controller
@RestController
public class EmployeeController {
@Autowired
private EmployeeRepository repository;
@GetMapping("/employees")
public List<Employee> getEmployees(Predicate predicate) {
Iterable<Employee> iterable = repository.findAll(predicate);
List<Employee> employees = StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
return employees;
}
@GetMapping("/employeesAsString")
public String getEmployeesAsString(@QuerydslPredicate(root = Employee.class) Predicate predicate) {
Iterable<Employee> iterable = repository.findAll(predicate);
List<Employee> employees = StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
return employees.toString();
}
}
Running
To try examples, run embedded tomcat (configured in pom.xml of example project below):
mvn tomcat7:run-war
Output
/employess (without any query parameters) will return all employees:
/employees?dept=IT
/employees?dept=IT&dept=Admin
/employeesAsString?salary=3000
Example ProjectDependencies and Technologies Used: - spring-data-jpa 2.0.7.RELEASE: Spring Data module for JPA repositories.
Uses org.springframework:spring-context version 5.0.6.RELEASE - spring-webmvc 5.0.6.RELEASE: Spring Web MVC.
- querydsl-apt 4.2.1: APT based Source code generation for Querydsl.
- querydsl-jpa 4.2.1: JPA support for Querydsl.
- jackson-databind 2.9.5: General data-binding functionality for Jackson: works on core streaming API.
- javax.servlet-api 3.0.1 Java Servlet API
- hibernate-core 5.3.1.Final: Hibernate's core ORM functionality.
Implements javax.persistence:javax.persistence-api version 2.2 - h2 1.4.197: H2 Database Engine.
- JDK 1.8
- Maven 3.3.9
|