当前位置: 代码迷 >> 综合 >> Mybatis批量删除多表
  详细解决方案

Mybatis批量删除多表

热度:94   发布时间:2024-02-19 18:40:36.0

Mybatis要批量删除多表的数据,怎么办 ?今天又同事问到了

一:Mybatis里面处理

<!-- 删除多个表的信息  -->
<delete id="deleteByFrameId" statementType="STATEMENT"><foreach collection="tableList" item="tableName" index="index" >delete from ${tableName} where gov_frame_id = ${ govFrameId} ;</foreach>
</delete>

说明:tableList 是参数,govFrameId也是参数

           记得使用 $ 关键字,不要使用 # 关键字

二:java内传参

//1.0 删除历史已经存储,但是有可能发生了异常的数据
Map<String, Object> params = Maps.newHashMap();
String[] tableList = {"gov_price_desc", "gov_price_category", "gov_price_category_detail"};
params.put("tableList", tableList);
params.put("govFrameId", govHandlerStep2VO.getGovPriceFrame().getId());
govPriceDescMapper.deleteByFrameId(params);

说明:数组 和 List都可以

三:数据库连接加上 允许批量更新

jdbc:mysql://192.168.3.3:3316/dev_db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true

一定要加上,否则执行不了:&allowMultiQueries=true

  相关解决方案