PageHelper 使用示例

1. mapper 层返回的 List 的泛型和 service 层返回的PageInfo 的泛型类型一致:

@Override
public PageInfo queryVO1List(String startTime,String endTime,Integer pageNum,Integer pageSize) {
    boolean pageStarted = false;
    try {
        PageHelper.startPage( pageNum,pageSize );
        pageStarted = true;
        List vo1List = this.xxxMapper.queryVO1List( startTime,endTime );
        if( vo1List == null || vo1List.size() == 0 ){
            return new PageInfo(new ArrayList<>(0));
        }
        return new PageInfo( vo1List );
    } catch (Exception e) {
        log.error(Constants.EXCEPTION_LOG_PREFIX,e);
        return new PageInfo(new ArrayList<>(0));
    }finally {
        if( pageStarted ){
            PageHelper.clearPage();
        }
    }
}

2. mapper 层返回的 List 的泛型和 service 层返回的 PageInfo 的泛型类型不一致:

@Override
public PageInfo queryVO2List(String startTime,String endTime,Integer pageNum,Integer pageSize) {
    boolean pageStarted = false;
    try {
        PageHelper.startPage( pageNum,pageSize );
        pageStarted = true;
        List vo1List = this.xxxMapper.queryVO1List( startTime,endTime );
        if( vo1List == null || vo1List.size() == 0 ){
            return new PageInfo(new ArrayList<>(0));
        }
        PageInfo pageInfo_vo1 = new PageInfo<>(vo1List);
        List vo2List = this.vo1ListToVO2List( vo1List );
        PageInfo pageInfo_vo2 = new PageInfo<>();
        pageInfo_vo1.setList( null );
        BeanUtils.copyProperties( pageInfo_vo1,pageInfo_vo2 );
        pageInfo_vo2.setList( vo2List );
        return pageInfo_vo2;
    } catch (Exception e) {
        log.error(Constants.EXCEPTION_LOG_PREFIX,e);
        return new PageInfo(new ArrayList<>(0));
    }finally {
        if( pageStarted ){
            PageHelper.clearPage();
        }
    }
}

你可能感兴趣的:(mybatis,PageHelper,java,springboot,mybatis,mysql,PageHelper,数据库分页)