问题:for循环执行sql查询,想优化一下,想着把for循环组合成一条sql语句执行
代码:
List<DataInfo> dataPotints = (ArrayList<DataInfo>) request.getAttribute("dataPotints");
ArrayList<IotDataHistory> iotDataHistories = new ArrayList<IotDataHistory>();
for (int i = 0; i < dataPotints.size(); i++) {
//这里执行for循环,通过mybatis查询,并将查询的数据放到list里面
iotDataHistories.add(**dataService.getLastData(dataPotints.get(i))**);
}
String responsebody = handlerJson(iotDataHistories);
log.info("获取最后一条数据返回的json数据" + responsebody);
return responsebody;
具体应该如何优化,如何合并成一条sql,改怎么合并?用union、or 还是 其它的 ?
表结构和具体sql是什么?
可以通过for循环拼接需要传入sql的查询参数,然后传入sql执行一次查询