Page 分页之 Entity to Domain
结合分页插件 PageHelper
【Page 分页之 Entity to Domain】分页类
public class Page implements Serializable {
private static final long serialVersionUID = 1L;
private int page;
private int pageSize;
private int pages;
private long total;
private List rows;
public Page() {
}public int getPage() {
return this.page;
}public void setPage(int page) {
this.page = page;
}public int getPageSize() {
return this.pageSize;
}public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}public int getPages() {
return this.pages;
}public void setPages(int pages) {
this.pages = pages;
}public long getTotal() {
return this.total;
}public void setTotal(long total) {
this.total = total;
}public List getRows() {
return this.rows;
}public void setRows(List rows) {
this.rows = rows;
}
}
PageUtils
public static Page toPage(List list, Function mapper){
Page page = new Page<>();
if(list instanceof com.github.pagehelper.Page){
com.github.pagehelper.Page pagehelperPage = (com.github.pagehelper.Page) list;
page.setPage(pagehelperPage.getPageNum());
page.setPageSize(pagehelperPage.getPageSize());
page.setTotal(pagehelperPage.getTotal());
page.setPages(pagehelperPage.getPages());
}
List resultList = new ArrayList<>();
list.forEach(item->{
resultList.add(mapper.apply(item));
});
page.setRows(resultList);
return page;
}/**
* 根据查询出的 list entity 对象,转换成 domain 对象 ,返回page对象
* @param dataList数据list
* @param domainInput ,new 一个
* @param 数据库对应的entity对象
* @param 返回给页面的domain 对象
* @return
*/
public static Page getPageDomainForEntity(List dataList,D domainInput){
List lists = new ArrayList<>();
String className = domainInput.getClass().getName();
try {
//entity 转换成 domain
dataList.forEach(item->{
try {
D domain = (D) Class.forName(className).newInstance();
BeanUtils.copyProperties(item, domain);
lists.add(domain);
} catch (Exception e) {
logger.error(" 初始化实体类异常 : "+ className ,e);
}});
} catch (Exception e) {
logger.error("进行 entity 转换 domain 失败 : "+ className ,e);
}//封装 page 信息
Page retPage = new Page<>();
if(dataList instanceof com.github.pagehelper.Page){
com.github.pagehelper.Page pagehelperPage = (com.github.pagehelper.Page) dataList;
retPage.setPage(pagehelperPage.getPageNum());
retPage.setPageSize(pagehelperPage.getPageSize());
retPage.setTotal(pagehelperPage.getTotal());
retPage.setPages(pagehelperPage.getPages());
}
retPage.setRows(lists);
return retPage;
}
应用:
List list = service.find();
Page retPage= PageUtils.getPageDomainForEntity(list ,new YourDomain());
推荐阅读
- PMSJ寻平面设计师之现代(Hyundai)
- 太平之莲
- 闲杂“细雨”
- 七年之痒之后
- 深入理解Go之generate
- 由浅入深理解AOP
- 期刊|期刊 | 国内核心期刊之(北大核心)
- 生活随笔|好天气下的意外之喜
- 感恩之旅第75天
- python学习之|python学习之 实现QQ自动发送消息