Last time we showed you how to do paging queries with myBatis-plus;
In this article, let’s look at the paging query parameters Page of myBatis- Plus;
The constructor
There are four constructors that take arguments in the Page class:
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;
}
Copy the code
Where current and size indicate the number of pages to query and the number of records per page, what about total and isSearchCount?
In short:
IsSearchCount: indicates whether to use count(1) to count the total number of query results. By default, isSearchCount is true.
Total: indicates the total number of records that match the search conditions.
Let’s use an example to illustrate this;
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());
Copy the code
Let’s look at the execution log
==> 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 the 19:54:59 2021-07-11. 2859-438 the INFO [main] S.Z.M.P.D.D.S.I.T StationServiceImpl: 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());
Copy the code
Let’s look at the execution log
==> 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 the 19:54:59 2021-07-11. 2859-442 the INFO [main] S.Z.M.P.D.D.S.I.T StationServiceImpl: listPageNoSearch total = 0,page = 5,size = 3,pages = 0
The total and pages are 0 in the IPage result because count(1) is not queried.
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());
Copy the code
Let’s look at the execution log
==> 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 the 19:54:59 2021-07-11. 2859-446 the INFO [main] S.Z.M.P.D.D.S.I.T StationServiceImpl: listPageNoSearch total = 20,page = 5,size = 3,pages = 7
The default isSearchCount is true, so count(1) is used to update the total to the actual number of results.
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());
Copy the code
Let’s look at the execution log:
==> 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 the 19:54:59 2021-07-11. 2859-449 the INFO [main] S.Z.M.P.D.D.S.I.T StationServiceImpl: listPageNoSearch total = 10,page = 5,size = 3,pages = 4
We set isSearchCount to false, so we did not count(1). So total and pages are the values we set instead of the total number of results.
Conclusion:
1. Set isSearchCount to false for more efficient paging queries, especially for multi-table or large table associations.
2. If you want to have an approximate number of results and pages after setting isSearchCount to false, you can set total by yourself.
This is particularly true for results returned from Google queries;