用到的依赖:

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</exclusion>
</exclusions>
</dependency>

首先配置mybatis-plus配置

package com.qfclo.login.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration

@MapperScan("com.qfclo.login.mapper")

public class MybatisPlusConfig {

/**

* 分页插件

*/

    @Bean

    public PaginationInterceptorpaginationInterceptor() {

return new PaginationInterceptor();

    }

/**

* 打印 sql

*/

    @Bean

    public PerformanceInterceptorperformanceInterceptor() {

PerformanceInterceptor performanceInterceptor =new PerformanceInterceptor();

        //格式化sql语句

        Properties properties =new Properties();

        properties.setProperty("format", "faalse");

        performanceInterceptor.setProperties(properties);

        return performanceInterceptor;

    }

}

第一种方式,mybatis-plus原生QueryWrapper方式分页,这种方式比较简单,可以不用修改Mapper,适合简单的增删改查。

    @RequestMapping(value = "/orgist1")//,method = RequestMethod.POST)
public Map<String,Object> orglist1()
{ Map<String,Object> map = new HashMap<>(); QueryWrapper<OauthOrganization> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id"); Page<OauthOrganization> page = new Page<>(1,5); // 查询第1页,每页返回5条
IPage<OauthOrganization> iPage = oauthOrganizationMapper.selectPage(page,queryWrapper);
System.out.println(iPage.getRecords().size());
System.out.println(JSON.toJSONString(iPage));
return map;
}

第二种方式,使用mapper文件的select注解,优点是可以方便的建立查询语句,可以联合多表查询。
Mapper文件

    @Select("SELECT * FROM oauth_organization WHERE id < #{m.id} ORDER BY `id` DESC")
List<OauthOrganization> selectpage(Map<String,Object> m, Page<OauthOrganization> page);

Controller文件

    @RequestMapping(value = "/orgist4")//,method = RequestMethod.POST)
public Map<String,Object> orglist4()
{ Map<String,Object> map = new HashMap<>();
Map<String,Object> m = new HashMap<>();
m.put("id",5);
Page<OauthOrganization> page = new Page<>(1,5);
page.setRecords(oauthOrganizationMapper.selectpage(m,page));
System.out.println(page.getRecords().size());
System.out.println(JSON.toJSONString(page));
return map;
}

作者:小鱼儿2020
链接:https://www.jianshu.com/p/0a21569f1e06
来源:简书

SpringBoot+Mybatis-Plus两种分页方法的更多相关文章

  1. PostgreSQL两种分页方法查询时间比较

    数据库中存了3000W条数据,两种分页查询测试时间 第一种 SELECT * FROM test_table WHERE i_id> limit 100; Time: 0.016s 第二种 SE ...

  2. sql server两种分页方法

    方法一: --分页方法一 OrderID,CustomerID, EmployeeID,OrderDate,ShippedDate,ShipName,ShipAddress,Freight from ...

  3. mybatis的两种分页方式:RowBounds和PageHelper

    原理:拦截器. 使用方法: RowBounds:在mapper.java中的方法中传入RowBounds对象. RowBounds rowBounds = new RowBounds(offset, ...

  4. sqlserver两种分页方法比较

    -- 3000 page(从1开始) 10 pagesize -- 方法1(效率不高): SELECT TOP 10 * FROM [xxx].[oooo] WHERE id NOT IN (SELE ...

  5. SQL Server两种分页的存储过程介绍

          由于现在很多的企业招聘的笔试都会让来招聘的写一个分页的存储过程,有的企业甚至要求应聘者用两种方式实现分页,如果没有在实际项目中使用过分页,那么很多的应聘者都会出现一定的问题,下面介绍两种分 ...

  6. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  7. mybatis中两种取值方式?谈谈Spring框架理解?

    1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...

  8. angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用

    今天我们要讲的是ng2的路由系统. 例子

  9. git两种合并方法 比较merge和rebase

    18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...

随机推荐

  1. Centos7下关闭Firewalls配置iptables

    在网上搜索了很多这种资料,现在总结一下以备后用. 1.关闭防火墙:sudo systemctl stop firewalld.service 2.关闭开机启动:sudo systemctl disab ...

  2. python中sort和sorted用法的区别

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  3. k8s之RBAC-基于角色的访问控制

    一个在名称空间内的对象的完整url模板: Object_URL: /apis/<GROUP>/<VERSION>/namespaces/<NAMESPACE_NAME&g ...

  4. 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务

    来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...

  5. 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据

    一.  使用RSA证书加.解密敏感数据 X.509证书标准支持三种不对称加密算法:RSA, DSA, Diffie-Hellman algorithms.最常用的是RSA算法.所以本文就以前面章节使用 ...

  6. C#UDP异步通信

    using SetingDemo.LogHelp;using SetingDemo.SingleRowDeclare;using System;using System.Collections.Gen ...

  7. JS中逗号运算符的用法

    逗号运算符,它将先计算左边的参数,再计算右边的参数值.然后返回最右边参数的值. 原书举的例子不太好,无法解释上面那句话,这里另外提供一个: var a = 10, b = 20; function C ...

  8. 销售订单(SO)-API-创建销售订单

    创建销售订单API主要注意几点: 初始化环境变量:fnd_global.apps_initialize(); mo_global.init('ONT'); mo_global.set_policy_c ...

  9. Java基础加强-内部类及代理

    /*内部类是一个编译时的概念,*/ 常规内部类.静态内部类.局部内部类.匿名内部类 1.常规内部类(常规内部类没有static修饰且定义在外部类类体中) 1.常规内部类中的方法可以直接使用外部类的实例 ...

  10. Django 开发相关知识 整理

    前言 前端ajax HTTP请求头 ajax上传文件 jsonp跨域 URL 设计 分发 url参数编码 反向生成url 视图 request对象 POST url信息 视图返回值 HttpRespo ...