目录
- 起因
- 修改
- 定义接口
- 重写MyPage的方法
- 实践测试
起因
在我们通过list返回的列表页,出现了一个需要数据合计的需求,例如一个订单1块钱,那么所有订单加起来多少钱,那么list一般都通过分页返回,而统计所有订单又不能只统计分页的那几条数据,因此需要在查询一下数据库,如果通过left join去查询并在list中加个字段好像又不太合适
mybatisPlus自带了分页插件,但是在page中又不能set我们需要返回的合计数据,不过通过上次修改了poi的源码后,我们只要能看到源码就能轻易扒出来源码进行修改
修改
定义接口
继承原先的IPage接口,在此基础上进行扩展
import com.baomidou.mybatisplus.core.metadata.IPage;public interface MyPage<T> extends IPage<T> {/*** 获取自定义内容** @return 自定义内容*/Object getContent();/*** 设置自定义内容* @param content 自定义内容* @return*/MyPage<T> setContent(Object content);
}
重写MyPage的方法
通过重写MyPage接口的方式实现diy分页
public class MyPageImpl<T> implements MyPage<T> {private static final long serialVersionUID = 8545996863226528798L;/*** 查询数据列表*/protected List<T> records;/*** 总数*/protected long total;/*** 每页显示条数,默认 10*/protected long size;/*** 当前页*/protected long current;/*** 排序字段信息*/protected List<OrderItem> orders;/*** 自动优化 COUNT SQL*/protected boolean optimizeCountSql;/*** 是否进行 count 查询*/protected boolean searchCount;/*** countId*/protected String countId;/*** maxLimit*/protected Long maxLimit;/*** 自定义Object*/protected Object content;public MyPageImpl() {this.records = Collections.emptyList();this.total = 0L;this.size = 10L;this.current = 1L;this.orders = new ArrayList();this.optimizeCountSql = true;this.searchCount = true;this.content = null;}/*** 分页构造函数** @param current 当前页* @param size 每页显示条数*/public MyPageImpl(long current, long size) {this(current, size, 0L);}public MyPageImpl(long current, long size, long total) {this(current, size, total, true);}public MyPageImpl(long current, long size, boolean searchCount) {this(current, size, 0L, searchCount);}public MyPageImpl(long current, long size, long total, boolean searchCount) {this.records = Collections.emptyList();this.total = 0L;this.size = 10L;this.current = 1L;this.orders = new ArrayList();this.optimizeCountSql = true;this.searchCount = true;if (current > 1L) {this.current = current;}this.size = size;this.total = total;this.searchCount = searchCount;}public MyPageImpl(long current, long size, long total, boolean searchCount, Object content) {this.records = Collections.emptyList();this.total = 0L;this.size = 10L;this.current = 1L;this.orders = new ArrayList();this.optimizeCountSql = true;this.searchCount = true;if (current > 1L) {this.current = current;}this.size = size;this.total = total;this.searchCount = searchCount;this.content = content;}/*** 是否存在上一页** @return true / false*/public boolean hasPrevious() {return this.current > 1L;}/*** 是否存在下一页** @return true / false*/public boolean hasNext() {return this.current < this.getPages();}public List<T> getRecords() {return this.records;}public MyPageImpl<T> setRecords(List<T> records) {this.records = records;return this;}public long getTotal() {return this.total;}public MyPageImpl<T> setTotal(long total) {this.total = total;return this;}public Object getContent() {return this.content;}public MyPageImpl<T> setContent(Object content) {this.content = content;return this;}public long getSize() {return this.size;}public MyPageImpl<T> setSize(long size) {this.size = size;return this;}public long getCurrent() {return this.current;}public MyPageImpl<T> setCurrent(long current) {this.current = current;return this;}public String countId() {return this.countId;}public Long maxLimit() {return this.maxLimit;}/*** 查找 order 中正序排序的字段数组** @param filter 过滤器* @return 返回正序排列的字段数组*/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 columns.toArray(new String[0]);}/*** 移除符合条件的条件** @param filter 条件判断*/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);}}}/*** 添加新的排序条件,构造条件可以使用工厂:{@link OrderItem#(String, boolean)}** @param items 条件* @return 返回分页参数本身*/public MyPageImpl<T> addOrder(OrderItem... items) {this.orders.addAll(Arrays.asList(items));return this;}/*** 添加新的排序条件,构造条件可以使用工厂:{@link OrderItem#(String, boolean)}** @param items 条件* @return 返回分页参数本身*/public MyPageImpl<T> addOrder(List<OrderItem> items) {this.orders.addAll(items);return this;}public List<OrderItem> orders() {return this.orders;}public boolean optimizeCountSql() {return this.optimizeCountSql;}public boolean searchCount() {return this.total >= 0L && this.searchCount;}public MyPageImpl<T> setSearchCount(boolean searchCount) {this.searchCount = searchCount;return this;}public MyPageImpl<T> setOptimizeCountSql(boolean optimizeCountSql) {this.optimizeCountSql = optimizeCountSql;return this;}public long getPages() {return MyPage.super.getPages();}/* --------------- 以下为静态构造方式 --------------- */public static <T> MyPageImpl<T> of(long current, long size) {return of(current, size, 0L);}public static <T> MyPageImpl<T> of(long current, long size, long total) {return of(current, size, total, true);}public static <T> MyPageImpl<T> of(long current, long size, boolean searchCount) {return of(current, size, 0L, searchCount);}public static <T> MyPageImpl<T> of(long current, long size, long total, boolean searchCount) {return new MyPageImpl<>(current, size, total, searchCount);}/*** --begin------------- 未来抛弃移除的方法 -------------begin--* 该部分属性转移至 {@link PageDTO}*//*** @deprecated*/@Deprecatedpublic String getCountId() {return this.countId;}/*** @deprecated*/@Deprecatedpublic Long getMaxLimit() {return this.maxLimit;}/*** @deprecated*/@Deprecatedpublic List<OrderItem> getOrders() {return this.orders;}/*** @deprecated*/@Deprecatedpublic boolean isOptimizeCountSql() {return this.optimizeCountSql;}/*** @deprecated*/@Deprecatedpublic boolean isSearchCount() {return this.searchCount;}public void setOrders(final List<OrderItem> orders) {this.orders = orders;}public void setCountId(final String countId) {this.countId = countId;}public void setMaxLimit(final Long maxLimit) {this.maxLimit = maxLimit;}/** --end------------- 未来抛弃移除的方法 -------------end-- */
}
实践测试
//获取分页信息MyPageImpl<InStoreDtl> page = new MyPageImpl<>(pageNo, pageSize);//返回我们自定义的分页对象MyPage<InStoreDtl> pageList = inStoreDtlMapper.listByMainCode(mainCode, page);//获取合计的数据Map<String, Object> countMap = inStoreDtlMapper.selectCountNum(mainCode);pageList.setContent(countMap);return pageList;