// 进行分页查询
SmallTool.printMess("数量大于1万分页查询");
//+++++++++++++++++++++++++++++++++++++++++++++
int onePageNum =10000;
int from =1;
int max =0;
List tableNum = Db.use().query("select count(1) as count from " + tableName);
int count = tableNum.get(0).getInt("count");
if (count >0) {
max = Db.use().queryNumber("select max(id) from " + tableName).intValue();
}
int page = max / onePageNum;
if (max % onePageNum !=0) {
page++;
}
List tempNum = Collections.synchronizedList(new ArrayList<>());
List pageInfos =new ArrayList<>();
for (int i =1; i <= page; i++) {
int start = (i -1) * onePageNum + from;
int stop = i * onePageNum + from -1;
if (stop > max) {
stop = max + onePageNum;
}
pageInfos.add(new PageInfo(start, stop));
}
List finalRes = Collections.synchronizedList(new ArrayList<>(10000000));
int finalPage = page;
pageInfos.parallelStream().forEach(pg -> {
List tableData =null;
tempNum.add(pg.getFrom());
SmallTool.printMess(" 现在开始: " + pg.getFrom() +" -- " + pg.getTo() +" 总条数: " +finalPage +" 目前已处理: " +tempNum.size() +" 表名: " +tableName);
try {
tableData = Db.use().query("select * from " +tableName +" where id between " + pg.getFrom() +" and " + pg.getTo());
}catch (SQLException throwables) {
throwables.printStackTrace();
}
finalRes.addAll(tableData);
});
res = finalRes;
多线程查表 存放数据 list 必须是 线程安全的 用synchronizedList 进行存放