mybatis-plus 实现分页查询步骤

MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。它提供了代码生成器、条件构造器、分页插件等多种功能,其中分页查询是一个常用的功能。

以下是如何在 MyBatis-Plus 中实现分页查询的基本步骤:

1. 引入 MyBatis-Plus 分页插件依赖

首先,确保你的项目中已经添加了 MyBatis-Plus 的依赖,并且包含了分页插件。如果没有,可以在 pom.xml 中添加如下依赖:

xml

java 复制代码
<!-- MyBatis-Plus 分页插件 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>你的MyBatis-Plus版本</version>
</dependency>

2. 配置分页插件

在你的 Spring Boot 配置类中,添加分页插件的配置:

java

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

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

3. 使用分页查询

在你的 Mapper 接口中,你可以使用 MyBatis-Plus 提供的 IPage<T> 类型来接收分页参数,并返回分页结果。

java

java 复制代码
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import your.package.name.entity.YourEntity;

@Mapper
public interface YourEntityMapper extends BaseMapper<YourEntity> {
    // 这里定义你的 CRUD 操作
}

在你的服务层或控制器层,你可以这样使用分页查询:

java

java 复制代码
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import your.package.name.mapper.YourEntityMapper;
import your.package.name.entity.YourEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class YourEntityService {

    @Autowired
    private YourEntityMapper yourEntityMapper;

    public Page<YourEntity> selectPage(int current, int size) {
        Page<YourEntity> page = new Page<>(current, size);
        return yourEntityMapper.selectPage(page, null); // 第二个参数可以是查询条件,这里为 null 表示查询所有
    }
}

4. 控制器层调用

在你的控制器中,你可以接收前端传递的分页参数,并调用服务层的方法:

java

java 复制代码
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;
import your.package.name.entity.YourEntity;
import your.package.name.service.YourEntityService;

import java.util.List;

@RestController
public class YourEntityController {

    @Autowired
    private YourEntityService yourEntityService;

    @GetMapping("your-entity/list")
    public Page<YourEntity> list(@RequestParam(defaultValue = "1") int current,
                                 @RequestParam(defaultValue = "10") int size) {
        return yourEntityService.selectPage(current, size);
    }
}

这样,当请求到达控制器的 list 方法时,就会执行分页查询,并返回分页结果。

请注意,这里的 your.package.name 需要替换为你的实际包名,YourEntityYourEntityMapper 需要替换为你的实际实体类和 Mapper 接口。

相关推荐
risc1234561 天前
Roaring Bitmap
java·开发语言·mybatis
Wang's Blog1 天前
JavaWeb快速入门: MyBatis入门与实战
java·mybatis
前端炒粉2 天前
马克思主义基本原理在MyBatis框架中的指导作用探析
mybatis·马克思主义
.Hypocritical.2 天前
MyBatis-Plus笔记
mybatis·mybatisplus
折哥的程序人生 · 物流技术专研3 天前
Java面试通关⑩:MyBatis核心源码全集
mybatis·校招·java面试·orm框架·源码解析·数据库交互·社招
SeeYa-J3 天前
MyBatis(数据持久层,❗ “接口 = SQL执行器”)
mybatis
骑士雄师3 天前
java面试题:jvm ,mybatis
java·jvm·mybatis
风中芦苇啊3 天前
Java MyBatis 实战:如何通过 SQL 查询返回 List<Map> 数据格式
java·sql·mybatis
脑子运行超载4 天前
Jackson处理和mybatis的xml转换问题
xml·jackson·mybatis·javatype
杨运交4 天前
[042][数据模块]Mybatis Plus 数据库级租户:基于多数据源路由的动态隔离实现
数据库·oracle·mybatis