查询接口的分页实现

目录

1、导入PageHelper依赖

2、写Page工具类

3、开始写查询接口(以实体类Person为例)

1、导入PageHelper依赖


	com.github.pagehelper
	pagehelper-spring-boot-starter
	1.4.1

2、写Page工具类

import lombok.Data;

import java.util.List;

@Data
public class Page {

    // 当前页码
    private int pageNum;

    // 每页数据量
    private int pageSize;

    // 总记录数
    private long total;

    // 数据列表
    private List content;

    // 省略

    // 计算总页数
    public int getTotalPages() {
        return (int) Math.ceil((double) total / (double) pageSize);
    }

    // 上一页页码
    public int getPrevPage() {
        return pageNum > 1 ? pageNum - 1 : 1;
    }

    // 下一页页码
    public int getNextPage() {
        int totalPages = getTotalPages();
        return pageNum < totalPages ? pageNum + 1 : totalPages;
    }

    // 是否有上一页
    public boolean hasPrevPage() {
        return pageNum > 1;
    }

    // 是否有下一页
    public boolean hasNextPage() {
        return pageNum < getTotalPages();
    }

    // 设置当前页数据列表
    public void setContent(List list) {
        this.content = list;
    }
}

3、开始写查询接口(以实体类Person为例)

   (1)Person实体类

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("person")
public class Person {

    @TableId(type = IdType.AUTO)
    @Column(name = "id")
    private Long Id;

    private String name;

    private String gender;

    private Integer age;

    @TableField(exist = false)
    private List ids;

    public Person(List ids) {
        this.ids = ids;
    }

}

   (2)Mapper层接口

@Mapper
public interface PersonMapper{

    List selectPersonList(Person person);


}

     (3)PersonMapper.xml(注意实体类路径那里根据自己的包名自行修改)

    
        select
            P.id,
            P.name,
            P.gender,
            P.age
         from person P
    

    
        
        
        
        
    

    

(4)Service层

        PersonService:

public interface PersonService extends IService {

    Page selectPersonList(Person person);


}

        PersonServiceImpl

@Service
public class PersonServiceImpl extends ServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;

    @Override
    public Page selectPersonList(Person person,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List persons = personMapper.selectPersonList(person);
        Page perspnsPage = new Page<>(persons);
        personsPage.setPageNum(pageNum);
        personsPage.setPageSize(pageSize);
        personsPage.setTotal(persons.size());
        return personsPage;
    }
}

(4)Controller层

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @PostMapping("/list")
    public JSONResult PersonList(@RequestBody(required = false) Person person,@RequestParam Integer pageNum,@RequestParam pageSize) {

        person = person == null ? new Person() : person;
        return JSONResult.ok(personService.selectPersonList(person));
        //这里的JSONResult是统一结果返回集,不懂可看之前文章
    }
}

         JsonResult工具类:

/**
 * @Description: 自定义响应数据结构 这个类是提供给门户,ios,安卓,微信商城用的
 * 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list) 其他自行处理 200:表示成功
 * 500:表示错误,错误信息在msg字段中 501:bean验证错误,不管多少个错误都以map形式返回
 * 502:拦截器拦截到用户token出错 555:异常抛出信息 401:请求未授权 400:请求参数不支持
 */
public class JSONResult implements Serializable {

    private static final long serialVersionUID = 1L;

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Object data;

    @JsonIgnore
    private String ok; // 不使用

    public static JSONResult build(Integer status, String msg, Object data) {
        return new JSONResult(status, msg, data);
    }

    public static JSONResult ok(Object data) {
        return new JSONResult(data);
    }

    public static JSONResult ok() {
        return new JSONResult(null);
    }

    public static JSONResult errorMsg(String msg) {
        return new JSONResult(500, msg, null);
    }

    public static JSONResult errorMap(Object data) {
        return new JSONResult(501, "error", data);
    }

    public static JSONResult errorTokenMsg(String msg) {
        return new JSONResult(502, msg, null);
    }

    public static JSONResult errorException(String msg) {
        return new JSONResult(555, msg, null);
    }

    public static JSONResult errorAuthorized(String msg) {
        return new JSONResult(401, msg, null);
    }

    public static JSONResult errorMethodArgument(String msg, Object data) {
        return new JSONResult(400, msg, data);
    }

    public JSONResult() {

    }

    public JSONResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public JSONResult(Object data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
    }

    public Boolean isOK() {
        return this.status == 200;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getOk() {
        return ok;
    }

    public void setOk(String ok) {
        this.ok = ok;
    }

}

你可能感兴趣的:(java,mybatis,spring,boot)