SpringBoot+Mybatis-Plus两种分页方法

用到的依赖:

<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
来源:简书

转载于:https://www.cnblogs.com/tuituji27/p/11303841.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值