springboot,mybatis,pageHelper and jQuery DataTable
1. Introduction
In previous post I have demostrated how to query the database by page by using springboot, mybatis and pageHelper.
Today I would continue to use springboot myBatis and pageHelper, but I would create a dynamic html page by using JQuery DataTable, and I would demo how to use a spring RestController to support the jQuery DataTable pagination.
The result:
It’s recommended to read the previous post at first. And then you can do the additional task in this post.
2. Environments
SpringBoot 2.0.2+
PageHelper springboot starter 1.2.5
MyBatis springboot starter 1.3.2
DataTable 1.10.16
jQuery 3.3.1
Java 1.8
3. The Pom.xml
Add these dependencies to the pom.xml:
4. The initial data
There are 11 records in the tbl_student table:
We init the table with 11 records to get 2 pages in the DataTable.
7. The domain class
This time we add a DataTableResultInfo to be used by DataTable.
Here we use the lombok @Data annotation to generate getter/setter and constructors for the Student class.
This object would be initialized in the RestController and returned as the html ajax result
8. The app codes
8.1 The app project layout
As you can see, we created a folder named static in the src/main/resources to store the html files
8.2 The Controller class
We create a StudentRestController to produce data for the jQuery DataTable.
We retrieve the start/length/draw params from the request
We call the studentService to get the page result
We adapt the result to DataTableResultInfo object and return it
8.3 The index.html file
In the html file, we would use the DataTable to display our data.
In the html header, we import the jquery and DataTable
In the body, we create a table with some headers
In the javascript, we init the table and call the ajax to get the data from the spring restful service
This is the result:
It’s so easy, do you think so?
The example source code has been uploaded to github, you can visit here to view the example source codes.