当前位置: 首页 > article >正文

使用mybatis plus的@Select自定义sql时,如何实现通用的分页查询?

在 MyBatis Plus 中使用 @Select 自定义 SQL 实现通用的分页查询,可按以下步骤操作:

1. 配置分页插件

首先,需要在项目中配置 MyBatis Plus 的分页插件。以 Spring Boot 项目为例,在配置类中添加分页插件的 Bean:

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;
    }
}

2. 定义实体类

假设我们有一个 User 实体类:

import com.baomidou.mybatisplus.annotation.TableName;

@TableName("user")
public class User {
    private Long id;
    private String name;
    private Integer age;

    // 构造函数、Getter 和 Setter 方法
    public User() {}

    public User(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3. 定义 Mapper 接口

在 Mapper 接口中使用 @Select 注解编写自定义 SQL,并接收 Page 对象作为参数:

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

public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT * FROM user WHERE age > #{age}")
    IPage<User> selectUserByAge(Page<User> page, Integer age);
}

4. 实现分页查询服务

在服务层调用 Mapper 接口的方法进行分页查询:

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> getUserByAgePage(Integer pageNum, Integer pageSize, Integer age) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return userMapper.selectUserByAge(page, age);
    }
}

5. 调用分页查询方法

在控制器或测试类中调用服务层的方法进行分页查询:

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")
    public IPage<User> getUsers(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam Integer age) {
        return userService.getUserByAgePage(pageNum, pageSize, age);
    }
}

解释

  • 分页插件配置MybatisPlusInterceptor 是 MyBatis Plus 的拦截器,PaginationInnerInterceptor 是分页拦截器,用于实现分页功能。
  • Mapper 接口@Select 注解中的 SQL 是自定义的查询语句,Page 对象作为参数传入,MyBatis Plus 会自动处理分页逻辑。
  • 服务层:创建 Page 对象,设置当前页码和每页记录数,调用 Mapper 接口的方法进行分页查询。
  • 控制器:接收前端传入的页码、每页记录数和查询条件,调用服务层的方法进行分页查询并返回结果。

通过以上步骤,就可以在 MyBatis Plus 中使用 @Select 自定义 SQL 实现通用的分页查询。


http://www.kler.cn/a/573791.html

相关文章:

  • 2025前端最新面试题-安全篇
  • DeepSeek 与云原生后端:AI 赋能现代应用架构
  • 达梦数据库备份
  • 什么是kube-proxy?
  • 容器 /dev/shm 泄漏学习
  • 嵌入式系统启动流程分析:从汇编到C语言环境的过渡分析
  • Van Uploader解决Android11及以下系统上传图片无反应问题
  • java面向对象(详细讲解)
  • 记录linux安装mysql后链接不上的解决方法
  • 期权帮|2025年股指期货最新规则是什么?
  • 2025.3.3总结
  • android接入rocketmq
  • FastGPT 源码:混合检索调用链路
  • PHP fastadmin 学习
  • [杂学笔记]HTTP1.0和HTTP1.1区别、socket系列接口与TCP协议、传输长数据的时候考虑网络问题、慢查询如何优化、C++的垃圾回收机制
  • MQ保证消息的顺序性
  • 如何在Windows下离线部署DeepSeek并以WebApi形式调用
  • Golang的代码注释规范指南
  • Macro Bullion:国际金价回调态势下,金市表现与金银交易建议
  • 数据结构与算法:回溯(下):子集相关力扣题(78.子集、90.子集Ⅱ、491.非递减子序列)、排列相关力扣题(46.全排列、47.全排列Ⅱ)