controller:
service:
问题描述:service使用分页插件将dao查询数据库返回的结果集,封装成page对象,返回到controller,service的page对象中total的值还在,到controller后,page中total的值就不见了
原因:因为dubbo远程调用对象,对象要序列化后才能传输,page因为继承了ArrayList,在序列化的时候,page的成员变量(total)并不会序列化,所以controller的page的total才会丢失值
(原因更新:
1.基本数据类型不会序列化,应该用包装类
2.page不应该直接返回,因为使用dubbo远程调用,应该包装一下返回controller)
解决方案:(新增第三种解决方案)
1.指定dubbo的序列化方式(不建议)
2.使用PageInfo对象传输
service:
controller:
3.自定义一个类PageResult,用来封装分页结果,返回controller
import java.io.Serializable;
import java.util.List;
/**
* 分页结果封装对象
*/
public class PageResult<T> implements Serializable{
private Long total;//总记录数
private List<T> rows;//当前页结果
public PageResult(Long total, List<T> rows) {
super();
this.total = total;
this.rows = rows;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
service:
/**
* 分页查询
* @param queryPageBean
* @return
*/
@Override
public PageResult<Package> findPage(QueryPageBean queryPageBean) {
//判断分页条件是否为空,加上%
if (queryPageBean.getQueryString() != null) {
queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
}
//使用分页插件,注意:只对下一行代码进行处理,查询方法必须放在之后
PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//处理查询结果,这里的查询方法查询的是当前条件的所有记录,没有limit
//将查询结果封装成page,这里已经分页
Page<Package> page = packageDao.findAllPackage(queryPageBean.getQueryString());
//封装分页数据,返回controller
PageResult<Package> pageResult = new PageResult<>(page.getTotal(),page.getResult());
return pageResult;
}
controller:
/**
* 分页查询
* @param queryPageBean
* @return
*/
@PostMapping(path = "/findPage")
@@ResponseBody //将javabean 转换成json 返回前端
public Result findPage(@RequestBody QueryPageBean queryPageBean){
//接受分页结果对象
PageResult<Package> pageResult = packageService.findPage(queryPageBean);
//返回前端
return new Result(true,MessageConstant.ADD_PACKAGE_SUCCESS,pageResult);
}