mybatisPlus中批量刪除的示例代碼
業(yè)務(wù)需求:編輯時,對主表中數(shù)據(jù)的修改,以及子數(shù)據(jù)可以進行刪除,修改,或者新增的操作
前端(主要展示子表):可以點擊新增,也可以在原數(shù)據(jù)上進行編輯,也可以刪除該條數(shù)據(jù)再新增

前端傳值(主要展示子表):

higtRule里面就是子數(shù)據(jù),沒有id的則是點擊新增后新增的數(shù)據(jù),所以既有新增操作,又有編輯操作
后臺處理(主要對子數(shù)據(jù)):
思路:
1:首先查詢?nèi)孔訑?shù)據(jù)
2:創(chuàng)建一個空集合newIds,用于存放未刪除的子數(shù)據(jù)的id,遍歷前端傳遞過來的子數(shù)據(jù),將子數(shù)據(jù)的id存放在newIds集合中
3:過濾數(shù)據(jù)也就是用全部的數(shù)據(jù)去過濾newIds的數(shù)據(jù),如果全部的數(shù)據(jù)中的數(shù)據(jù)和newIds中的數(shù)據(jù)不一致,則就是要刪除的子數(shù)據(jù),使用Java8的stream流處理,該方法返回的就是要刪除的數(shù)據(jù)的集合
4:創(chuàng)建一個空集合deleteIds,用于存放要刪除的子數(shù)據(jù)的id,遍歷deleteList,將id存放在deleteIds中
最后執(zhí)行刪除操作
controller:
/**
* 面積戶表-收費標準-編輯
*
* @param artbFeeStandardVo
* @return
*/
@AutoLog(value = "artb_fee_standard-編輯")
@ApiOperation(value="artb_fee_standard-編輯", notes="artb_fee_standard-編輯")
@PutMapping(value = "/edit")
public Result<?> edit(@RequestBody ArtbStandardVo artbFeeStandardVo) {
return artbFeeStandardService.editArtbFeeStandard(artbFeeStandardVo);
}service層
Result<?> editArtbFeeStandard(ArtbStandardVo artbFeeStandardVo);
實現(xiàn)類(主要是對子數(shù)據(jù)的處理):
@Override
@Transactional
public Result<?> editArtbFeeStandard(ArtbStandardVo artbFeeStandardVo) {
// 更新子數(shù)據(jù)
// 查詢?nèi)砍咭?guī)則
QueryWrapper<ArtbFeeHighRule> oldChildWrapper = new QueryWrapper<>();
oldChildWrapper.eq("standard_id", feeStandard.getId());
List<ArtbFeeHighRule> highRules = highRuleMapper.selectList(oldChildWrapper);
// 存儲前端傳遞過來未刪除的id
List<String> newIds = new ArrayList<>();
for (ArtbFeeHighRule rule : artbFeeStandardVo.getHighRuleList()) {
newIds.add(rule.getId());
}
// 篩選出要刪除的超高數(shù)據(jù),進行批量刪除
List<ArtbFeeHighRule> deleteList = highRules.stream().filter(item -> !newIds.contains(item.getId())).collect(Collectors.toList());
// 存放要刪除的id
List<String> deleteIds = new ArrayList<>();
for (ArtbFeeHighRule rule : deleteList) {
deleteIds.add(rule.getId());
}
if (deleteIds.size() > 0) {
highRuleMapper.deleteBatchById(deleteIds);
}
if (!CollectionUtils.isEmpty(artbFeeStandardVo.getHighRuleList())) {
List<ArtbFeeHighRule> highList = new ArrayList<>();
List<ArtbFeeHighRuleHistory> highHistoryList = new ArrayList<>();
artbFeeStandardVo.getHighRuleList().forEach(item -> {
item.setInsertBaseColumnNoCompanyCode();
if (StringUtils.isEmpty(item.getId())) {
// 說明是新增
item.setId(IdUtil.getCombineId(feeStandard.getId()));
item.setStandardId(feeStandard.getId());
item.setCompanyCode(feeStandard.getCompanyCode());
}
highList.add(item);
// 更新超高歷史表
//操作軌跡
ArtbFeeHighRuleHistory highRuleHistory = new ArtbFeeHighRuleHistory();
BeanUtils.copyProperties(item, highRuleHistory);
highRuleHistory.setStandardId(feeStandardHistory.getId());
highRuleHistory.setCompanyCode(feeStandardHistory.getCompanyCode());
highRuleHistory.setInsertBaseColumnNoCompanyCode();
highRuleHistory.setId(IdUtil.getCombineId(highRuleHistory.getCompanyCode()));
highHistoryList.add(highRuleHistory);
});
// 超過規(guī)則做批量新增或者編輯
highService.saveOrUpdateBatch(highList);
// 超過規(guī)則歷史表做新增操作,記錄具體操作數(shù)據(jù)
highHistoryService.saveBatch(highHistoryList);
}
}mapper中的刪除
/** * 批量刪除超高數(shù)據(jù) * @param deleteIds * @return */ int deleteBatchById(List<String> deleteIds);
xml中
<!--批量刪除超高數(shù)據(jù)-->
<delete id="deleteBatchById" parameterType="java.util.List">
delete from artb_fee_high_rule
where id IN
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>最后返回成功,或者失?。?/p>
if (update > 0) {
return Result.OK("修改成功");
} else {
return Result.error("修改失敗");
}到此這篇關(guān)于mybatisPlus中批量刪除的示例代碼的文章就介紹到這了,更多相關(guān)mybatisPlus 批量刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java sftp下載文件報錯Caused by:com.jcraft.jsch.JSchExcep
文章講述了作者在日常工作中遇到的JSch連接問題,經(jīng)過分析發(fā)現(xiàn)是由于連接泄露導致的,作者提出了解決方案,并給出了使用建議:1.在finally代碼塊中關(guān)閉連接;2.在真正使用階段再創(chuàng)建連接,避免創(chuàng)建后不使用又忘記關(guān)閉連接2024-11-11
Java ArrayList如何實現(xiàn)生成不重復隨機數(shù)
這篇文章主要介紹了Java ArrayList如何實現(xiàn)生成不重復隨機數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
SpringBoot數(shù)據(jù)訪問的實現(xiàn)
本文主要介紹了SpringBoot數(shù)據(jù)訪問的實現(xiàn),引入各種xxxTemplate,xxxRepository來簡化我們對數(shù)據(jù)訪問層的操作,感興趣的可以了解一下2023-11-11

