mybatisplus のバージョンは次のとおりです。
<依存関係> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <バージョン>3.4.0</version> </dependency>
まず、クエリ結果が必要になるシナリオについて説明し、次に結果を取得するために直接ページングするのではなくページングについて説明します。
このクエリ内の一部のインジケーターの合計数をカウントする必要がある場合は、すべてのデータを取得して蓄積する必要があります。直接ページネーションした場合は、最初の 10 ページの統計指標のみが取得されますが、これでは十分ではありません。
早速、コードに進みましょう。
private Page<ProductOrderRecord> pageList(List<ProductOrderRecord> productOrderRecords, RecordListJson recordListJson) {
Page<ProductOrderRecord> page = new Page<>(null != recordListJson.getPageNo() ? recordListJson.getPageNo() : 1, null != recordListJson.getPageSize() ? recordListJson.getPageSize() : 10);
page.setRecords(productOrderRecords.isEmpty() ? Collections.emptyList() : startPage(productOrderRecords, null != recordListJson.getPageNo() ? recordListJson.getPageNo() : 1, null != recordListJson.getPageSize() ? recordListJson.getPageSize() : 10));
page.setCurrent(null != recordListJson.getPageNo() ? recordListJson.getPageNo() : 1);
page.setSize(null != recordListJson.getPageSize() ? recordListJson.getPageSize() : 10);
page.setTotal(productOrderRecords.size());
return page;
}
public static List startPage(List list, Integer pageNum, Integer pageSize) {
if (list == null) {
return Collections.emptyList();
}
if (list.isEmpty()) {
return Collections.emptyList();
}
//记录总数
Integer count = list.size();
//开始索引
int fromIndex = (pageNum - 1) * pageSize;
//结束索引
int toIndex = pageNum * pageSize;
if (fromIndex + 1 > count) {
return Collections.emptyList();
}
if (pageNum * pageSize > count) {
toIndex = count;
}
return list.subList(fromIndex, toIndex);
}
主なことは、リスト コレクションの subList メソッドに従ってコレクションを分割することです。必要に応じて使用してください。