Mybatis-plus的分页查询

1. 简单说明

嗨,大家好!今天给大家分享的是Mybatis-plus 插件的分页机制,说起分页机制,相信我们程序员都不陌生,今天,我就给大家分享一下Mybatis-plus的分页机制,供大家学习和Copy。

2. 介绍说明

如果你想看代码,可以直接跳到代码区域,这里只是一些简单的说明,如果你想学习,建议可以看看这一块的任容

本章节将介绍 BaseMapper 中的分页查询,BaseMapper 接口提供了如下几个分页查询接口:

  • selectPage:根据 entity 条件,查询全部记录
  • selectMapsPage:根据 Wrapper 条件,查询全部记录

在使用上面两个方法进行分页查询时,我们需要配置分页插件。这是只是在介绍SpringBoot的使用。
注意:由于我们使用的 Spring Boot 项目,因此需要通过 @Configuration@Bean 注解来添加配置

3. 完整配置类代码:

下边就是完整的配置类,至于为什么比官网上的少一点,因为那个可以说会报错,而且也不需要使用到它,以下就是完整配置类:

package com.hxstrive.mybatis_plus;
 
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 paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 指定数据库方言为 MYSQL
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
 
}

注意:如果你没有配置分页插件,则不会进行分页。所以这个一定要配置。

4. 示例代码

  1. 使用 QueryWrapper 和 Page 作为参数进行分页,例如:
package com.hxstrive.mybatis_plus.select;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class Select6Test {

   @Autowired
   private SimpleMapper simpleMapper;

   @Test
   void contextLoads() {
       QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
       wrapper.isNotNull("user_id");

       // 创建分页对象(1表示第一页;4表示每页大小为4)
       Page<UserBean> page = new Page<>(1, 4);
       Page<UserBean> result = simpleMapper.selectPage(page, wrapper);
       System.out.println("page == result: " + (page == result));
       System.out.println("size: " + result.getSize());
       System.out.println("total: " + result.getTotal());
       for(UserBean userBean : result.getRecords()) {
           System.out.println(userBean);
       }
   }

}

运行上面代码,你会发现 page 和selectPage 返回的 result1 相等,说明两者是同一个对象。因此,可以忽略掉 selectPage 方法的返回结果,如下:

Page<UserBean> page = new Page<>(1, 4);
simpleMapper.selectPage(page, wrapper);
  1. 另外一个分页方法,selectMapsPage 和上面的使用方法一样,仅仅是返回类型不一样。代码如下:
package com.hxstrive.mybatis_plus.select;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Map;
 
@RunWith(SpringRunner.class)
@SpringBootTest
class Select7Test {
 
    @Autowired
    private SimpleMapper simpleMapper;
 
    @Test
    void contextLoads() {
        // 返回的结果类型为 Map<String,Object>
        Page<Map<String,Object>> page = new Page<>(1, 4);
        simpleMapper.selectMapsPage(page, null);
        System.out.println("size: " + page.getSize());
        System.out.println("total: " + page.getTotal());
        System.out.println("pages: " + page.getPages());
        for(Map<String,Object> map : page.getRecords()) {
            System.out.println(map);
        }
    }
 
}

注意:这里我们平常会使用以下代码获取page里边的存放的代码。
page.getRecords():这是用来获取我们分页查出来的数据

5. 最后总结

这一小结,我们主要是对mybatis-pluts 插件的分页功能的使用,做了简单介绍。下边我们来梳理以下,使用插件步骤:

  1. 在我们项目的配置文件夹下,一定要添加MybatisPlusConfig
  2. 我们需要在这个配置类中添加paginationInterceptor()方法,进行分页功能的配置,其实就是配置分页功能的拦截器
  3. 使用方法,进来数据的分页
  4. 使用方法,返回分页的数据

以上就是我们使用分页的步骤了,这里需要注意一些问题,一定要把相应的注解加上去,要不然,是没办法使用的。

  • 22
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
mybatis-plus 是基于 mybatis 的增强工具,提供了很多方便实用的功能,其中包括分页查询分页查询通常用于处理海量数据时,对数据进行分页展示,以优化系统性能。 使用 mybatis-plus 进行分页查询需要注意以下几点: 1. 在 mapper 接口中定义方法时需要使用 IPage<T> 类型的参数,其中 T 为实体类。 2. 在 service 层调用 mapper 中的分页查询方法时,需要传入当前页数和每页显示的数量,可以通过 Page<T> 对象来实现。 3. 在 mapper 映射文件中使用 select 标签进行查询,需要按照 mybatis-plus 提供的语法结构来书写,主要包括表名、查询条件、分页条件等。 例如,以下是使用 mybatis-plus 进行分页查询的一个示例: 在 mapper 中定义方法: ``` public interface UserMapper extends BaseMapper<User> { IPage<User> selectUserByPage(Page<?> page, @Param("username") String username); } ``` 在 service 中实现分页查询: ``` @Override public IPage<User> getUserByPage(int pageNum, int pageSize, String username) { // 创建分页对象 Page<User> page = new Page<>(pageNum, pageSize); // 调用 mapper 方法进行分页查询 return userMapper.selectUserByPage(page, username); } ``` 在 mapper 映射文件中编写查询语句: ``` <select id="selectUserByPage" resultMap="userResultMap"> select * from user <where> <if test="username != null and username != ''"> and username like concat('%', #{username}, '%') </if> </where> order by id desc </select> ``` 通过以上步骤,就可以实现在 mybatis-plus 中进行分页查询并展示数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

知识的搬运工旺仔

希望能帮助到大家学习

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

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

打赏作者

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

抵扣说明:

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

余额充值