mybatisplus实现分页查询

一、搭建mybatisplus项目

参考博客地址 https://blog.csdn.net/only_foryou/article/details/156913310?spm=1001.2014.3001.5501

mybatis-plus 版本 3.5.8只需要增加配置类即可

java 复制代码
@Configuration
public class MybatisPlusConfig {
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
复制代码
page有多种方式这里给一种service方法

如:

java 复制代码
    @Override
    public Page<Student> getPageByName(int pageNum, int pageSize, String name) {
        return page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<Student>()
                .eq(Student::getName, name));
    }

二、entity

java 复制代码
package com.qiu.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 描述:学生实体类
 *
 * @author: qxd
 * @date: 2025/11/29 22:09
 * @version: 1.0.0
 */
@Data
// 指定表名
@TableName("student")
public class Student {
    /**
     * 学生ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 学生姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 性别
     */
    private String gender;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 电话
     */
    private String phone;

    /**
     * 地址
     */
    private String address;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
}

**三、**dao

java 复制代码
package com.qiu.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiu.entity.Student;
import org.apache.ibatis.annotations.Mapper;

/**
 * 描述:学生数据访问接口
 *
 * @author: qxd
 * @date: 2025/11/12 22:11
 * @version: 1.0.0
 */
@Mapper
public interface StudentDao extends BaseMapper<Student> {


}

**四、**Service

java 复制代码
package com.qiu.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qiu.entity.Student;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 描述:
 *
 * @author: qxd
 * @date: 2025/11/29 22:04
 * @version: 1.0.0
 */
public interface StudentService extends IService<Student> {

    public List<Student> getListByName(String name);
    public Page<Student> getPageByName(@RequestParam(defaultValue = "1") int pageNum,
                                       @RequestParam(defaultValue = "10") int pageSize,
                                       @RequestParam("name") String name);
}

**五、**ServiceImpl

java 复制代码
package com.qiu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiu.dao.StudentDao;
import com.qiu.entity.Student;
import com.qiu.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述:
 *
 * @author: qxd
 * @date: 2025/11/19 22:04
 * @version: 1.0.0
 */
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {

    @Override
    public List<Student> getListByName(String name) {
        return list(new LambdaQueryWrapper<Student>().eq(Student::getName, name));
    }

    @Override
    public Page<Student> getPageByName(int pageNum, int pageSize, String name) {
        return page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<Student>()
                .eq(Student::getName, name));
    }
}

六、Controller

java 复制代码
package com.qiu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiu.entity.Student;
import com.qiu.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 描述:demo
 *
 * @author: qxd
 * @date: 2025/11/29 21:54
 * @version: 1.0.0
 */
@Slf4j
@RequestMapping("/student")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Operation(summary = "hello", description = "hello")
    @GetMapping("/hello")
    public String hello() {
        log.info("hello world");
        return "hello world";
    }

    @Operation(summary = "根据姓名查询学生列表", description = "根据姓名查询学生列表")
    @GetMapping("/getListByName")
    public List<Student> getListByName(@RequestParam("name") String name) {
        return studentService.getListByName(name);
    }

    @Operation(summary = "根据名字分页查询", description = "根据姓名分页查询学生信息")
    @GetMapping("/getPageByName")
    public Page<Student> getPageByName(@RequestParam(defaultValue = "1") int pageNum,
                                       @RequestParam(defaultValue = "10") int pageSize,
                                       @RequestParam("name") String name) {
        return studentService.getPageByName(pageNum, pageSize, name);
    }


}

七、config

java 复制代码
package com.qiu.config;

import com.baomidou.mybatisplus.annotation.DbType;
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(DbType.MYSQL));
        return interceptor;
    }
}

八、项目地址

https://gitee.com/qiuxiaodong/demo

项目名称:demo-springboot-mybatisplus

相关推荐
nbsaas-boot2 小时前
基于 Java 21 ScopedValue 的多租户动态数据源完整实践
java·开发语言
2301_780669862 小时前
线程安全、线程同步(三种加锁方式)、线程池(两种创建线程池方式、线程池处理Runnable任务、线程池处理Callable任务)、并发/并行
java
liuc03172 小时前
Java项目关于不同key的读取
java·开发语言
yaoxin5211232 小时前
296. Java Stream API - 二元操作符与“单位元“
java·服务器·windows
罗伯特_十三2 小时前
Spring AI ChatModel 使用记录
java·人工智能·spring
毕设源码-朱学姐2 小时前
【开题答辩全过程】以 基于SpringBoot的律师事务所管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
菜宾2 小时前
java-seata基础教学
java·开发语言·adb
毕设源码-朱学姐3 小时前
【开题答辩全过程】以 基于springboot的日用药品仓库管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
毕设源码-赖学姐3 小时前
【开题答辩全过程】以 基于javaweb的外卖点餐系统的设计与实现为例,包含答辩的问题和答案
java