This is a sample repository that offers a simplified implementation of
the smartsensesolutions-commons-dao repository.
It contains implementation for Books and Authors entities to showcase how
the smartsensesolutions-commons-dao can facilitate the
search functionality.
With the SpecificationUtils
provided in the library, implementing pagination in a query and performing various types of
queries becomes easy.
Here, we use Gradle as the build tool and add below dependency to our build.gradle file.
implementation group: 'com.smartsensesolutions', name: 'commons-dao', version: '0.0.2'
The BaseEntity
, BaseRepository
and BaseService
classes from the above package must be extended to Entity
,
Repository
and Service
classes, respectively .
This project contains a few endpoints that provide a mechanism to save new objects and query them using the common-dao package.
- Add new data:
curl --location '::8080/create/books' \
--header 'Content-Type: application/json' \
--data '[
{
"authorName": "AuthorName",
"age": 25,
"books": [
{
"bookName": "Spring Automation 2022",
"description": "Basic Automation setup from Zero to Hero with SpringBoot 2022."
}
]
}
]'
-
Filter the data with different criteria where
column
indicates the variable name from your entity class and theoperator
value comes from here. Thevalues
parameter contains a list of search parameter in String format. To search, use below POST endpoint with the mentioned request bodies based on the different scenarios:::8080/search
-
Get Paginated response:
{ "page": 0, "size": 10 }
-
Search author based on the author name (equals query):
{ "page": 0, "size": 10, "criteria": [ { "column": "authorName", "operator": "=", "values": [ "Ziemer Miller" ] } ] }
Prepared Query by SpecificationUtils:
select a1_0.id,a1_0.age,a1_0.author_name,a1_0.created_at from public.author a1_0 where a1_0.author_name=? offset ? rows fetch first ? rows only
-
Search author based on the author name which contains author name (like query):
{ "page": 0, "size": 10, "criteria": [ { "column": "authorName", "operator": "like", "values": [ "Miller" ] } ] }
Prepared Query by SpecificationUtils:
select a1_0.id,a1_0.age,a1_0.author_name,a1_0.created_at from public.author a1_0 where (cast(a1_0.author_name as text) like ? escape '') offset ? rows fetch first ? rows only
-
Search author based on age:
{ "page": 0, "size": 10, "criteria": [ { "column": "age", "operator": "=", "values": [ "25" ] } ] }
Prepared Query by SpecificationUtils:
select a1_0.id,a1_0.age,a1_0.author_name,a1_0.created_at from public.author a1_0 where a1_0.age=? offset ? rows fetch first ? rows only
-
Search based on the book's name and author's name (like with join-equals):
{ "page": 0, "size": 10, "criteria": [ { "column": "authorName", "operator": "like", "values": [ "Thomson" ] }, { "column": "books_bookName", "operator": "join_eq", "values": [ "Microservice with spring boot" ] } ] }
Prepared Query by SpecificationUtils:
select a1_0.id,a1_0.age,a1_0.author_name,a1_0.created_at from public.author a1_0 join (public.author_books_mapping b1_0 join public.books b1_1 on b1_1.id=b1_0.book_id) on a1_0.id=b1_0.author_id where (cast(a1_0.author_name as text) like ? escape '') and b1_1.book_name=? offset ? rows fetch first ? rows only
-
Search based on the boolean valued from the table:
{ "page": 0, "size": 10, "criteria": [ { "column": "active", "operator": "is_true" } ] }
Prepared Query by SpecificationUtils:
select a1_0.id,a1_0.active,a1_0.age,a1_0.author_name,a1_0.created_at from public.author a1_0 where a1_0.active offset ? rows fetch first ? rows only
-
Search Books based on the BookName with active author with
OR
criteriaOperator. (Default criteriaOperator is AND){ "page": 0, "size": 10, "criteriaOperator": "OR", "criteria": [ { "column": "active", "operator": "is_true" }, { "column": "books_bookName", "operator": "join_like", "values": [ "Microservice" ] } ] }
Prepared Query by SpecificationUtils:
select a1_0.id,a1_0.active,a1_0.age,a1_0.author_name,a1_0.created_at from public.author a1_0 left join (public.author_books_mapping b1_0 join public.books b1_1 on b1_1.id=b1_0.book_id) on a1_0.id=b1_0.author_id where a1_0.active or cast(b1_1.book_name as text) like ? escape '' offset ? rows fetch first ? rows only
-