项目需要全量同步对方的用户机构数据,对方限制了分页条件,就需要递归全量拖过来。下面是实例代码:
// 最大条数
public abstract class BaseLoader<T> {private final static Integer PAGE_SIZE = 1000;private Class<T> tClass;private String uri;public BaseLoader(Class<T> tClass, String uri) {this.tClass = tClass;this.uri = uri;}protected List<T> loadDataArray() {List<T> dataArrayResult = new ArrayList<>();PageNum pageNum = new PageNum();doLoad(dataArrayResult, pageNum);return dataArrayResult;}// 递归分页查找,把所有数据抓过来private void doLoad(List<T> dataArrayResult, PageNum pageNum) {List<T> dataArray = doLoadDataArray(pageNum);if (ZYListUtils.isEmptyList(dataArray)) {return;}dataArrayResult.addAll(dataArray);// 比长度小,没得下一页了if (dataArray.size() < PAGE_SIZE) {return;}// 页数+1pageNum.nextPage();// 递归查询doLoad(dataArrayResult, pageNum);}private List<T> doLoadDataArray(PageNum pageNum) {String url = xxxProperties.getHost()+ uri;Map<String, Object> param = new HashMap<>();param.put("pageNum", pageNum.getPageNum());param.put("pageSize", PAGE_SIZE);JSONArray dataArray = post(url,param);return dataArray.toJavaList(tClass);}
}
public class PageNum {private int pageNum = 1;public int getPageNum() {return pageNum;}public void nextPage() {pageNum++;}
}