Mybatis-Plus使用Page、PageHelper实现分页

1 篇文章 0 订阅

一、Page方式

1、依赖

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

2、实现

开启page分页

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.coname.projectname.mapper")
public class MyBatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

分页代码

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.xxxx.entity.User;
import com.xxx.xxx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 徒手千行代码无bug
 * @date 2021/01/15 16:01
 * @description: description about this class
 */
@Service
public class CommonServiceImpl extends ServiceImpl<UserMapper, User> implements CommonService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void test(){
        List<User> users = userMapper.selectPage(new Page<>(2,1),
                new QueryWrapper<User>().in("userId", Arrays.asList(1,2))).getRecords();

        System.out.println("users number is:" + users.size());
   }
}

二、PageHelper方式

1、依赖

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2、配置

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  page-size-zero: true
  params: count=countSql

3、实现

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.xxxx.entity.User;
import com.xxxx.xxxx.entity.CredentialDetail;
import com.xxxx.xxxx.mapper.CredentialDetailMapper;
import com.xxxx.xxxx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 徒手千行代码无bug
 * @date 2021/01/15 16:01
 * @description: description about this class
 */
@Service
public class CommonServiceImpl extends ServiceImpl<UserMapper, User> implements CommonService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void test(){
        PageHelper.startPage(1, 100);
 // 作用于下一行的查询
        List<User> users = userMapper.selectList(
                new QueryWrapper<User>().in("userId", Arrays.asList(1,2)));

        PageHelper.startPage(2, 4);
 // 作用于下一行的查询
        List<CredentialDetail> credentialDetails = credentialDetailMapper.selectList(
                new QueryWrapper<CredentialDetail>().lambda().notLike(CredentialDetail::getFieldValue, "测试"));

        System.out.println("users number is:" + users.size());
        System.out.println("credentialDetails number is:" + credentialDetails.size());
   }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MyBatis-Plus提供的PageHelper类是用于分页查询的工具类,它可以帮助我们简化分页查询的代码,提高开发效率。使用PageHelper类进行分页查询的步骤如下: 1. 引入MyBatis-Plus的依赖包和PageHelper的依赖包。 2. 在MyBatis的配置文件中配置PageHelper插件。 3. 在DAO层的Mapper接口中定义查询方法,并添加Page对象作为方法参数。 4. 在查询方法中使用PageHelper.startPage()方法设置分页参数,然后调用Mapper接口的查询方法查询数据。 5. 将查询结果封装到Page对象中,并返回Page对象。 下面是一个示例代码: ``` // 引入PageHelper依赖包 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> // 在MyBatis的配置文件中配置PageHelper插件 <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> </plugin> </plugins> // 在DAO层的Mapper接口中定义查询方法,并添加Page对象作为方法参数 public interface UserMapper { List<User> selectUserList(Page<User> page); } // 在查询方法中使用PageHelper.startPage()方法设置分页参数,然后调用Mapper接口的查询方法查询数据 public List<User> getUserList(int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); userMapper.selectUserList(page); return page.getRecords(); } ``` 在上面的示例代码中,我们使用PageHelper.startPage()方法设置分页参数,然后调用Mapper接口的selectUserList()方法查询数据。查询结果封装在Page对象中,最后返回Page对象中的记录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值