• mapepr ```java

import com.baomidou.mybatisplus.core.mapper.BaseMapper; import info.andav.boot.entities.BatchInsertTestTableEntities;

/**

  • @author anda
  • @since 1.0 */ public interface BatchInsertTestTableMapper extends BaseMapper { }
  1. - controller
  2. ```java
  3. private static List<BatchInsertTestTableEntities> batchList = new ArrayList();
  4. private final BatchInsertTestTableService batchInsertTestTableService;
  5. static {
  6. for (int i = 0; i < 1000; i++) {
  7. BatchInsertTestTableEntities batchInsertTestTableEntities = new BatchInsertTestTableEntities();
  8. batchInsertTestTableEntities.setPassword("root" + i);
  9. batchInsertTestTableEntities.setUsername("gaosheng" + i);
  10. batchInsertTestTableEntities.setCreateTime(LocalDateTime.now());
  11. batchInsertTestTableEntities.setUpdateTime(LocalDateTime.now());
  12. batchList.add(batchInsertTestTableEntities);
  13. }
  14. }

1.循环代码的操作

1.controller

  1. /**
  2. * 循环操作代码
  3. * 109442ms 1000条数据耗时
  4. */
  5. @GetMapping("/insert1")
  6. public void insert1() {
  7. long start = System.currentTimeMillis();
  8. batchList.forEach(batchInsertTestTableService::insert);
  9. long end = System.currentTimeMillis();
  10. System.out.println("耗时 " + (end - start) + "ms");
  11. }

2.service_impl

  1. /**
  2. * @author anda
  3. * @since 1.0
  4. */
  5. @Service
  6. @RequiredArgsConstructor
  7. public class BatchInsertTestTableServiceImpl extends ServiceImpl<BatchInsertTestTableMapper, BatchInsertTestTableEntities> implements BatchInsertTestTableService {
  8. public void insert(BatchInsertTestTableEntities batchInsertTestTableEntities) {
  9. batchInsertTestTableMapper.insert(batchInsertTestTableEntities);
  10. }
  11. }

3.service

  1. public interface BatchInsertTestTableService {
  2. void insert(BatchInsertTestTableEntities batchInsertTestTableEntities);
  3. }