Mybatis-Plus的IPage和Page

Mybatis-Plus 中的分页查询接口主要有两个:IPage 和 Page。

  1. IPage 接口:
    IPage 是 Mybatis-Plus 中的分页结果集接口,它继承了 Mybatis 的 RowBounds 接口,提供了一系列的分页查询方法。该接口主要用于返回分页后的数据结果。

  2. Page 类:
    Page 类是 IPage 接口的默认实现类,实现了 IPage 接口中的方法。在进行分页查询时,通常会创建一个 Page 对象,并设置相关的分页参数。

以下是 IPage 和 Page 类的常用参数:

  • current:当前页数,必须大于等于 1,默认值为 1。
  • size:每页条数,默认值为 10。
  • total:总条数,默认值为 0。
  • records:当前页数据集合,默认值为空集合。
  • searchCount:是否进行 count 查询,默认值为 true,表示会统计总条数。
  • pages:总页数,通过计算得出。
  • optimizeCountSql:是否优化 count 查询,默认值为 true。
  • hitCount:是否对 count 进行 limit 优化,默认值为 false。
  • countId:count 查询的列名,默认为 null,表示所有列。
  • maxLimit:设置最大的 limit 查询限制,默认值为 -1,表示不限制。

举个栗子:

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public IPage<User> getUserList(int currentPage, int pageSize) {
        // 创建分页对象
        Page<User> page = new Page<>(currentPage, pageSize);

        // 构造查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("age", 18);

        // 执行分页查询
        IPage<User> userPage = userMapper.selectPage(page, queryWrapper);

        return userPage;
    }
}

在这个例子中,我们通过构造一个 Page 对象来设置当前页和每页条数。然后通过 QueryWrapper 构造查询条件,在调用 userMapper.selectPage(page, queryWrapper) 方法进行分页查询。

你可能感兴趣的:(Java,mybatis,java,开发语言)