PageHelper分页插件上的PageInfo使用

依赖包

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <!--<version>1.3.0</version>-->
            <version>1.2.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

 工具类

创建ResultCode类

public enum ResultCode {
    // 自定义状态码
    SUCCESS(0, "操作成功!"),
    FAIL(-1,"操作失败!");

    private Integer code;
    private String message;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer code() {
        return this.code;
    }

    public String message() {
        return this.message;
    }

    public static String getMessage(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.message;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }
}

 创建ResultJson类

import cn.xm.entity.KanBan;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;

//返回数据
@Data
public class ResultJson<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	private Integer error_code=0;

	private String message="操作成功!";

	private T data;

	//@JSONField(serialize = false)
	private T json;

	private T list;

	@JsonInclude(value= Include.NON_NULL)
	private T timelist;

	@JsonInclude(value= Include.NON_NULL)
	private Integer totalPage;

	@JsonInclude(value= Include.NON_NULL)
	private long total;

	@JsonInclude(value= Include.NON_NULL)
	private KanBan kb;


	public ResultJson() {
	}




	public ResultJson(Integer error_code, String message, T data) {
		super();
		this.error_code = error_code;
		this.message = message;
		this.data = data;
	}

	//返回成功 带data
	public static ResultJson suc(Object data){
		ResultJson j=new ResultJson();
		j.setData(data);
		return j;
	}

	//全部返回 oklist success
	public static ResultJson okList(Integer error_code, String message, Object list){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setList(list);
		return j;
	}
	//全部返回 okListPage
	public static ResultJson okListPage(Integer error_code, String message, Object list, Integer totalPage){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setList(list);
		j.setTotalPage(totalPage);
		return j;
	}

	//全部返回 okDataPage
	public static ResultJson okDataPage(Integer error_code, String message, Object data, Integer totalPage){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setData(data);
		j.setTotalPage(totalPage);
		return j;
	}

	//全部返回 okdata
	public static ResultJson okData(Integer error_code, String message, Object data){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setData(data);
		return j;
	}

	//全部返回 okJson
	public static ResultJson okJson(Integer error_code, String message, Object json){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setJson(json);
		return j;
	}

	//全部返回 data json
	public static ResultJson okDataJson(Integer error_code, String message,Object data ,Object json){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setData(data);
		j.setJson(json);
		return j;
	}

	//不带数据返回 ok
	public static ResultJson ok(Integer error_code, String message){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setData(new ArrayList());
		return j;
	}

	//不带数据返回  error
	public static ResultJson error(Integer error_code, String message){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		//j.setData(new ArrayList());
		return j;
	}

	//默认 error错误返回
	public static ResultJson error(){
		ResultJson j=new ResultJson();
		j.setError_code(1);
		j.setMessage("操作失败!");
		return j;
	}


	//全部返回 okListPage
	public static ResultJson okDataList(Integer error_code, String message, Object list, Object data){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setList(list);
		j.setData(data);
		return j;
	}


	public ResultJson(Integer error_code) {
		this.error_code = error_code;
	}

	public ResultJson(Integer error_code, String message) {
		super();
		this.error_code = error_code;
		this.message = message;
	}

	public ResultJson(Integer error_code, T data) {
		super();
		this.error_code = error_code;
		this.data = data;
	}



	public static ResultJson HD_Charts(Integer error_code, String message,  Object data,Object list){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setData(data);
		j.setList(list);
		return j;
	}



	public void Result() {}

	public void Result(Integer error_code, String message) {
		this.error_code = error_code;
		this.message = message;
	}

	public static cn.z_currency.bean.ResultJson success() {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setResultCode(ResultCode.SUCCESS);
		return result;
	}


	public static cn.z_currency.bean.ResultJson success(Object data) {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setResultCode(ResultCode.SUCCESS);
		if (data instanceof PageInfo){
			result.setData(((PageInfo<?>) data).getList());
			result.setTotalPage(((PageInfo<?>) data).getPages());
			result.setTotal(((PageInfo<?>) data).getTotal());
		}else {
			result.setData(data);
		}
		return result;
	}
	public static cn.z_currency.bean.ResultJson success(Object data,Object list) {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setList(list);
		result.setResultCode(ResultCode.SUCCESS);
		if (data instanceof PageInfo){
			result.setData(((PageInfo<?>) data).getList());
			result.setTotalPage(((PageInfo<?>) data).getPages());
			result.setTotal(((PageInfo<?>) data).getTotal());
		}else {
			result.setData(data);
		}
		return result;
	}

	public static ResultJson success(Integer error_code, String message,Object data ,Object json){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setData(data);
		j.setJson(json);
		return j;
	}

	public static ResultJson success(Integer error_code, String message){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		return j;
	}

	public static ResultJson success(Integer error_code, String message,Object data){
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		j.setData(data);
		return j;
	}

	public static cn.z_currency.bean.ResultJson fail() {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setResultCode(ResultCode.FAIL);
		return result;
	}

	public static cn.z_currency.bean.ResultJson fail(Integer error_code, String message) {
		ResultJson j=new ResultJson();
		j.setError_code(error_code);
		j.setMessage(message);
		return j;
	}

	public static cn.z_currency.bean.ResultJson fail(Object data) {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setResultCode(ResultCode.FAIL);
		if (data instanceof PageInfo){
			result.setData(((PageInfo<?>) data).getList());
			result.setTotalPage(((PageInfo<?>) data).getPages());
		}else {
			result.setData(data);
		}
		return result;
	}

	public static cn.z_currency.bean.ResultJson failure(ResultCode resultCode) {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setResultCode(resultCode);
		return result;
	}

	public static cn.z_currency.bean.ResultJson failure(ResultCode resultCode, Object data) {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setResultCode(resultCode);
		result.setData(data);
		return result;
	}

	public static cn.z_currency.bean.ResultJson validateFailed(String str) {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setMessage(str);
		return result;
	}

	public static cn.z_currency.bean.ResultJson validateFailed(String str, Exception e) {
		cn.z_currency.bean.ResultJson result = new cn.z_currency.bean.ResultJson();
		result.setMessage(str);
		return result;
	}

	public void setResultCode(ResultCode error_code) {
		this.error_code = error_code.code();
		this.message = error_code.message();
	}

 PageInfo使用

实体

import lombok.Data;

@Data
@Table(name = "cw_refund")//表名
public class Refund {
    /**页码*/
    @Transient
    private int pageIndex;
    /**条数*/
    @Transient
    private int pageSize;
}

Constroller类

@RestController
@RequestMapping("/refund")
public class RefundController {
    /**
     *列表
     */
    @RequestMapping(value = "/List", method = RequestMethod.POST)
    public ResultJson selectAll(@RequestBody Refund refund) {
        PageInfo<Refund> list = refundService.selectAllRefund(refund);
        return ResultJson.success(list);
    }
}

Server类

import com.github.pagehelper.PageInfo;

public interface RefundService {

    PageInfo<Refund> selectAllRefund(Refund refund);

}

Impl类

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;

@Service
public class RefundServicelmpl implements RefundService {

    @Autowired
    private RefundMapper refundMapper;

    @Override
    public PageInfo<Refund> selectAllRefund(Refund refund) {
        //传页数,条数
        PageHelper.startPage(refund.getPageIndex(),refund.getPageSize());
        //下两行代码中间不允许存在其它代码
        List<Refund> list = refundMapper.selectAllRefund(refund);
        PageInfo<Refund> pageInfoList = new PageInfo<>(list);
        return pageInfoList;
    }
}

Mapper类

import java.util.List;

@Repository
public interface RefundMapper extends MyMapper<Refund> {

    List<Refund> selectAllRefund(Refund refund);


}

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cw.dao.RefundMapper"><!--mapper所在路径-->
<!-- id:mapper里方法名,parameterType(传):实体所在路径,resultType(返):实体所在路径-->
<select id="selectAllRefund" parameterType="cn.cw.entity.Refund" resultType="cn.cw.entity.Refund">
      select
         *
      from cw_refund
  </select>

</mapper>

PageInfo的属性

private int pageNum; //当前页
 
private int pageSize; //每页的数量
    
private int size; //当前页的数量
    
//由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
 
private int endRow; //当前页面最后一个元素在数据库中的行号

private long total; //总记录数
    
private int pages; //总页数
    
private List<T> list; //结果集(每页显示的数据)
    
private int firstPage; //第一页
    
private int prePage; //前一页
    
private boolean isFirstPage = false;  //是否为第一页
    
private boolean isLastPage = false; //是否为最后一页
   
private boolean hasPreviousPage = false; //是否有前一页
   
private boolean hasNextPage = false; //是否有下一页
 
private int navigatePages; //导航页码数
 
private int[] navigatepageNums; //所有导航页号

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值