MyBatis-Plus自定义分页模型

MyBatis-Plus自带的分页模型Page有些参数,我觉得不是很必要,因此自定义自己的分页模型。该类继承了 IPage 类,实现了简单分页模型如果你要实现自己的分页模型可以继承 Page 类或者实现 IPage 类。因为Java是单继承多实现的,所以我们使用实现IPage接口的方式实现我们自己的分页模型。

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.Data;

import java.util.Collections;
import java.util.List;

@Data
public class PageImpl<T> implements IPage<T> {
    
    
    private static final long serialVersionUID = 1L;
    protected long current; //当前页页码
    protected long size; //当前页数据数量
    protected long total; //数据总量
    protected List<T> records; //记录

    public static <T> PageImpl<T> of(long current, long size){
    
    
        return of(current,size,0L);
    }

    public static <T> PageImpl<T> of(long current, long size, long total){
    
    
        return new PageImpl<>(current,size,total);
    }

    public PageImpl(long current, long size, long total) {
    
    
        this.records = Collections.emptyList();
        this.current = Math.max(current, 1L);
        this.size = size;
        this.total = total;
    }

    @Override
    public List<OrderItem> orders() {
    
    
        return null;
    }

    @Override
    public List<T> getRecords() {
    
    
        return this.records;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
    
    
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
    
    
        return this.total;
    }

    @Override
    public IPage<T> setTotal(long total) {
    
    
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
    
    
        return this.size;
    }

    @Override
    public IPage<T> setSize(long size) {
    
    
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
    
    
        return this.current;
    }

    @Override
    public IPage<T> setCurrent(long current) {
    
    
        this.current = current;
        return this;
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43933728/article/details/131586996