BES-项目中PageHelper的使用

项目中为了减少数据传输并在前段实现分页功能,使用了PageHelper。用PageInfo来返回值。

以Organization表操作为例。

Controller层

controller子项目中的application.yml中对pagehelper的配置:

pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

Controller层仅仅是用PageInfo<VO>重新包装了service层返回的数据。

@PostMapping(value = "/queryByCondition")
    public CommonResponse<PageInfo<OrganizationDataListVO>> queryByCondition(@RequestBody CommonRequest<OrganizationQueryConditionVO> commonRequest){
        CommonRequestUtils.checkCommonRequest(commonRequest);
        OrganizationDTO dto = new OrganizationDTO();
        OrganizationQueryConditionVO organizationQueryConditionVO = commonRequest.getBody();
        BeanUtils.copyProperties(organizationQueryConditionVO,dto);
        try {
            PageInfo<OrganizationDTO> dtoPageInfo = organizationService.queryByCondition(dto);
            List<OrganizationDTO> dtoList = dtoPageInfo.getList();
            List<OrganizationDataListVO> vos = ConversionUtils.simpleListConversion(dtoList, OrganizationDataListVO.class);
            PageInfo<OrganizationDataListVO> voPageInfo = new PageInfo<>();
            BeanUtils.copyProperties(dtoPageInfo, voPageInfo);
            voPageInfo.setList(vos);
            return CommonResponseUtils.makeSuccessCommonResponse(voPageInfo, "0", null, null, null);
        } catch (ServiceException exception) {
            throw new BusinessException(exception);
        } catch (IllegalAccessException | InstantiationException e) {
            throw new BusinessException(SystemExceptionEnum.SYSTEM_ERROR);
        }
    }

Service层

这里使用了通用mapper。service层是关键

步骤:

1. 初始化PageHelper:传入参数pageNum(第几页)、pageSize(每页有几条数据)

PageHelper.startPage(organizationDTO.getPageNum(), organizationDTO.getPageSize());

2. 调用dao层的方法,从数据库中获取数据,存在一个List中

List<Organization> results = organizationDao.selectByExample(condition);

3. 用PageInfo包装数据,PageInfo中将会保存total(总条数)、pages(总页数)等关键数据。

PageInfo<Organization> organizationPageInfo = new PageInfo<Organization>(results);

(PageInfo的成员变量说明:https://blog.csdn.net/weixin_41996974/article/details/81099693

在三个步骤中间不要再有其他对于数据的操作,否则取回的数据或者PageInfo中的分页数据可能会出现错误!

完整的service层代码:

public PageInfo<OrganizationDTO> queryByCondition(OrganizationDTO organizationDTO) throws ServiceException {
        Condition condition = new Condition(Organization.class);
        Example.Criteria criteria = condition.createCriteria();
        if (!StringUtils.isEmpty(organizationDTO.getName())) {
            criteria.andLike("name", organizationDTO.getName() + "%");
        }
        condition.setOrderByClause("updated_time DESC");
        PageHelper.startPage(organizationDTO.getPageNum(), organizationDTO.getPageSize());
        List<Organization> results = organizationDao.selectByExample(condition);
        PageInfo<Organization> organizationPageInfo = new PageInfo<Organization>(results);
        List<OrganizationDTO> dtos = null;
        OrganizationDTO dto = null;
        dtos = new ArrayList<OrganizationDTO>(results.size());
        for (Organization result : results) {
            dto = new OrganizationDTO();
            BeanUtils.copyProperties(result, dto);
            dtos.add(dto);
        }
        PageInfo<OrganizationDTO> organizationDtoPageInfo = new PageInfo<OrganizationDTO>();
        BeanUtils.copyProperties(organizationPageInfo, organizationDtoPageInfo);
        organizationDtoPageInfo.setList(dtos);
        return organizationDtoPageInfo;
    }

Dao层

继承了通用mapper的接口。

@Repository
public interface OrganizationDao extends Mapper<Organization>, MySqlMapper<Organization> {
}
上一篇:从Android触发JavaScript事件


下一篇:cordova 常用命令整理