目录
-
- 起因
- 修改
-
- 定义接口
- 重写MyPage的方法
- 实践测试
起因
在我们通过list返回的列表页,出现了一个需要数据合计的需求,例如一个订单1块钱,那么所有订单加起来多少钱,那么list一般都通过分页返回,而统计所有订单又不能只统计分页的那几条数据,因此需要在查询一下数据库,如果通过left join去查询并在list中加个字段好像又不太合适
mybatisPlus自带了分页插件,但是在page中又不能set我们需要返回的合计数据,不过通过上次修改了poi的源码后,我们只要能看到源码就能轻易扒出来源码进行修改
修改
定义接口
继承原先的IPage接口,在此基础上进行扩展
import com.baomidou.mybatisplus.core.metadata.IPage;
public interface MyPageT> extends IPageT> {
/**
* 获取自定义内容
*
* @return 自定义内容
*/
Object getContent();
/**
* 设置自定义内容
* @param content 自定义内容
* @return
*/
MyPageT> setContent(Object content);
}
重写MyPage的方法
通过重写MyPage接口的方式实现diy分页
public class MyPageImplT> implements MyPageT> {
private static final long serialVersionUID = 8545996863226528798L;
/**
* 查询数据列表
*/
protected ListT> records;
/**
* 总数
*/
protected long total;
/**
* 每页显示条数,默认 10
*/
protected long size;
/**
* 当前页
*/
protected long current;
/**
* 排序字段信息
*/
protected ListOrderItem> 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 MyPag服务器托管eImpl(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 ListT> getRecords() {
return this.records;
}
public MyPageImplT> setRecords(ListT> records) {
this.records = records;
return this;
}
public long getTotal() {
return this.total;
}
public MyPageImplT> setTotal(long total) {
this.total = total;
return this;
}
public Object getContent() {
return this.content;
}
public MyPageImplT> setContent(Object content) {
this.content = content;
return this;
}
public long getSize() {
return this.size;
}
public MyPageImplT> setSize(long size) {
this.size = size;
return this;
}
public long getCurrent() {
return this.current;
}
public MyPageImplT> 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(PredicateOrderItem> filter) {
ListString> 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(PredicateOrderItem> 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 MyPageImplT> addOrder(OrderItem... items) {
this.orders.addAll(Arrays.asList(items));
return this;
}
/**
* 添加新的排序条件,构造条件可以使用工厂:{@link OrderItem#(String, boolean)}
*
* @param items 条件
* @return 返回分页参数本身
*/
public MyPageImplT> addOrder(ListOrderItem> items) {
this.orders.addAll(items);
return this;
}
public ListOrderItem> orders() {
return this.orders;
}
public boolean optimizeCountSql() {
return this.optimizeCountSql;
}
public boolean searchCount() {
return this.total >= 0L && this.searchCount;
}
public MyPageImplT> setSearchCount(boolean searchCount) {
this.searchCount = searchCount;
return this;
}
public MyPageImplT> setOptimizeCountSql(boolean optimizeCountSql) {
this.optimizeCountSql = optimizeCountSql;
return this;
}
public long getPages() {
return MyPage.super.getPages();
}
/* --------------- 以下为静态构造方式 --------------- */
public static T> MyPageImplT> of(long current, long size) {
return of(current, size, 0L);
}
public static T> MyPageImplT> of(long current, long size, long total) {
return of(current, size, total, true);
}
public static T> MyPageImplT> of(long current, long size, boolean searchCount) {
return of(current, size, 0L, searchCount);
}
public static T> MyPageImplT> of(long current, long size, long total, boolean searchCount) {
return new MyPageImpl>(current, size, total, searchCount);
}
/**
* --begin------------- 未来抛弃移除的方法 -------------begin--
* 该部分属性转移至 {@link PageDTO}
*/
/**
* @deprecated
*/
@Deprecated
public String getCountId() {
return this.countId;
}
/**
* @deprecated
*/
@Deprecated
public Long getMaxLimit() {
return this.maxLimit;
}
/**
* @deprecated
*/
@Deprecated
public ListOrderItem> getOrders() {
return this.orders;
}
/**
* @deprecated
*/
@Deprecated
public boolean isOptimizeCountSql() {
return this.optimizeCountSql;
}
/**
* @deprecated
*/
@Deprecated
public boolean isSearchCount() {
return this.searchCount;
}
public void setOrders(final ListOrderItem> orders) {
this.orders = orders;
}
public void setCountId(final String countId) {
this.countId = countId;
}
public void setMaxLimit(final Long maxLimit) {
this.maxLimit = maxLimit;
}
/** --end------------- 未来抛弃移除的方法 -------------end-- */
}
实践测试
//获取分页信息
MyPageImplInStoreDtl> page = new MyPageImpl>(pageNo, pageSize);
//返回我们自定义的分页对象
MyPageInStoreDtl> pageList = inStoreDtlMapper.listByMainCode(mainCode, page);
//获取合计的数据
MapString, Object> countMap = inStoreDtlMapper.selectCountNum(mainCode);
pageList.setContent(countMap);
return pageList;
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
一、样式复用 @Styles 1.组件内的 @styles 方法只能在当前组件中使用,全局的 @styles 方法目前只允许在当前的 .ets文件 中使用 2.组件内定义 @styles 方法时不需要使用 function 关键字,全局的 @styles 方法…