av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MyBatis foreach 批量更新實(shí)例

瀏覽:9日期:2023-10-20 10:35:12

在做配置選項(xiàng)(設(shè)備類(lèi)型,所屬樓層等)的時(shí)候,當(dāng)刪除某配置的時(shí)候,我需要檢驗(yàn)該配置是否已被刪除。

@Override public BaseVO deleteOptionDetail(Integer id) { // 合法性驗(yàn)證 if (null == id) { return ParamErrorVO.getInstance(); } ConfigOptionDetail configOptionDetail = configOptionDetailMapper.selectById(id); if (null == configOptionDetail || 1 == configOptionDetail.getIsDeleted()) { return new ErrorVO('該配置不存在'); } if (configOptionDetail.getSystem() == 1) { return new ErrorVO('系統(tǒng)屬性不能刪除'); } if (configOptionDetail.getUseCount() = 0) { return new ErrorVO('配置正在使用不能刪除,請(qǐng)先刪除使用配置的地方'); } // 合法性通過(guò) configOptionDetail.setIsDeleted(1); configOptionDetail.setGmtModefied(Calendar.getInstance().getTime()); configOptionDetailMapper.updateById(configOptionDetail); // 更新內(nèi)存配置 ConfigOptionConstruct.updateOption(); return SuccessVO.getInstance(); }

思考之后我決定采用,給配置選項(xiàng)設(shè)備一個(gè)use_count字段,代表該配置被引用的次數(shù)。 只有當(dāng)該字段值為 0 時(shí),該配置選項(xiàng)記錄才可被刪除。

MyBatis foreach 批量更新實(shí)例

使用情況:

我需要批量刪除房間, 刪除房間的同時(shí),room對(duì)象中使用到了所屬樓層的配置選項(xiàng),我需要將他們的引用減少

@Override public BaseVO deleteRoomByIds(Integer[] ids) { if (null == ids) { return ParamErrorVO.getInstance(); } EntityWrapper<Room> entityWrapper = new EntityWrapper<>(); entityWrapper.where('isdelete={0}', 0); // 核查刪除的房間中是否存在正在使用的設(shè)備 List<Integer> notDelete = deviceInfoService.checkRoomIds(ids); if (null != notDelete && 0 != notDelete.size()) { // 存在仍在使用設(shè)備的房間 entityWrapper.in('id', notDelete); // 查詢(xún)這些房間 List<Room> roomList = roomMapper.selectList(entityWrapper); // 獲取房間的名稱(chēng) StringBuilder stringBuilder = new StringBuilder(roomList.stream().map(Room::getName).collect(Collectors.toList()).toString()); System.out.println(stringBuilder); // TODO: 2018/4/8 可能需要修改提示語(yǔ) return new ErrorVO(stringBuilder + ' 房間存在未刪除的設(shè)備,請(qǐng)先刪除設(shè)備'); } // 房間沒(méi)有設(shè)備在使用 List<Integer> idList = new ArrayList<>(); idList.addAll(Arrays.asList(ids)); // 查詢(xún)需要?jiǎng)h除的房間 entityWrapper.in('id', idList); List<Room> roomList = roomMapper.selectList(entityWrapper); if (null == roomList || idList.size() != roomList.size()) { return new ErrorVO('存在錯(cuò)誤的房間'); } // ******************************************************************************************** 重點(diǎn) // 可以邏輯刪除 int count = roomMapper.logicDeleteByIds(idList); List<Long> optionIds = roomList.stream().map(room -> Long.parseLong(room.getRoomPosition())).collect(Collectors.toList()); Map<Long, Long> optionIdsMap = optionIds.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting())); // 移除所屬樓層配置選項(xiàng)的使用 configOptionDetailService.removeUseCount(optionIdsMap); ConfigOptionConstruct.updateOption(); if (count == idList.size()) { return SuccessVO.getInstance(); } else { return new ErrorVO('部分刪除失敗'); } }

optionIds 是從roomList 房間集合中,通過(guò)stream, 所引用的配置選項(xiàng)id集合

上面我紅字標(biāo)明他們,是因?yàn)椋绻块gA 是一樓, 房間B 也是一樓, 那么我應(yīng)該將一樓的引用減 2。

所以我將optionIds 分組轉(zhuǎn)成Map<配置選項(xiàng)id, 需要減少引用的次數(shù)>

最后一步,也是最重要的進(jìn)行數(shù)據(jù)庫(kù)操作,我希望可以批量更新減少這些引用。

查看MyBatis文檔:

foreach

動(dòng)態(tài) SQL 的另外一個(gè)常用的操作需求是對(duì)一個(gè)集合進(jìn)行遍歷,通常是在構(gòu)建 IN 條件語(yǔ)句的時(shí)候。比如:

<select resultType='domain.blog.Post'> SELECT * FROM POST P WHERE ID in <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> #{item} </foreach></select>

foreach 元素的功能非常強(qiáng)大,它允許你指定一個(gè)集合,聲明可以在元素體內(nèi)使用的集合項(xiàng)(item)和索引(index)變量。它也允許你指定開(kāi)頭與結(jié)尾的字符串以及在迭代結(jié)果之間放置分隔符。這個(gè)元素是很智能的,因此它不會(huì)偶然地附加多余的分隔符。

注意 你可以將任何可迭代對(duì)象(如 List、Set 等)、Map 對(duì)象或者數(shù)組對(duì)象傳遞給 foreach 作為集合參數(shù)。當(dāng)使用可迭代對(duì)象或者數(shù)組時(shí),index 是當(dāng)前迭代的次數(shù),item 的值是本次迭代獲取的元素。當(dāng)使用 Map 對(duì)象(或者 Map.Entry 對(duì)象的集合)時(shí),index 是鍵,item 是值。

<update id='addUseCountByIds'> update config_option_detail set gmt_modified = #{gmtModified}, use_count = use_count + <foreach item='item' index='index' collection='list' open=' case id ' separator=' ' close=' end'> when #{index} then #{item} </foreach> where id in <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> #{index} </foreach></update>

補(bǔ)充:mybatis 用<foreach>根據(jù)ID批量更新時(shí)的一個(gè)注意點(diǎn)。

看接口。傳入一個(gè)Long型的List。

int updateReadCount(@Param(value = 'topicIdList') List<Long> topicIdList);

xml里面循環(huán)update.

<update parameterType='java.util.List'> update CTS set read_count = read_count + 1 where topic_id in <foreach item='item' index='index' collection='topicIdList' open='(' close=')' separator=','> #{item.topicId} </foreach> </update>

就是直接復(fù)制了別人的代碼,改了一改。怎么都跑不通。。。。。。。

問(wèn)題就出在這個(gè)item,item 表示集合中每一個(gè)元素進(jìn)行迭代時(shí)的別名。

List<Long> topicIdList 因?yàn)闀r(shí)Long型(不是entity封裝著的),就不需要?jiǎng)e名了。改為如下就可以跑通了。

<update parameterType='java.util.List'> update CTS set read_count = read_count + 1 where topic_id in <foreach item='topicId' index='index' collection='topicIdList' open='(' close=')' separator=','> #{topicId} </foreach> </update>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章:
主站蜘蛛池模板: 天天躁日日躁狠狠躁白人 | 国产福利在线 | 欧美国产中文 | 拍真实国产伦偷精品 | 亚洲日韩中文字幕一区 | 91在线综合| 四虎影视在线 | 国产精品亚洲一区二区三区在线 | 国产精品美女久久久免费 | 亚洲一区二区三区在线播放 | 一区二区三区四区视频 | 国产乱码精品一区二三赶尸艳谈 | 国产精品国产三级国产aⅴ无密码 | 久久久久久久久久久久91 | 大香在线伊779 | 亚洲男人天堂2024 | 久久成人国产 | 欧美成人a | 亚洲精品99久久久久久 | 中文字幕第一页在线 | 亚洲性在线| 欧美日韩专区 | 西西裸体做爰视频 | 青青久久久 | 美女在线国产 | 久草视频在线播放 | 一区二区三区四区在线 | 综合久久综合久久 | 久久亚洲一区二区三区四区 | 亚洲天堂一区二区 | 亚洲国产精品视频 | 欧美日韩一区二区视频在线观看 | 中文字幕成人免费视频 | 黄网免费看 | 久久一久久 | 日本成人免费网站 | 99精品一区二区 | 日产精品久久久一区二区福利 | 中文字幕 在线观看 | 色一级片| 日韩精品一区二区三区在线播放 |