public TableDataInfo list(TAllParam tAllParam) { startPage(); List<TAllParam> list = tAllParamService.list(new LambdaQueryWrapper<TAllParam>() .like(StringUtils.isNotBlank(tAllParam.getMsc()),TAllParam::getMsc,StringUtils.isNotBlank(tAllParam.getMsc())?tAllParam.getMsc().toUpperCase():"") .like(StringUtils.isNotBlank(tAllParam.getClxh()),TAllParam::getClxh,tAllParam.getClxh()) .like(StringUtils.isNotBlank(tAllParam.getHbpl()),TAllParam::getHbpl(),tAllParam.getHbpl()) .like(StringUtils.isNotBlank(tAllParam.getVehRlzl()),TAllParam::getVehRlzl(),tAllParam.getVehRlzl()) .like(StringUtils.isNotBlank(tAllParam.getClyyxzcbh()),TAllParam::getClyyxzcbh,tAllParam.getClyyxzcbh()) .like(StringUtils.isNotBlank(tAllParam.getVehZbzl()),TAllParam::getVehZbzl(),tAllParam.getVehZbzl()) ); // 遍历List,根据MSC匹配查询T_VEHCERT_PRINT表和T_LOCK_VIN表 for (TAllParam param : list) { String msc = param.getMsc(); List<String> vinList = vehcertPrintService.getVinByMsc(msc); // 根据MSC查询对应的VIN列表 boolean isBlc = false; // 是否为保留车的标志位 for (String vin : vinList) { if (StringUtils.isNotBlank(vin) && lockVinService.isVinExisted(vin)) { isBlc = true; break; } } param.setBlc(isBlc ? "是" : ""); } // 如果查询条件中有blc字段,则按照该字段进行匹配查询 if (StringUtils.isNotBlank(tAllParam.getBlc())) { list = list.stream().filter(param -> tAllParam.getBlc().equals(param.getBlc())).collect(Collectors.toList()); } return getDataTable(list); }能不能优化代码,这样在循环中调用查询方法,每循环一次都要去查询对应的表一次,很拖时间,页面半天出不来
时间: 2024-03-16 07:47:44 浏览: 140
可以优化代码,可以考虑将查询操作放在循环外面,避免每次循环都去查询,可以考虑使用 Map 来存储 msc 和对应的 VIN 列表,然后在循环中根据 msc 取出对应的 VIN 列表进行操作。修改后的代码如下所示:
```
public TableDataInfo list(TAllParam tAllParam) {
startPage();
List<TAllParam> list = tAllParamService.list(new LambdaQueryWrapper<TAllParam>()
.like(StringUtils.isNotBlank(tAllParam.getMsc()),TAllParam::getMsc,StringUtils.isNotBlank(tAllParam.getMsc())?tAllParam.getMsc().toUpperCase():"")
.like(StringUtils.isNotBlank(tAllParam.getClxh()),TAllParam::getClxh,tAllParam.getClxh())
.like(StringUtils.isNotBlank(tAllParam.getHbpl()),TAllParam::getHbpl(),tAllParam.getHbpl())
.like(StringUtils.isNotBlank(tAllParam.getVehRlzl()),TAllParam::getVehRlzl(),tAllParam.getVehRlzl())
.like(StringUtils.isNotBlank(tAllParam.getClyyxzcbh()),TAllParam::getClyyxzcbh,tAllParam.getClyyxzcbh())
.like(StringUtils.isNotBlank(tAllParam.getVehZbzl()),TAllParam::getVehZbzl(),tAllParam.getVehZbzl())
);
// 遍历List,根据MSC匹配查询T_VEHCERT_PRINT表和T_LOCK_VIN表
Map<String, List<String>> mscToVinListMap = new HashMap<>();
for (TAllParam param : list) {
String msc = param.getMsc();
if (!mscToVinListMap.containsKey(msc)) {
List<String> vinList = vehcertPrintService.getVinByMsc(msc);
mscToVinListMap.put(msc, vinList);
}
List<String> vinList = mscToVinListMap.get(msc);
boolean isBlc = false; // 是否为保留车的标志位
for (String vin : vinList) {
if (StringUtils.isNotBlank(vin) && lockVinService.isVinExisted(vin)) {
isBlc = true;
break;
}
}
param.setBlc(isBlc ? "是" : "");
}
// 如果查询条件中有blc字段,则按照该字段进行匹配查询
if (StringUtils.isNotBlank(tAllParam.getBlc())) {
list = list.stream().filter(param -> tAllParam.getBlc().equals(param.getBlc())).collect(Collectors.toList());
}
return getDataTable(list);
}
```
通过以上优化,可以避免在循环中重复查询,提高代码的执行效率。
阅读全文
相关推荐


















<?php
session_start();
include "Conn/conn.php";
include "page.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>博文列表</title>
</head>
<body style=" background-color:#F1F2F6;">
博客系统
<?php
if(isset($_SESSION['username'])) {
?>
个人中心
|
安全退出
<?php
}else {
?>
登录
|
注册
<?php
}
?>
博文列表
标题
作者
查看
发表时间
<?php
//分页
$sql="select id from tb_article ";
$result=mysqli_query($link,$sql);
//计算总条数
$rows = $result->num_rows;
//调用分页函数
Page($rows,4);
$sql = "select * from tb_article limit $select_from $select_limit";
$result=mysqli_query($link,$sql);
//5
if($result && mysqli_num_rows($result)>0){
while($rows=mysqli_fetch_assoc($result)){
?>
<?php echo $rows['title']?>
<?php echo $rows['author']?>
查看
<?php echo date("Y-m-d",$rows['time'])?>
<?php
}
}
?>
<?php echo $pagenav;?>
<?php
include "footer.php";
?>
</body>
</html>在该代码中,添加标题什么的信息,修改代码并输出

