IPage分页记录
@Override
public IPage<RoleListVO> page( Integer pageNo,Integer PageSize ) {
IPage<RoleEntity> page = roleMapper.selectPage(new Page<>(pageNo, PageSize), null);
List<RoleListVO> collect = page.getRecords().stream().map(s -> {
RoleListVO roleListVO = new RoleListVO();
BeanUtils.copyProperties(s, roleListVO);
return roleListVO;
}).collect(Collectors.toList());
Page<RoleListVO> roleListVOPage = new Page<>();
roleListVOPage.setRecords(collect);
roleListVOPage.setCurrent(page.getCurrent());
roleListVOPage.setPages(page.getPages());
roleListVOPage.setTotal(page.getTotal());
return roleListVOPage;
}
使用IPage 有多个问题 就是 实例类的转换,实在不方便,将 RoleEntity 转入 RoleListVO 中原有的page信息不能再使用
还需要进行 进一步的封装来简化代码,但是不需要其他引入。非常简单
PageHelper分页记录
pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.3</version>
</dependency>
Application.yml
# PageHelper 分页插件配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
操作服务类中
@Override
public PageInfo<MenuListVO> page(Integer pageNo, Integer pageSize) {
PageHelper.startPage(pageNo, pageSize);
List<MenuEntity> menuEntities = menuMapper.selectList(Wrappers.lambdaQuery(MenuEntity.class));
List<MenuListVO> menuLists = menuEntities.stream().map(s -> {
MenuListVO menuListVO = new MenuListVO();
BeanUtils.copyProperties(s, menuListVO);
return menuListVO;
}).collect(Collectors.toList());
return new PageInfo<>(menuLists);
}
PageHelper运用中更为方便,代码更为简单。