Mybatis批量foreach进行insert、update操作

insert的foreach

@DslDataSource(MASTER)
    @Insert("<script>" +
            "insert into zx_tendaysreport t\n" +
            "  (REPORTID,PLACEPOINTID,USEDAY,GATHERTYPE,REALMONEY,BANCIMONEY,FINANCEMONEY,uploadflag,docno,STATUS)\n" +
            "select zx_tendaysreport_seq.nextval REPORTID,a.* from (\n" +
            " <foreach collection=\"inReportMapList\" item=\"item\" index=\"index\" separator=\"union all\">\n" +
            "(select " +
            "#{item.placepointid} PLACEPOINTID," +
            "to_date(#{item.useday},'yyyy-MM-dd') USEDAY," +
            " #{item.gathertype} GATHERTYPE," +
            "'0' REALMONEY," +
            "'0' BANCIMONEY," +
            "#{item.financemoney} FINANCEMONEY," +
            "'N' uploadflag," +
            "#{item.docno} docno," +
            "'1' STATUS\n" +
            "from dual)\n" +
            " </foreach>"+
            ") a\n" +
            "</script>")
    public int insTenDaysReport(@Param("inReportMapList")List<ReportItem> inReportMapList);

update的foreach

@DslDataSource(MASTER)
    @Update("<script>" +
            "<foreach collection='upReportMapList' item='item' index='index' open='begin' close=';end;' separator=';'>\n" +
            "update zx_tendaysreport set financemoney = #{item.financemoney} where placepointid = #{item.placepointid} and gathertype = #{item.gathertype} and useday = to_date(#{item.useday},'yyyy-MM-dd')" +
            "</foreach>" +
            "</script>")
    public int upTenDaysReport(@Param("upReportMapList")List<ReportItem> upReportMapList);

分批调用mybatis的接口方法


  int inPointDataLimit = 50;//每一批的数量
  List<ReportItem> inNewList = new ArrayList<ReportItem>();
  for (int i = 0; i < inReportMapList.size(); i++) {
      //分批次处理
      inNewList.add(inReportMapList.get(i));
      if(inPointDataLimit == inNewList.size() || i == inReportMapList.size() - 1){
          tenDaysToHubMapper.insTenDaysReport(inNewList);
          inNewList.clear();
      }
  }


  int upPointDataLimit = 50;//每一批的数量
  List<ReportItem> upNewList = new ArrayList<ReportItem>();
  for (int i = 0; i < upReportMapList.size(); i++) {
      //分批次处理
      upNewList.add(upReportMapList.get(i));
      if(upPointDataLimit == upNewList.size() || i == upReportMapList.size() - 1){
          tenDaysToHubMapper.upTenDaysReport(upNewList);
          upNewList.clear();
      }
  }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值