上一篇我们介绍了如何用myBatis-plus进行分页查询;
这一篇让我们看看myBatis-plus的分页查询参数Page;
构造函数
Page类中,有参的构造函数共有4个:
public Page(long current, long size) {
this(current, size, 0);
}
public Page(long current, long size, long total) {
this(current, size, total, true);
}
public Page(long current, long size, boolean isSearchCount) {
this(current, size, 0, isSearchCount);
}
public Page(long current, long size, long total, boolean isSearchCount) {
if (current > 1) {
this.current = current;
}
this.size = size;
this.total = total;
this.isSearchCount = isSearchCount;
}
其中current和size表示要查询的页码和每页的记录数量,那么total和isSearchCount又有什么作用呢?
简单来说:
isSearchCount:表示在查询时,要不要通过count(1)来统计查询结果的总数,默认isSearchCount是true;
total:记录了通过count(1)统计的结果总数,即:表示符合查询条件的记录总数;
接下来我们通过示例来进行说明;
Page(long current, long size)
LambdaQueryWrapper<TStation> queryWrapper = Wrappers.lambdaQuery();
IPage result = page(new Page<>(5,3), queryWrapper);
log.info("listPage total = {},page = {},size = {},pages = {}",
result.getTotal(),result.getCurrent(),result.getSize(),result.getPages());
我们看一下执行日志
==> Preparing: SELECT COUNT(1) FROM t_station
==> Parameters:
<== Columns: COUNT(1)
<== Row: 20
==> Preparing: SELECT id,no FROM t_station limit ? offset ?
==> Parameters: 3(Long), 12(Long)
<== Columns: id, no
<== Row: 1, no-13
<== Row: 1, no-14
<== Row: 1, no-15
<== Total: 3
2021-07-11 19:54:59.438 INFO 2859 --- [ main] s.z.m.p.d.d.s.i.TStationServiceImpl : listPage total = 20,page = 5,size = 3,pages = 7
结果说明:进行了一次count(1)查询,并且total =20;
Page(long current, long size, boolean isSearchCount)
LambdaQueryWrapper<TStation> queryWrapper = Wrappers.lambdaQuery();
IPage result = page(new Page<>(5,3,false), queryWrapper);
log.info("listPageNoSearch total = {},page = {},size = {},pages = {}",
result.getTotal(),result.getCurrent(),result.getSize(),result.getPages());
我们看一下执行日志
==> Preparing: SELECT id,no FROM t_station limit ? offset ?
==> Parameters: 3(Long), 12(Long)
<== Columns: id, no
<== Row: 1, no-13
<== Row: 1, no-14
<== Row: 1, no-15
<== Total: 3
2021-07-11 19:54:59.442 INFO 2859 --- [ main] s.z.m.p.d.d.s.i.TStationServiceImpl : listPageNoSearch total = 0,page = 5,size = 3,pages = 0
结果说明:由于没有进行count(1)查询,所以IPage结果中total和pages 都为0;
Page(long current, long size, long total)
LambdaQueryWrapper<TStation> queryWrapper = Wrappers.lambdaQuery();
IPage result = page(new Page<>(5,3,10), queryWrapper);
log.info("listPageNoSearch total = {},page = {},size = {},pages = {}",
result.getTotal(),result.getCurrent(),result.getSize(),result.getPages());
我们看一下执行日志
==> Preparing: SELECT COUNT(1) FROM t_station
==> Parameters:
<== Columns: COUNT(1)
<== Row: 20
==> Preparing: SELECT id,no FROM t_station limit ? offset ?
==> Parameters: 3(Long), 12(Long)
<== Columns: id, no
<== Row: 1, no-13
<== Row: 1, no-14
<== Row: 1, no-15
<== Total: 3
2021-07-11 19:54:59.446 INFO 2859 --- [ main] s.z.m.p.d.d.s.i.TStationServiceImpl : listPageNoSearch total = 20,page = 5,size = 3,pages = 7
结果说明:虽然我们在查询是设置了total=10,但是由于默认isSearchCount为true,所以进行了count(1),从而total被更新为实际的查询结果总数;
Page(long current, long size, long total, boolean isSearchCount)
LambdaQueryWrapper<TStation> queryWrapper = Wrappers.lambdaQuery();
IPage result = page(new Page<>(5,3,10,false), queryWrapper);
log.info("listPageNoSearch total = {},page = {},size = {},pages = {}",
result.getTotal(),result.getCurrent(),result.getSize(),result.getPages());
我们看一下执行日志:
==> Preparing: SELECT id,no FROM t_station limit ? offset ?
==> Parameters: 3(Long), 12(Long)
<== Columns: id, no
<== Row: 1, no-13
<== Row: 1, no-14
<== Row: 1, no-15
<== Total: 3
2021-07-11 19:54:59.449 INFO 2859 --- [ main] s.z.m.p.d.d.s.i.TStationServiceImpl : listPageNoSearch total = 10,page = 5,size = 3,pages = 4
结果说明:由于将isSearchCount设置为false,从而没有进行count(1),因此total和pages也就是我们设置的值,而非实际查询结果的总数;
总结:
1.在某些复杂的分页查询中,特别是多表关联,大表关联时,为了提高效率,可以把isSearchCount设置为false;
2.当把isSearchCount设置为false后,又想有一个大概的查询结果总数和页数,可以自己设置total;
这种情况特别适用于Google查询的结果返回;