MyBatis-Plus分页插件

MyBatis-Plus 是一个 MyBatis 的增强工具,在简化开发、提高效率方面有着显著的优势。MyBatis-Plus 提供了分页插件(Pagination Interceptor),可以方便地实现分页查询。在使用分页插件自定义查询方法时,可以按照以下步骤进行:

1. 引入依赖

首先,在 pom.xml 文件中引入 MyBatis-Plus 相关的依赖:

XML 复制代码
<dependencies>
    <!-- MyBatis-Plus 核心依赖 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>最新版本号</version>
    </dependency>
    <!-- MySQL 连接池(示例) -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>最新版本号</version>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

2. 配置分页插件

在 MyBatis-Plus 的配置类中配置分页插件:

java 复制代码
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
复制代码
3. 创建实体类和 Mapper 接口

创建对应的实体类和 Mapper 接口。例如,有一个 User 表:

java 复制代码
// User.java
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;
    // 其他字段...
}

// UserMapper.java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    // 自定义分页查询
    @Select("SELECT * FROM user WHERE age > #{age}")
    IPage<User> selectUserByAge(Page<?> page, int age);
}

4. 使用分页查询

在 Service 层或者 Controller 层使用分页查询:

java 复制代码
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public IPage<User> getUserByAge(int currentPage, int pageSize, int age) {
        // 创建分页对象
        Page<User> page = new Page<>(currentPage, pageSize);
        // 调用自定义分页查询方法
        return userMapper.selectUserByAge(page, age);
    }
}

5. 调用分页查询

在 Controller 层或者其他调用位置调用分页查询方法:

java 复制代码
import com.baomidou.mybatisplus.core.metadata.IPage;
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 {

    @Autowired
    private UserService userService;

    @GetMapping("/users/byAge")
    public IPage<User> getUserByAge(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam int age) {
        return userService.getUserByAge(currentPage, pageSize, age);
    }
}
复制代码
总结
  1. 引入依赖:确保项目中包含 MyBatis-Plus 和数据库连接池依赖。
  2. 配置分页插件 :在配置类中注册 PaginationInterceptor
  3. 创建实体类和 Mapper 接口:定义实体类和 Mapper 接口,并在 Mapper 接口中添加自定义分页查询方法。
  4. 使用分页查询:在 Service 层中调用自定义分页查询方法。
  5. 调用分页查询:在 Controller 层或其他位置调用 Service 层的方法。
相关推荐
不恋水的雨4 小时前
mybatis-plus保存数据实现公共字段自动填充
mybatis
MegaDataFlowers4 小时前
基于EasyCode插件的SpringBoot和Mybatis框架快速整合以及PostMan的使用
spring boot·mybatis·postman
qingwufeiyang_5307 小时前
Mybatis-plus学习笔记1
笔记·学习·mybatis
毅炼11 小时前
MyBatis 常见问题总结
java·数据库·sql·mybatis
消失的旧时光-194312 小时前
Spring Boot 实战(四):MySQL + MyBatis 接入,打通用户注册最小闭环
spring boot·mysql·mybatis
行走的搬运工2 天前
Spring Security_05
java·spring·mybatis
无级程序员2 天前
Mybatis中保证时间戳的一致性
mybatis
希望永不加班2 天前
SpringBoot 自定义 Starter:从零开发一个私有 Starter
java·spring boot·后端·spring·mybatis
未秃头的程序猿2 天前
💥 MyBatis 面试连环炮:从源码原理到实战避坑,彻底拿下 Offer 通关秘籍
后端·面试·mybatis
A_QXBlms2 天前
企微群发消息技术实现:定时任务+模板消息
java·mybatis·企业微信