给大家介绍一个简单分页的方法(有兴趣的可以自己试一下)
1、实体
package com.hffss.entity.ext;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
@Builder
public class CorpChangeAndAmendmentInfo implements Serializable {
private static final long serialVersionUID = -8474251114162138L;
private Long corpId;
private String changePropertyName;
private Date changeDate;
private String beforeContent;
private String afterContent;
private String columnComment;
private Long id;
private Date amendmentDate;
private String amendmentContent;
private String processedBeforeContent;
private String processedAfterContent;
private Integer isValid;
private Integer amendmentType;
private Date createDate;
private BigDecimal transactionAmount;
private String investorStrIn;
private String investorStrOut;
}
package com.baomidou.mybatisplus.extension.plugins.pagination;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import org.jetbrains.annotations.Nullable;
public class Page<T> implements IPage<T> {
private static final long serialVersionUID = 8545996863226528798L;
private List<T> records;
private long total;
private long size;
private long current;
private List<OrderItem> orders;
private boolean optimizeCountSql;
private boolean isSearchCount;
public Page() {
this.records = Collections.emptyList();
this.total = 0L;
this.size = 10L;
this.current = 1L;
this.orders = new ArrayList();
this.optimizeCountSql = true;
this.isSearchCount = true;
}
public Page(long current, long size) {
this(current, size, 0L);
}
public Page(long current, long size, long total) {
this(current, size, total, true);
}
public Page(long current, long size, boolean isSearchCount) {
this(current, size, 0L, isSearchCount);
}
public Page(long current, long size, long total, boolean isSearchCount) {
this.records = Collections.emptyList();
this.total = 0L;
this.size = 10L;
this.current = 1L;
this.orders = new ArrayList();
this.optimizeCountSql = true;
this.isSearchCount = true;
if (current > 1L) {
this.current = current;
}
this.size = size;
this.total = total;
this.isSearchCount = isSearchCount;
}
public boolean hasPrevious() {
return this.current > 1L;
}
public boolean hasNext() {
return this.current < this.getPages();
}
public List<T> getRecords() {
return this.records;
}
public Page<T> setRecords(List<T> records) {
this.records = records;
return this;
}
public long getTotal() {
return this.total;
}
public Page<T> setTotal(long total) {
this.total = total;
return this;
}
public long getSize() {
return this.size;
}
public Page<T> setSize(long size) {
this.size = size;
return this;
}
public long getCurrent() {
return this.current;
}
public Page<T> setCurrent(long current) {
this.current = current;
return this;
}
/** @deprecated */
@Deprecated
@Nullable
public String[] ascs() {
return CollectionUtils.isNotEmpty(this.orders) ? this.mapOrderToArray(OrderItem::isAsc) : null;
}
private String[] mapOrderToArray(Predicate<OrderItem> filter) {
List<String> columns = new ArrayList(this.orders.size());
this.orders.forEach((i) -> {
if (filter.test(i)) {
columns.add(i.getColumn());
}
});
return (String[])columns.toArray(new String[0]);
}
private void removeOrder(Predicate<OrderItem> filter) {
for(int i = this.orders.size() - 1; i >= 0; --i) {
if (filter.test(this.orders.get(i))) {
this.orders.remove(i);
}
}
}
public Page<T> addOrder(OrderItem... items) {
this.orders.addAll(Arrays.asList(items));
return this;
}
public Page<T> addOrder(List<OrderItem> items) {
this.orders.addAll(items);
return this;
}
/** @deprecated */
@Deprecated
public Page<T> setAscs(List<String> ascs) {
return CollectionUtils.isNotEmpty(ascs) ? this.setAsc((String[])ascs.toArray(new String[0])) : this;
}
/** @deprecated */
@Deprecated
public Page<T> setAsc(String... ascs) {
this.removeOrder(OrderItem::isAsc);
String[] var2 = ascs;
int var3 = ascs.length;
for(int var4 = 0; var4 < var3; ++var4) {
String s = var2[var4];
this.addOrder(OrderItem.asc(s));
}
return this;
}
/** @deprecated */
@Deprecated
public String[] descs() {
return this.mapOrderToArray((i) -> {
return !i.isAsc();
});
}
/** @deprecated */
@Deprecated
public Page<T> setDescs(List<String> descs) {
if (CollectionUtils.isNotEmpty(descs)) {
this.removeOrder((item) -> {
return !item.isAsc();
});
Iterator var2 = descs.iterator();
while(var2.hasNext()) {
String s = (String)var2.next();
this.addOrder(OrderItem.desc(s));
}
}
return this;
}
/** @deprecated */
@Deprecated
public Page<T> setDesc(String... descs) {
this.setDescs(Arrays.asList(descs));
return this;
}
public List<OrderItem> orders() {
return this.getOrders();
}
public List<OrderItem> getOrders() {
return this.orders;
}
public void setOrders(List<OrderItem> orders) {
this.orders = orders;
}
public boolean optimizeCountSql() {
return this.optimizeCountSql;
}
public boolean isSearchCount() {
return this.total < 0L ? false : this.isSearchCount;
}
public Page<T> setSearchCount(boolean isSearchCount) {
this.isSearchCount = isSearchCount;
return this;
}
public Page<T> setOptimizeCountSql(boolean optimizeCountSql) {
this.optimizeCountSql = optimizeCountSql;
return this;
}
}
2、方法
private Page<CorpChangeAndAmendmentInfo> getPageInfo(Integer currentPage, Integer pageSize, List<CorpChangeAndAmendmentInfo> list) {
Page<CorpChangeAndAmendmentInfo> page = new Page<>();
// 总条数
int size = list.size();
page.setTotal(size);
if (currentPage > 0 && pageSize > 0) {
// 总页数
page.setPages(size == 0 ? 0 : size % pageSize == 0 ? size / pageSize : size / pageSize + 1);
// 截取的范围
int index = (currentPage - 1) * pageSize;
list = list.subList(index, (size - index > pageSize ? index + pageSize : size));
// 当前页
page.setCurrent(currentPage);
// 每页数量
page.setSize(pageSize);
}
// 当前页显示的数据
page.setRecords(list);
return page;
}
|