Basic Tutorial
Queries
Spring Data JPA Specifications allow you to create dynamic queries with filtering conditions in a flexible way.
Specification<T>
?AND
, OR
).Ensure you have Spring Data JPA dependency and lombok in your pom.xml
:
Let's assume we have a User
entity with fields: id
, name
, email
and age
Table name : user
id | name | age | |
---|---|---|---|
1 | John | john@gmail.com | 25 |
2 | Mike | mike@gmail.com | 35 |
3 | Watson | watson@gmail.com | 40 |
4 | Kenny | kenny@gmail.com | 45 |
Each method returns a Specification<User>
that dynamically applies a filtering condition.
JpaSpecificationExecutor
JpaSpecificationExecutor
for dynamic queries.JpaSpecificationExecutor<User>
allows dynamic querying with Specification<User>
.Once the application is running, test the endpoints:
GET:http://localhost:8080/users/search?name=John
GET:http://localhost:8080/users/search?age=40
GET:http://localhost:8080/users/search?name=John&age=40