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

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

higtRule里面就是子數(shù)據(jù),沒(méi)有id的則是點(diǎn)擊新增后新增的數(shù)據(jù),所以既有新增操作,又有編輯操作
后臺(tái)處理(主要對(duì)子數(shù)據(jù)):
思路:
1:首先查詢(xún)?nèi)孔訑?shù)據(jù)
2:創(chuàng)建一個(gè)空集合newIds,用于存放未刪除的子數(shù)據(jù)的id,遍歷前端傳遞過(guò)來(lái)的子數(shù)據(jù),將子數(shù)據(jù)的id存放在newIds集合中
3:過(guò)濾數(shù)據(jù)也就是用全部的數(shù)據(jù)去過(guò)濾newIds的數(shù)據(jù),如果全部的數(shù)據(jù)中的數(shù)據(jù)和newIds中的數(shù)據(jù)不一致,則就是要?jiǎng)h除的子數(shù)據(jù),使用Java8的stream流處理,該方法返回的就是要?jiǎng)h除的數(shù)據(jù)的集合
4:創(chuàng)建一個(gè)空集合deleteIds,用于存放要?jiǎng)h除的子數(shù)據(jù)的id,遍歷deleteList,將id存放在deleteIds中
最后執(zhí)行刪除操作
controller:
/**
* 面積戶表-收費(fèi)標(biāo)準(zhǔn)-編輯
*
* @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);
實(shí)現(xiàn)類(lèi)(主要是對(duì)子數(shù)據(jù)的處理):
@Override
@Transactional
public Result<?> editArtbFeeStandard(ArtbStandardVo artbFeeStandardVo) {
// 更新子數(shù)據(jù)
// 查詢(xún)?nèi)砍咭?guī)則
QueryWrapper<ArtbFeeHighRule> oldChildWrapper = new QueryWrapper<>();
oldChildWrapper.eq("standard_id", feeStandard.getId());
List<ArtbFeeHighRule> highRules = highRuleMapper.selectList(oldChildWrapper);
// 存儲(chǔ)前端傳遞過(guò)來(lái)未刪除的id
List<String> newIds = new ArrayList<>();
for (ArtbFeeHighRule rule : artbFeeStandardVo.getHighRuleList()) {
newIds.add(rule.getId());
}
// 篩選出要?jiǎng)h除的超高數(shù)據(jù),進(jìn)行批量刪除
List<ArtbFeeHighRule> deleteList = highRules.stream().filter(item -> !newIds.contains(item.getId())).collect(Collectors.toList());
// 存放要?jiǎng)h除的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())) {
// 說(shuō)明是新增
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ò)規(guī)則做批量新增或者編輯
highService.saveOrUpdateBatch(highList);
// 超過(guò)規(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>最后返回成功,或者失敗:
if (update > 0) {
return Result.OK("修改成功");
} else {
return Result.error("修改失敗");
}到此這篇關(guān)于mybatisPlus中批量刪除的示例代碼的文章就介紹到這了,更多相關(guān)mybatisPlus 批量刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java sftp下載文件報(bào)錯(cuò)Caused by:com.jcraft.jsch.JSchExcep
文章講述了作者在日常工作中遇到的JSch連接問(wèn)題,經(jīng)過(guò)分析發(fā)現(xiàn)是由于連接泄露導(dǎo)致的,作者提出了解決方案,并給出了使用建議:1.在finally代碼塊中關(guān)閉連接;2.在真正使用階段再創(chuàng)建連接,避免創(chuàng)建后不使用又忘記關(guān)閉連接2024-11-11
IntelliJ IDEA版Postman強(qiáng)大功能介紹
這篇文章主要為大家介紹了IDEA版Postman的強(qiáng)大功能介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
mybatis接收以逗號(hào)分隔的字符串批量查詢(xún)方式
這篇文章主要介紹了mybatis接收以逗號(hào)分隔的字符串批量查詢(xún)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù)
這篇文章主要介紹了Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn)
本文主要介紹了SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn),引入各種xxxTemplate,xxxRepository來(lái)簡(jiǎn)化我們對(duì)數(shù)據(jù)訪問(wèn)層的操作,感興趣的可以了解一下2023-11-11

