MyBatisPlus如何实现对查询结果分页?

2024-08-28 14:28

本文主要是介绍MyBatisPlus如何实现对查询结果分页?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。MyBatis-Plus 支持多种数据库的分页查询,其分页功能是通过 Page 类实现的。

以下是使用 MyBatis-Plus 实现分页查询的基本步骤:

  1. 添加依赖:首先确保你的项目中已经添加了 MyBatis-Plus 的依赖。

  2. 配置 Mapper 接口:创建一个 Mapper 接口,该接口继承自 BaseMapper<T>,其中 T 是你的实体类。

  3. 创建 Service:在 Service 层中,你可以注入 Mapper 接口,并调用其分页查询的方法。

  4. 使用 Page 类:创建一个 Page 对象,设置当前页码和每页显示的记录数。

  5. 调用分页查询:在 Mapper 接口中定义一个分页查询的方法,使用 @Select 注解或者 XML 映射文件来指定查询语句。然后在 Service 层调用这个方法,传入 Page 对象。

  6. 处理结果:分页查询的结果会返回一个 PageInfo<T> 对象,其中包含了当前页的数据和分页信息。

好的,用一个案例来具体看一下,如何使用 MyBatis-Plus 进行分页查询。假设我们有一个需求是,在用户管理系统,需要对用户列表进行分页显示。

  1. 实体类(User.java)
public class User {private Long id;private String name;private Integer age;private String email;// 省略其他字段和getter/setter方法
}
  1. Mapper 接口(UserMapper.java)
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;public interface UserMapper extends BaseMapper<User> {// 这里可以添加自定义的查询方法,例如按状态查询Page<User> selectByState(Page<User> page, @Param("state") Integer state);
}
  1. Mapper XML 文件(UserMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper"><!-- 按状态查询用户的分页结果 --><select id="selectByState" resultType="com.example.entity.User">SELECT * FROM user WHERE state = #{state}</select>
</mapper>
  1. Service 接口(IUserService.java)
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.entity.User;public interface IUserService extends IService<User> {IPage<User> getUserListByState(Integer state, int current, int size);
}
  1. Service 实现(UserServiceImpl.java)
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.entity.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {@Overridepublic IPage<User> getUserListByState(Integer state, int current, int size) {IPage<User> page = this.page(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size), null);if (state != null) {return this.baseMapper.selectByState(page, state);}return page;}
}
  1. Controller(UserController.java)
import com.example.service.IUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.entity.User;
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 {@Autowiredprivate IUserService userService;@GetMapping("/users")public IPage<User> getUsers(@RequestParam(defaultValue = "0") Integer state,@RequestParam(defaultValue = "1") int current,@RequestParam(defaultValue = "10") int size) {return userService.getUserListByState(state, current, size);}
}
  1. 启动类(Application.java)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
  1. application.properties
# 配置数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/userdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=weige
spring.datasource.password=wg123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# 配置 MyBatis-Plus
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
mybatis-plus.type-aliases-package=com.vin.entity

OK,这个示例不知道是否满足你的需求,你也可以根据自己的实际情况来优化代码,成为你需要的样子,如何有更多问题可以后台私信 V 哥,看能不能帮你解决。

这篇关于MyBatisPlus如何实现对查询结果分页?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115022

相关文章

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

MyBatisX逆向工程的实现示例

《MyBatisX逆向工程的实现示例》本文主要介绍了MyBatisX逆向工程的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录逆向工程准备好数据库、表安装MyBATisX插件项目连接数据库引入依赖pom.XML生成实体类、

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

Java实现MinIO文件上传的加解密操作

《Java实现MinIO文件上传的加解密操作》在云存储场景中,数据安全是核心需求之一,MinIO作为高性能对象存储服务,支持通过客户端加密(CSE)在数据上传前完成加密,下面我们来看看如何通过Java... 目录一、背景与需求二、技术选型与原理1. 加密方案对比2. 核心算法选择三、完整代码实现1. 加密上