解决github的pageHelper中查询出来的分页对象PageInfo<PO>优雅转化PageInfo<DTO>的问题
在我们使用github的pageHelper的时候,我们查出来的是PO对象,我们往往在上层需要DTO或VO回想,这里我写了一个工具类来优雅的转化PageInfo类型工具类 PageInfoUtil.javaimport com.github.pagehelper.Page;import com.github.pagehelper.PageInfo;import org.springfr...
文章共206字 · 阅读需要大约1分钟
一键AI生成摘要,助你高效阅读
问答
·
在我们使用github的pageHelper的时候,我们查出来的是PageInfo的PO对象,我们往往在上层需要PageInfo的DTO或VO对象,这里我写了一个工具类来优雅的转化PageInfo类型
工具类 PageInfoUtil.java
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
/**
* @description:github Pagehelper工具
* @Author:yuyufeng
* @Date:2019/7/22 10:38
*/
public class PageInfoUtil {
public static <P, D> PageInfo<D> pageInfo2PageInfoDTO(PageInfo<P> pageInfoPO, Class<D> dClass) {
Page<D> page = new Page<>(pageInfoPO.getPageNum(), pageInfoPO.getPageSize());
page.setTotal(pageInfoPO.getTotal());
for (P p : pageInfoPO.getList()) {
D d = null;
try {
d = dClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
BeanUtils.copyProperties(p, d);
page.add(d);
}
return new PageInfo<>(page);
}
}
使用
PageHelper.startPage(1, 10, "crreate_time desc");
List<Product> list = productMapper.select(product);
PageInfo pageInfo = new PageInfo(list);
PageInfo<ProductDTO> productDTOPageInfo = PageInfoUtil.pageInfo2PageInfoDTO(pageInfo, ProductDTO.class);
更多推荐
已为社区贡献1条内容
所有评论(0)