Please do read about the Chenile Query framework before you read this tutorial. That would give you some context on what is Chenile Mybatis Query. As we saw there, Chenile already has a configured query to fetch data from a query database using Mybatis. We can configure new queries to execute in Mybatis. No code needed!
We will see how to do this in this article. Please download the student-query-service from chenile samples
Understanding the configurations in student-query-service
You will find that there is no code in src/main/java. We straightaway skip to configurations at src/main/resources. By convention, we use com.{companyname}.{orgname}.query.service.mapper to keep the configuration files. The code generator uses that convention to generate the code. For the samples, we used org.chenile.samples instead of com.{companyname}.{orgname}.
You will find two files. One is a typical Mybatis mapper file and the other one is a JSON file that contains the query meta data. Any query in Chenile needs to be configured using a query meta data file.
The JSON file
First let us look at the JSON.
As you see , the query meta data is not tied to Mybatis. You can use this to configure any query in Chenile. We will support other types of queries in the future using the same meta data. Here we will use the id to map the query to mybatis. The name will be visible to the user. All requests to the getAll query would be made to /q/students. This would internally map it to the Mybatis Srudents.getAll query that will be defined in the mapper file. The column meta data is useful and will be passed verbatim in SearchResponse so that the UI can use this information to display the screen as stated in the page on query framework
It is important to explicitly enable pagination and sorting at the query level. Once the query is defined we can write the corresponding Mybatis mapper file.
The Mapper file
Here we define the getALl query in the student namespace. A few observations:
- getAll is a paginated query. Since paginated queries need to explicitly return maxPages and maxCount it is important to define a “count query” as well. The count query is called getAll-count by convention. This allows Chenile to know where to look for it.
- The orderby and pagination variables need to be added verbatim at the end of the getAll query (not the count query). This allows pagination and sorting. The paginated and sortable fields in the query meta data (in the JSON) must be set to true.
- The count query has exactly the same where clause as the getAll query.
- like filters are treated using the key word like as shown above
- between filters need to be treated using upper bound and lower bound (not shown in this example)
- contains filters must have the foreach loop as shown above
With these two files in place, you are all set. Please see the src/test/ for the testcases and feature files.