分页插件(Mybatis-Plus)

分页配置

MybatisPlusConfig自定义类中开启分页插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Component
@MapperScan("com.catnyan.mapper")
public class MybatisPlusConfig {
/**
* 新版3.4.3.1
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//开启分页插件
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));

return mybatisPlusInterceptor;
}
}

使用分页

1
2
3
4
5
6
@Test
void page() {
Page<User> page = new Page<>(1, 2);//第一页最多显示两条记录
userMapper.selectPage(page, null);
page.getRecords().forEach(System.out::println);
}

执行结果:

1
2
3
4
5
6
==>  Preparing: SELECT id,name,password,create_time,update_time,version,deleted FROM user WHERE deleted=0 LIMIT ?
==> Parameters: 2(Long)
<== Columns: id, name, password, create_time, update_time, version, deleted
<== Row: 1, name, 123456, null, null, 1, 0
<== Row: 2, van, 123, null, 2021-08-05 14:28:38.404, 2, 0
<== Total: 2