if (lastResult.isEmpty()) { toBeInserted = result; } else { // 遍历上一次查询结果,看看是否需要更新或删除 for (Map<String, Object> last : lastResult) { boolean found = false; for (Map<String, Object> current : result) { if (last.get("jsbh").equals(current.get("jsbh")) && last.get("sm").equals(current.get("sm")) && last.get("code").equals(current.get("code")) ) { found = true; break; } } if (!found) { toBeDeleted.add(last); } } // 遍历当前查询结果,看看是否需要插入或更新 for (Map<String, Object> current : result) { boolean found = false; for (Map<String, Object> last : lastResult) { if (last.get("jsbh").equals(current.get("jsbh")) && last.get("sm").equals(current.get("sm")) && last.get("code").equals(current.get("code")) ) { found = true; break; } } if (found) { toBeUpdated.add(current); } else { toBeInserted.add(current); } } } // 批量插入 if (!toBeInserted.isEmpty()) { jxMjKhTxService.batchInsert(toBeInserted); } // 批量更新 if (!toBeUpdated.isEmpty()) { jxMjKhTxService.batchUpdate(toBeUpdated); } // 批量删除 if (!toBeDeleted.isEmpty()) { jxMjKhTxService.batchDelete(toBeDeleted); } // 保存本次查询结果 lastResult = result; 相同数据会重复新增 之前有的数据这次没有了为什么没有删除掉 存在相同数据重复插入一级之前有的数据现在没有了也还会插入进去、 代码修改
时间: 2024-02-02 21:05:26 浏览: 76
这段代码中,如果数据重复出现,会导致重复插入,需要在插入之前进行判断并去重。同时,如果之前有的数据现在没有了,应该在遍历完当前查询结果后,再遍历上一次查询结果进行删除处理。以下是修改后的代码:
```java
if (lastResult.isEmpty()) {
toBeInserted = result;
} else {
List<Map<String, Object>> newToBeInserted = new ArrayList<>();
List<Map<String, Object>> newToBeUpdated = new ArrayList<>();
List<Map<String, Object>> newToBeDeleted = new ArrayList<>();
// 遍历上一次查询结果,看看是否需要更新或删除
for (Map<String, Object> last : lastResult) {
boolean found = false;
for (Map<String, Object> current : result) {
if (last.get("jsbh").equals(current.get("jsbh")) && last.get("sm").equals(current.get("sm")) && last.get("code").equals(current.get("code")) ) {
found = true;
break;
}
}
if (!found) {
newToBeDeleted.add(last);
}
}
// 遍历当前查询结果,看看是否需要插入或更新
for (Map<String, Object> current : result) {
boolean found = false;
for (Map<String, Object> last : lastResult) {
if (last.get("jsbh").equals(current.get("jsbh")) && last.get("sm").equals(current.get("sm")) && last.get("code").equals(current.get("code")) ) {
found = true;
break;
}
}
if (found) {
newToBeUpdated.add(current);
} else {
// 判断是否已存在相同数据
boolean exist = false;
for (Map<String, Object> inserted : toBeInserted) {
if (inserted.get("jsbh").equals(current.get("jsbh")) && inserted.get("sm").equals(current.get("sm")) && inserted.get("code").equals(current.get("code")) ) {
exist = true;
break;
}
}
if (!exist) {
newToBeInserted.add(current);
}
}
}
// 批量插入
if (!newToBeInserted.isEmpty()) {
jxMjKhTxService.batchInsert(newToBeInserted);
}
// 批量更新
if (!newToBeUpdated.isEmpty()) {
jxMjKhTxService.batchUpdate(newToBeUpdated);
}
// 批量删除
if (!newToBeDeleted.isEmpty()) {
jxMjKhTxService.batchDelete(newToBeDeleted);
}
// 保存本次查询结果
lastResult = result;
}
```
阅读全文