Mybatis 自带分页功能,但是该分页功能是基于内存的分页,也就是会讲所有符合条件的数据查询出来,然后在从内存中获取当前页的信息,这种方式在数据量大的情况下会存在严重的性能问题。
我们通过 Mybatis-Plus 自带的分页插件可以很好的解决这个问题,实现步骤记录如下:
1、 添加配置类,示例内容如下:
package com.paratera.protect.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;
/**
* Mybatis Plus 配置类,主要用于继承分页插件
* @author 朱治龙
* @date 2023-10-19 23:14:00
*/
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
2、使用分页方法查询
2.1、selectPage
2.1.1 示例代码
@Test
void testSelectPage() {
QueryWrapper<Staff> qw = Wrappers.query();
qw.ge("age", 30);
Page<Staff> page = new Page<Staff>(1, 2);
// 不查记录数
// page.setSearchCount(false);
Page<Staff> pageData = staffMapper.selectPage(page, qw);
System.out.println("总页数:" + pageData.getPages());
System.out.println("总记录数:" + pageData.getTotal());
pageData.getRecords().forEach(System.out::println);
}
2.1.2 查询结果
DEBUG==> Preparing: SELECT COUNT(*) AS total FROM staff WHERE (age >= ?)
DEBUG==> Parameters: 30(Integer)
TRACE<== Columns: total
TRACE<== Row: 4
DEBUG<== Total: 1
DEBUG==> Preparing: SELECT id,name,age,email,mobile,manager_id,create_time FROM staff WHERE (age >= ?) LIMIT ?
DEBUG==> Parameters: 30(Integer), 2(Long)
TRACE<== Columns: id, name, age, email, mobile, manager_id, create_time
TRACE<== Row: 1087982257332887553, 大boss, 40, 001@paratera.com, 13888886666, null, 2019-01-11 14:20:20
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@blsc.com, 18684700070, 1088248166370832385, 2019-01-14 09:15:15
DEBUG<== Total: 2
总页数:2
总记录数:4
Staff(id=1087982257332887553, name=大boss, age=40, email=001@paratera.com, mobile=13888886666, managerId=null, createTime=2019-01-11T14:20:20)
Staff(id=1094590409767661570, name=张雨琪, age=31, email=zjq@blsc.com, mobile=18684700070, managerId=1088248166370832385, createTime=2019-01-14T09:15:15)
2.2、selectMapsPage
2.2.1 示例代码
@Test
void testSelectMapsPage() {
QueryWrapper<Staff> qw = Wrappers.query();
qw.ge("age", 30);
Page<Map<String, Object>> page2 = new Page<>(1, 2);
Page<Map<String, Object>> pageData2 = staffMapper.selectMapsPage(page2, qw);
System.out.println("总页数:" + pageData2.getPages());
System.out.println("总记录数:" + pageData2.getTotal());
pageData2.getRecords().forEach(System.out::println);
}
2.2.2 查询结果
DEBUG==> Preparing: SELECT COUNT(*) AS total FROM staff WHERE (age >= ?)
DEBUG==> Parameters: 30(Integer)
TRACE<== Columns: total
TRACE<== Row: 4
DEBUG<== Total: 1
DEBUG==> Preparing: SELECT id,name,age,email,mobile,manager_id,create_time FROM staff WHERE (age >= ?) LIMIT ?
DEBUG==> Parameters: 30(Integer), 2(Long)
TRACE<== Columns: id, name, age, email, mobile, manager_id, create_time
TRACE<== Row: 1087982257332887553, 大boss, 40, 001@paratera.com, 13888886666, null, 2019-01-11 14:20:20
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@blsc.com, 18684700070, 1088248166370832385, 2019-01-14 09:15:15
DEBUG<== Total: 2
总页数:2
总记录数:4
{create_time=2019-01-11T14:20:20, name=大boss, mobile=13888886666, id=1087982257332887553, age=40, email=001@paratera.com}
{create_time=2019-01-14T09:15:15, manager_id=1088248166370832385, name=张雨琪, mobile=18684700070, id=1094590409767661570, age=31, email=zjq@blsc.com}
3、附加说明
使用分页插件查询时默认是会执行两条 SQL,一条获取当前页的数据,一条获取总记录数。在某些场景下(如瀑布流模式),只需要获取当前页的内容即可,不需要总记录数相关分页数值,此时可在创建 Page 时,第三个参数给值为 false:Page(long current, long size, boolean searchCount)
。也可使用 page.setSearchCount(false);
评论 (0)