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 {

    @Override
    public 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 {

    @Autowired
    private 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 哥,看能不能帮你解决。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

威哥爱编程(马剑威)

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值