MyBatis and PageHelper example
1. Introduction
This post demonstrates how to query a database with pagination using Spring Boot, MyBatis, and the PageHelper plugin.
2. Environments
- Spring Boot 2.0.2+
- PageHelper Spring Boot Starter 1.2.5
- MyBatis Spring Boot Starter 1.3.2
- Java 1.8
3. The Pom.xml
Spring Boot version:
All dependencies:
4. The dependencies explanation
4.1 The PageHelper
PageHelper is an open-source project designed to simplify pagination in Spring Boot and MyBatis applications. It functions as a MyBatis plugin.
For detailed usage instructions, refer to this article.
4.2 The Lombok
Lombok is an IDE plugin that helps reduce boilerplate code. In this post, we use Lombok to generate getters and setters for the domain object.
5. The architecture of this app
- PageHelper is a MyBatis plugin.
- MyBatis has a Spring Boot starter.
6. The database, table, and initial data
For demonstration purposes, I set up a local database as follows:
There is a table tbl_student
in the database:
Insert five records into the table:
7. The domain class Student
- The
@Data
annotation from Lombok generates getters, setters, and constructors for theStudent
class.
8. The app codes
8.1 The app code layers
- Service layer:
StudentService
, which supports finding students by page. - DAO layer: A MyBatis mapper interface.
- XML layer: A MyBatis XML file defining SQL queries.
8.2 The app project layout
- A folder named
mybatis
is created insrc/main/resources
.
8.3 The application.properties
Add these properties to your application.properties
:
8.4 The mapper XML
The StudentMapper.xml
content:
- A
resultMap
andcolumnList
are defined for reuse in SQL queries. - The
findByPage
SQL query matches the mapper interface. - Pagination is handled by the PageHelper plugin, so no
LIMIT
clause is needed.
8.5 The mapper interface
The StudentMapper
interface:
- The
findByPage
method has no parameters; pagination parameters are set elsewhere.
8.6 The service class
The StudentService
class:
PageHelper.startPage
sets the page number and size.- The mapper executes the paginated query.
8.7 The test case
The StudentServiceTest
class:
- With 5 records in the table, querying the first page with a page size of 2 should return a total count of 5 and 3 pages.
- Lombok’s
@CommonsLog
generates alog
object for the test case.
Running the test case produces a green bar:
The console output:
9. The com.github.pagehelper.Page class
After calling studentService.findByPage
, a Page<Student>
object is returned. Its structure is as follows:
Summary
This post demonstrated how to implement pagination in a Spring Boot application using MyBatis and the PageHelper plugin. Key steps included configuring the build.gradle
file, setting up the database, and writing service and mapper classes. The PageHelper plugin simplifies pagination by eliminating the need for manual LIMIT
clauses in SQL queries. The example source code is available on GitHub.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!