com.baomidou.mybatisplus.extension.plugins.pagination.Page Maven / Gradle / Ivy
Show all versions of mybatis-plus-extension Show documentation
/*
* Copyright (c) 2011-2020, hubin ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.extension.plugins.pagination;
import java.util.Collections;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
/**
*
* 简单分页模型
*
*
* @author hubin
* @since 2018-06-09
*/
public class Page implements IPage {
private static final long serialVersionUID = 8545996863226528798L;
/**
* 查询数据列表
*/
private List records = Collections.emptyList();
/**
* 总数,当 total 为 null 或者大于 0 分页插件不在查询总数
*/
private long total = 0;
/**
* 每页显示条数,默认 10
*/
private long size = 10;
/**
* 当前页
*/
private long current = 1;
/**
*
* SQL 排序 ASC 数组
*
*/
private String[] ascs;
/**
*
* SQL 排序 DESC 数组
*
*/
private String[] descs;
/**
*
* 自动优化 COUNT SQL
*
*/
private boolean optimizeCountSql = true;
public Page() {
// to do nothing
}
/**
*
* 分页构造函数
*
*
* @param current 当前页
* @param size 每页显示条数
*/
public Page(long current, long size) {
this(current, size, 0L);
}
public Page(long current, long size, Long total) {
if (current > 1) {
this.current = current;
}
this.size = size;
this.total = total;
}
/**
*
* 是否存在上一页
*
*
* @return true / false
*/
public boolean hasPrevious() {
return this.current > 1;
}
/**
*
* 是否存在下一页
*
*
* @return true / false
*/
public boolean hasNext() {
return this.current < this.getPages();
}
@Override
public List getRecords() {
return this.records;
}
@Override
public IPage setRecords(List records) {
this.records = records;
return this;
}
@Override
public long getTotal() {
return this.total;
}
@Override
public IPage setTotal(long total) {
this.total = total;
return this;
}
@Override
public long getSize() {
return this.size;
}
@Override
public IPage setSize(long size) {
this.size = size;
return this;
}
@Override
public long getCurrent() {
return this.current;
}
@Override
public IPage setCurrent(long current) {
this.current = current;
return this;
}
@Override
public String[] ascs() {
return ascs;
}
public IPage setAscs(List ascs) {
if (CollectionUtils.isNotEmpty(ascs)) {
this.ascs = ascs.toArray(new String[0]);
}
return this;
}
/**
*
* 升序
*
*
* @param ascs 多个升序字段
* @return
*/
public IPage setAsc(String... ascs) {
this.ascs = ascs;
return this;
}
@Override
public String[] descs() {
return descs;
}
public IPage setDescs(List descs) {
if (CollectionUtils.isNotEmpty(descs)) {
this.descs = descs.toArray(new String[0]);
}
return this;
}
/**
*
* 降序
*
*
* @param descs 多个降序字段
* @return
*/
public IPage setDesc(String... descs) {
this.descs = descs;
return this;
}
@Override
public boolean optimizeCountSql() {
return optimizeCountSql;
}
public IPage setOptimizeCountSql(boolean optimizeCountSql) {
this.optimizeCountSql = optimizeCountSql;
return this;
}
}