iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot MP简单的分页查询测试实现步骤分解
  • 908
分享到

SpringBoot MP简单的分页查询测试实现步骤分解

SpringBoot MP分页查询测试SpringBoot分页查询测试 2023-05-14 06:05:44 908人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

导入最新的mp依赖是第一步不然太低的版本什么都做不了,3,1以下的好像连分页插件都没有加进去,所以我们用最新的3.5的,保证啥都有: <dependency&g

导入最新的mp依赖是第一步不然太低的版本什么都做不了,3,1以下的好像连分页插件都没有加进去,所以我们用最新的3.5的,保证啥都有:

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

这里我们需要认识两个插件:mp的核心插件MybatisPlusInterceptor与自动分页插件PaginationInnerInterceptor。

MybatisPlusInterceptor的源码(去掉中间的处理代码):

public class MybatisPlusInterceptor implements Interceptor {
    private List<InnerInterceptor> interceptors = new ArrayList();
    public MybatisPlusInterceptor() {}
    public Object intercept(Invocation invocation) throws Throwable {}
    public Object plugin(Object target) {}
    public void addInnerInterceptor(InnerInterceptor innerInterceptor) {}
    public List<InnerInterceptor> getInterceptors() {}
    public void setProperties(Properties properties) {}
    public void setInterceptors(final List<InnerInterceptor> interceptors) {}
}

我们可以发现它有一个私有的属性列表 List<InnerInterceptor> 而这个链表中的元素类型是InnerInterceptor。

InnerInterceptor源码:

public interface InnerInterceptor {
    default boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, Boundsql boundSql) throws SQLException {
        return true;
    }
    default void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    }
    default boolean willDoUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        return true;
    }
    default void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
    }
    default void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
    }
    default void beforeGetBoundSql(StatementHandler sh) {
    }
    default void setProperties(Properties properties) {
    }
}

不难发现这个接口的内容大致就是设置默认的属性,从代码的意思上就是提供默认的数据库操作执行时期前后执行的一些逻辑,谁实现它的方法会得到新的功能?

再看看PaginationInnerInterceptor插件的源码:

public class PaginationInnerInterceptor implements InnerInterceptor {
    protected static final List<SelectItem> COUNT_SELECT_ITEM = Collections.singletonList((new SelectExpressionItem((new Column()).withColumnName("COUNT(*)"))).withAlias(new Alias("total")));
    protected static final Map<String, MappedStatement> countMsCache = new ConcurrentHashMap();
    protected final Log logger = LogFactory.getLog(this.getClass());
    protected boolean overflow;
    protected Long maxLimit;
    private DbType dbType;
    private IDialect dialect;
    protected boolean optimizeJoin = true;
    public PaginationInnerInterceptor(DbType dbType) {
        this.dbType = dbType;
    }
    public PaginationInnerInterceptor(IDialect dialect) {
        this.dialect = dialect;
    }
    public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        IPage<?> page = (IPage)ParameterUtils.findPage(parameter).orElse((Object)null);
        if (page != null && page.getSize() >= 0L && page.searchCount()) {
            MappedStatement countMs = this.buildCountMappedStatement(ms, page.countId());
            BoundSql countSql;
            if (countMs != null) {
                countSql = countMs.getBoundSql(parameter);
            } else {
                countMs = this.buildAutoCountMappedStatement(ms);
                String countSqlStr = this.autoCountSql(page, boundSql.getSql());
                MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
                countSql = new BoundSql(countMs.getConfiguration(), countSqlStr, mpBoundSql.parameterMappings(), parameter);
                PluginUtils.setAdditionalParameter(countSql, mpBoundSql.additionalParameters());
            }
            CacheKey cacheKey = executor.createCacheKey(countMs, parameter, rowBounds, countSql);
            List<Object> result = executor.query(countMs, parameter, rowBounds, resultHandler, cacheKey, countSql);
            long total = 0L;
            if (CollectionUtils.isNotEmpty(result)) {
                Object o = result.get(0);
                if (o != null) {
                    total = Long.parseLong(o.toString());
                }
            }
            page.setTotal(total);
            return this.continuePage(page);
        } else {
            return true;
        }
    }
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {...........省略之后全部的内容........}

我们不难发现它实现了来自于InnerInterceptor的方法,这里面的源码有时间需要好好处处逻辑。

我们知道了分页插件和核心插件的关系,也就是我们可以将分页插件添加入核心插件内部的插件链表中去,从而实现多功能插件的使用。

配置mp插件,并将插件交由spring管理(我们用的是SpringBoot进行测试所以不需要使用xml文件):

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 MpConfig {
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        
        PaginationInnerInterceptor pagInterceptor = new PaginationInnerInterceptor();
        
        pagInterceptor.setOverflow(false);
        
        pagInterceptor.setMaxLimit(500L);
        
        pagInterceptor.setDbType(DbType.Mysql);
        
        interceptor.addInnerInterceptor(pagInterceptor);
        return interceptor;
    }
}

配置完之后写一个Mapper接口:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hlc.mp.entity.Product;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
}

为接口创建一个服务类(一定按照mp编码的风格来):

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlc.mp.entity.Product;
import com.hlc.mp.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "ProductService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements IService<Product> {
    @Autowired
    ProductMapper productMapper;
    
    public Page<Product> page(Long current) {
        
        Page<Product> productPage = new Page<>(current, 1);
        
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        productMapper.selectPage(productPage, queryWrapper);
        return productPage;
    }
}

到这里我们可以看到分页的具体方法就是,先创建一个分页对象,规定页码和每一页的数据量的大小,其次确定查询操作的范围,并使用BaseMapper<T>给予我们的查询分页方法selectPage(E page,Wapper<T> queryWapper)进行查询分页的操作。

测试类:

    @Test
    public void testPage(){
        IPage<Product> productIPage = productService.page(2L);
        productIPage.getRecords().forEach(System.out::println);
        System.out.println("当前页码"+productIPage.getCurrent());
        System.out.println("每页显示数量"+productIPage.getSize());
        System.out.println("总页数"+productIPage.getPages());
        System.out.println("数据总量"+productIPage.getTotal());
    }

运行查看分页结果:

我们可以发现都正常的按照我们传入的页码去查询对应的页数据了,因为我设置的每页只展示一条数据,所以ID如果对应页码就说明分页成功。

到此这篇关于SpringBoot MP简单的分页查询测试实现步骤分解的文章就介绍到这了,更多相关SpringBoot MP分页查询测试内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot MP简单的分页查询测试实现步骤分解

本文链接: https://www.lsjlt.com/news/202824.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot MP简单的分页查询测试实现步骤分解
    导入最新的mp依赖是第一步不然太低的版本什么都做不了,3,1以下的好像连分页插件都没有加进去,所以我们用最新的3.5的,保证啥都有: <dependency&g...
    99+
    2023-05-14
    SpringBoot MP分页查询测试 SpringBoot分页查询测试
  • SpringBoot MP简单的分页查询测试怎么实现
    这篇文章主要讲解了“SpringBoot MP简单的分页查询测试怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot MP简单的分页查询测试怎么实现...
    99+
    2023-07-05
  • mybatis-plus多表分页查询最佳实现(简单)
    1.简介 在Mybatis Plus 中,虽然IService 接口帮我们定义了很多常用的方法,但这些都是 T 对象有用,如果涉及到 多表的查询,还是需要自定义Vo 对象和自己编写sql 语句,Mybatis Plus提供了一个Page 对...
    99+
    2023-08-23
    mybatis java mysql
  • SpringBoot中怎么实现分页查询
    在Spring Boot中,可以使用Spring Data JPA来实现分页查询。具体步骤如下: 在Repository接口中定义...
    99+
    2024-03-08
    SpringBoot
  • python中unittest单元测试的步骤分析
    这篇文章主要讲解了“python中unittest单元测试的步骤分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python中unittest单元测试的步骤分析”吧!说明导入unittes...
    99+
    2023-06-20
  • SpringBoot分页查询功能的实现方法
    目录前言:首先是手动实现分页查询:接下来是关联前端分页和后台数据:总结前言: 学习了SpringBoot分页查询的两种写法,一种是手动实现,另一种是使用框架实现。现在我将具体的实现流...
    99+
    2024-04-02
  • oracle+mybatis-plus+springboot实现分页查询的实例
    今天蠢了一上午才弄出这玩意,话不多说上代码! 1、建一个配置类 package com.sie.demo.config; import com.baomidou.mybati...
    99+
    2024-04-02
  • SpringBoot整合PageHelper实现分页查询功能详解
    前言 本文介绍的是MyBatis 分页插件 PageHelper,如果你也在用 MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页...
    99+
    2024-04-02
  • oracle+mybatis-plus+springboot怎么实现分页查询
    本篇内容主要讲解“oracle+mybatis-plus+springboot怎么实现分页查询”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“oracle+mybatis-plus+springb...
    99+
    2023-06-20
  • Springboot+Mybatis实现分页加条件查询功能
    本文实例为大家分享了Springboot+Mybatis实现分页加条件查询的具体代码,供大家参考,具体内容如下 User.xml <xml version="1.0" enco...
    99+
    2024-04-02
  • 【MybatisPlus】MP的分页查询、多条件查询以及查询过程中解决null的空值判定
    文章目录 前言一.分页处理1.调用方法传入参数获取返回值2.设置分页拦截器 二.条件查询2.1通过QueryWrapper对象来执行分页查询2.2在QueryWrapper对象的基础上使用lambda表达式2.3直接通过Lam...
    99+
    2023-08-25
    java spring mybatis
  • SqlServer 多种分页方式 详解(含简单速度测试)
    目录SQLServer分页方式1.Offset and Fetch 方式分页2.row_number() 方式分页3.row_number()变种4.not in/top方式分页5.not exists方式分页6.max...
    99+
    2022-12-16
    SqlServer 分页方式 SqlServer 分页
  • es 简单实现增加,查询,分词 热词
    看代码: $params = [ 'index' => "goods", 'body' => [ 'mappings' => [ ...
    99+
    2023-09-08
    elasticsearch mysql php
  • springboot结合vue实现增删改查及分页查询
    1:首先。创建一个springboot项目,这里我使用以及构建好基本框架的脚手架,打开是这个样子: Result类:已经封装好了三种返回类型的包装类:code,msg,data ...
    99+
    2024-04-02
  • jsp+mysql实现网页的分页查询
    本文实例为大家分享了jsp+mysql实现网页的分页查询的具体代码,供大家参考,具体内容如下 一、实现分页查询的核心sql语句 (1)查询数据库的记录总数的sql语句: selec&...
    99+
    2024-04-02
  • vue实现简单的分页功能
    前端Vue实现分页功能,供大家参考,具体内容如下 我们都知道在spring boot项目中安装pagehelper可以实现分页功能,但是在vue中也能在前端实现分页。 1、首先,在d...
    99+
    2024-04-02
  • SpringBoot如何整合PageHelper实现分页查询功能
    这篇文章主要介绍了SpringBoot如何整合PageHelper实现分页查询功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。使用方法导入依赖在中央仓库sonatype中搜...
    99+
    2023-06-29
  • MySQL实现分页查询的方法
    这篇文章主要介绍MySQL实现分页查询的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!MySQL中实现分页查询:在数据量较小的情况下可使用limit查询来实现分页查询,在数据量大...
    99+
    2024-04-02
  • Springboot+Mybatis怎么实现分页加条件查询功能
    本篇内容介绍了“Springboot+Mybatis怎么实现分页加条件查询功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!User.xml...
    99+
    2023-06-30
  • SpringBoot整合mybatis-plus实现分页查询(建议收藏)
    一、前言         最近学习了SpringBoot分页查询的两种写法,一种是手动实现,另一种是使用框架实现。现在我将具体的实现流程分享一下。 二、手动实现分页查询         先复习一下,SQL中的limit关键字,下面一行sql...
    99+
    2023-08-31
    mybatis spring boot java
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作