INSERT ALL - For more than 1000 rows

后端 未结 1 584
滥情空心
滥情空心 2020-12-20 09:03

I have an INSERT ALL query in my program like:


        INSERT ALL
        

        
相关标签:
1条回答
  • 2020-12-20 09:26

    You need to perform batch insert.

    int batchSize = 100;
    try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
      YourMapper mapper = sqlSession.getMapper(YourMapper.class);
      int size = list.size();
      for (int i = 0; i < size;) {
        mapper.insertRecord(list.get(i));
        i++;
        if (i % batchSize == 0 || i == size) {
          sqlSession.flushStatements();
          sqlSession.clearCache();
        }
      }
      sqlSession.commit();
    }
    

    You should find an appropriate value for the batchSize (it depends on various factors).

    The insert statement is pretty straight forward.

    <insert id="insertRecord">
      INSERT INTO MYTABLE (COLUMN1, COLUMN2, COLUMN3)
      VALUES (#{addrElement.element1}, #{addrElement.element2}, #{addrElement.element3})
    </insert>
    

    We have an FAQ entry.

    0 讨论(0)
提交回复
热议问题