天天看点

SpringDataJpa返回的数据和EasyUI中的数据匹配不上

使用SpringDataJpa和easyUI进行分页查询数据时,获取不了数据,前台能传参,后台也有数据,就是无法访问数据。

因为SpringDataJpa返回的数据和EasyUI中的数据匹配不上

SpringDataJpa返回:{content:…,totalElements:…}

EasyUI需要{rows:…,total:…}

无法匹配,导致数据无法获取

进行兼容

所以准备一个类进行兼容,直接把SpringDataJpa的Page对象进行一次封装,返回给前台即可

import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;

public class UIPage {

    private  List rows = new ArrayList();

    private Long total;

    public UIPage(Page page) {

        //兼容
        this.rows = page.getContent();
        this.total = page.getTotalElements();
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}

           
  • 返回的时候代码如下:
@RequestMapping("/page")
@ResponseBody
public UIPage page(EmployeeQuery query){
    return  new UIPage(employeeService.findPageByQuery(query));
}
           

继续阅读