文章目录

一、后台分页配置

MyBatis Plus自带分页插件(即BaseMapper接口中的selectPage()方法),只要简单的配置即可实现分页功能,具体步骤如下:

1. 配置分页插件

新创一个配置类,在配置类里面配置分页插件

package com.gblfy.flowable.config;

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;

/**
* MyBatisPlus 分页插件配置
*
* @Author gblfy
* @Date 2022-05-04 14:41
**/
@Configuration
public class MyBatisPlusPaginationInnerConfig {

/**
* 分页插件(官网最新)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}

}
2. 编写分页代码

分页的所有数据都在userPage对象中封装着,所以可以调用userPage对象的一系列方法对分页数据进行操作。

;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gblfy.flowable.entity.SysUser;
import com.gblfy.flowable.mapper.SysUserMapper;
import com.gblfy.flowable.service.SysUserService;
import com.gblfy.flowable.tools.pager.PagerModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* <p>
* 用户信息表 服务实现类
* </p>
*
* @author gblfy
* @since 2022-05-04
*/
@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

@Autowired
private SysUserMapper userMapper;

@Override
public PagerModel<SysUser> list(Page<SysUser> page, QueryWrapper<SysUser> queryWrapper) {
Page<SysUser> userIPage = userMapper.selectPage(page, queryWrapper);
// 分页的所有数据都在userPage对象中封装着
// 获取总页数
long pages = userIPage.getPages();
//一页显示几条数据
long size = userIPage.getSize();
// 获取当前页
long current = userIPage.getCurrent();
// 获取当前页数据集合
List<SysUser> records = userIPage.getRecords();
// 获取总记录数
long total = userIPage.getTotal();
// 当前页是否有下一页
boolean hasNext = userIPage.hasNext();
// 当前页是否有上一页
boolean hasPrevious = userIPage.hasPrevious();

System.out.println("总页数pages=" + pages);
System.out.println("当前页current=" + current);
System.out.println("当前页显示几条数据size=" + size);
System.out.println("当前页数据集合records=" + records);
System.out.println("总记录数total=" + total);
System.out.println("是否有下一页hasNext=" + hasNext);
System.out.println("是否有上一页hasPrevious=" + hasPrevious);
return new PagerModel<>(userIPage.getTotal(), records);
}

@Override
public Page<SysUser> listPage(Page<SysUser> page, QueryWrapper<SysUser> queryWrapper) {
return userMapper.selectPage(page, queryWrapper);
}
}
3. 测试

当前数据库的user表中有14条记录,设置当前页数为1,每页记录数为10。

如图:

MyBatisplus分页插件_自定义

MyBatisplus分页插件_自定义_02

controller

package com.gblfy.flowable.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gblfy.flowable.entity.SysUser;
import com.gblfy.flowable.service.SysUserService;
import com.gblfy.flowable.tools.pager.PagerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* MyBatisPlus 分页测试
*
* @Author gblfy
* @Date 2022-05-04 15:32
**/
@RestController
@RequestMapping("/page")
public class MyBatisPlusPaginateController {

@Autowired
private SysUserService userService;


@GetMapping("/list")
public PagerModel<SysUser> list(@RequestParam(value = "keyword", defaultValue = "") String keyword,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();//条件构造器
queryWrapper.like("user_name", keyword);//模糊查询Like
Page<SysUser> page = new Page(pageNum, pageSize);//分页插件
return userService.list(page, queryWrapper);//查询数据
}

@GetMapping("/listPage")
public Page<SysUser> listPage(@RequestParam(value = "keyword", defaultValue = "") String keyword,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();//条件构造器
queryWrapper.like("user_name", keyword);//模糊查询Like
Page<SysUser> page = new Page(pageNum, pageSize);//分页插件
return userService.listPage(page, queryWrapper);//查询数据
}
}

默认返回Page基本可以满足,如果自定义返回封装,参考以下案例即可

package com.gblfy.flowable.tools.pager;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* 分页
*
* @author gblfy
* @date 2022-05-03
**/
@Data
@Builder(toBuilder = true)
public class PagerModel<T> implements Serializable {
private static final long serialVersionUID = 4804053559968742915L;
/**
* 总记录数
*/
private long total;
/**
* 每页的查询结果集
*/
private List<T> rows = new ArrayList();
/**
* 获取总页数
*/
private long pages;
/**
* 获取当前页
*/
private long current;
/**
* 当前页显示几条数据
*/
private long size;
/**
* 当前页是否有下一页
*/
private boolean hasNext;
/**
* 当前页是否有上一页
*/
private boolean hasPrevious;

public PagerModel() {
}

public PagerModel(long total, List<T> rows) {
this.total = total;
this.rows = rows;
}

public PagerModel(long total, List<T> rows, long pages, long current) {
this.total = total;
this.rows = rows;
this.pages = pages;
this.current = current;
}

public PagerModel(long total, List<T> rows, long pages, long current, long size) {
this.total = total;
this.rows = rows;
this.pages = pages;
this.current = current;
this.size = size;
}

public PagerModel(long total, List<T> rows, long pages, long current, boolean hasNext, boolean hasPrevious) {
this.total = total;
this.rows = rows;
this.pages = pages;
this.current = current;
this.hasNext = hasNext;
this.hasPrevious = hasPrevious;
}

public PagerModel(long total, List<T> rows, long pages, long current, long size, boolean hasNext, boolean hasPrevious) {
this.total = total;
this.rows = rows;
this.pages = pages;
this.current = current;
this.size = size;
this.hasNext = hasNext;
this.hasPrevious = hasPrevious;
}
}
二、自定义查询
2.1. 自定义接口

如果想自定义查询那么在UserMapper.java里面写个方法

<User> selectPageVo(@Param("page") Page<User> page,@Param("age") Integer age);
2.2. 自定义查询

UserMapper.xml文件的查询语句

<select id="selectPageVo" resultType="com.mybatis_plus.bean.User">
SELECT id,name,age,email FROM user WHERE age>#{age}
</select>
2.3. 测试自定义分页
@Test
void test02(){
//测试自定义分页
Page<User> page=new Page<>(1,3);
userMapper.selectPageVo(page,20);//调用自定义的查询
System.out.println(page.getRecords());//获取当前页数据 3条记录
System.out.println(page.getSize());//获取每页的条数 3
System.out.println(page.getCurrent()); //获取当前页码 1
System.out.println(page.getPages());//获取总页数 2
System.out.println(page.getTotal());//获取总记录数 4
System.out.println(page.hasNext());//获取有没有下一页 true
System.out.println(page.hasPrevious());//获取是否有上一页 false
}