Custom Pagination with MyBatis-Plus in Spring Boot

This guide demonstrates how to implement custom pagination queries using MyBatis-Plus within a Spring Boot application.

  1. Project Setup and Dependencies

Ensure your project includes the necessary dependencies in your pom.xml:


<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
       <groupId>com.baomidou</groupId>
       <artifactId>mybatis-plus-boot-starter</artifactId>
       <version>3.4.1</version>
   </dependency>
   <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
   </dependency>
   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
   </dependency>
</dependencies>
   
  1. Application Configuration

Configure your data base connection details in application.yml:


spring:
 datasource:
   url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
   username: root
   password: your_password
   driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
 mapper-locations: classpath*:mapper/*.xml
   
  1. Entity Definition

Create you're entity class, for example, User.java:


package com.example.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("user")
public class User {
   @TableId
   private Long id;
   private String name;
   private Integer age;
   private String email;
}
   
  1. Mapper Interface

Define your mapper interface, extending BaseMapper. Include a method for your custom query.


package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface UserMapper extends BaseMapper<User> {
   /**
    * Custom query for user pagination based on age.
    * @param page The pagination information.
    * @param age The age to filter by.
    * @return A page of users matching the criteria.
    */
   IPage<User> fetchUsersByAge(Page<?> page, @Param("age") Integer age);
}
   
  1. XML Mapper Configuration

In src/main/resources/mapper/UserMapper.xml, define the SQL for your custom queery:


<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="com.example.mapper.UserMapper">
   <select id="fetchUsersByAge" resultType="com.example.entity.User">
       SELECT id, name, age, email
       FROM user
       WHERE age = #{age}
   </select>
</mapper>
   
  1. Service Interface

Define the service interface for business logic related to user operations.


package com.example.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;

public interface IUserService {
   /**
    * Retrieves a paginated list of users filtered by age.
    * @param page The pagination parameters.
    * @param age The age to filter users by.
    * @return A page object containing the user list.
    */
   IPage<User> getUserPageByAge(Page<User> page, Integer age);
}
   
  1. Service Implementation

Implement the service interface, delegating the custom query to the mapper.


package com.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
   
   private final UserMapper userDAO;

   @Autowired
   public UserServiceImpl(UserMapper userDAO) {
       this.userDAO = userDAO;
   }

   @Override
   public IPage<User> getUserPageByAge(Page<User> page, Integer age) {
       // Delegate to the custom mapper method
       return userDAO.fetchUsersByAge(page, age);
   }
}
   
  1. Controller Layer

Create a REST controller to expose the pagination endpoint.


package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

   private final IUserService userService;

   @Autowired
   public UserController(IUserService userService) {
       this.userService = userService;
   }

   @GetMapping("/users")
   public IPage<User> queryUsers(
           @RequestParam(name = "currentPage", defaultValue = "1") int currentPage,
           @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
           @RequestParam(name = "filterAge", required = false) Integer filterAge) {
       
       Page<User> pagination = new Page<>(currentPage, pageSize);
       return userService.getUserPageByAge(pagination, filterAge);
   }
}
   
  1. Application Entry Point

The main application class to start the Spring Boot application.


package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApplicationMain {
   public static void main(String[] args) {
       SpringApplication.run(ApplicationMain.class, args);
   }
}
   
  1. Testing the Endpoint

Start your Spring Boot application and access the endpoint to test custom pagination. For example:

http://localhost:8080/users?currentPage=1&pageSize=10&filterAge=25

Tags: Spring Boot mybatis-plus Pagination Custom Query java

Posted on Fri, 21 Aug 2026 16:50:24 +0000 by TweetyPie