zoukankan      html  css  js  c++  java
  • mybatis batch批量提交大量数据

    转载:https://blog.csdn.net/Java_Mr_Zheng/article/details/50476757

    在xml文件配置多条参数同时插入:

    <insert id="insertBatch2" parameterType="ctas.entity.SharkFlt">
         <selectKey keyProperty="recId" order="BEFORE" resultType="Long">
          select SEQ_CTAS_SHARK_FLT.nextval as recId from dual
       </selectKey>
         insert into CTAS_SHARK_FLT (<include refid="Base_Column_List"/>) SELECT SEQ_TEST.NEXTVAL, A.*
         FROM (
         <foreach collection="list" item="item" index="index" open="" close="" separator="union all">
           select #{item.awbType,jdbcType=VARCHAR}, #{item.awbPre,jdbcType=VARCHAR},... from dual
      </foreach>
        ) A
     </insert>

    在Java代码中,oracle中一次执行的sql语句长度是有限制的,如果最后拼出来的sql字符串过长,会导致执行失败,所以java端还要做一个分段处理,参考下面的处理:

    List<SharkFlt> data = new ArrayList<SharkFlt>();
              for (TSharkFlt f : sharkFlts) {
                 data.add(getSharkFlt(f));
             }
     
            System.out.println(data.size());
             long beginTime = System.currentTimeMillis();
            System.out.println("开始插入...");
             SqlSessionFactory sqlSessionFactory 
    =ctx.getBean(SqlSessionFactory.class);
             SqlSession session = null;
            try {
                 session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
                 int a = 2000;//每次提交2000条
                 int loop = (int) Math.ceil(data.size() / (double) a);
     
                 List<SharkFlt> tempList = new ArrayList<SharkFlt>(a);
               int start, stop;
                for (int i = 0; i < loop; i++) {
                   tempList.clear();
                   start = i * a;
                    stop = Math.min(i * a + a - 1, data.size() - 1);
                    System.out.println("range:" + start + " - " + stop);
                    for (int j = start; j <= stop; j++) {
                         tempList.add(data.get(j));
                   }
                     session.insert("ctas.importer.writer.mybatis.mappper.SharkFltMapper.insertBatch2", tempList);
                   session.commit();
                    session.clearCache();
                     System.out.println("已经插入" + (stop + 1) + " 条");
                 }
             } catch (Exception e) {
                 e.printStackTrace();
                 session.rollback();
            } finally {
               if (session != null) {
                    session.close();
                }
             }
            long endTime = System.currentTimeMillis();
            System.out.println("插入完成,耗时 " + (endTime - beginTime) + " 毫秒!");
  • 相关阅读:
    证券创新之翼 阿里金融云
    通过改变计算机策略来解决“只能通过Chrome网上应用商店安装该程序”的方法及模版文件下载
    VMware Workstation pro 12下载以及序列号
    Centos7下配置Tomcat7以指定(非root)身份运行
    apache2: Could not reliably determine the server's fully qualified domain name
    apache使用ssl数字证书
    苹果远程控制
    excel 组及分级显示制作教程
    Apache 性能优化
    Pigs and chickens
  • 原文地址:https://www.cnblogs.com/zxf330301/p/9966343.html
Copyright © 2011-2022 走看看