SpringBoot+JPA实现批量处理新增、修改

时间:2025-04-24 07:32:41

jpa的sava与saveAll

save()方法

 @Transactional
    public <S extends T> S save(S entity) {
        if ((entity)) {
            (entity);
            return entity;
        } else {
            return (entity);
        }
    }

根据源码我们可以看出来,save是先通过判断这个对象是不是新的,新的便会新增,否则就是执行的修改。整个是有分两步进行的,先查询再新增

saveAll()方法

  @Transactional
    public <S extends T> List<S> saveAll(Iterable<S> entities) {
        (entities, "The given Iterable of entities not be null!");
        List<S> result = new ArrayList();
        Iterator var3 = ();

        while(()) {
            S entity = ();
            ((entity));
        }

        return result;
    }

saveAll()方法是一种更新多条的一种方式,里面传的存对象的集合。分析源码我们可以看出saveAll()底层还是调用的save()方法,也就是每次需要先查询再做修改。在使用上方便,但是因每次涉及到查询、新增,事务的关系,导致修改或者新增耗时得非常的久。

那么下面我们将结合EntityManager对批量新增,修改做出优化。

jpa结合Batch

配置文件

spring:
  #配置 Jpa
  jpa:
    properties:
      hibernate:
        generate_statistics: false
        order_insert: true    //配置批量新增
        order_update: true    //配置批量修改
        dialect: .MySQL5InnoDBDialect
        jdbc:
          batch_size: 1000    //容器内批量的大小
    open-in-view: true

EntityManager

EntityManager其实就是一个容器,通过注解引入EntityManager对象 使用EntityManager对新增,修改进行批量处理

/**
 * @author 程序员panda
 * @date 
 * @desc 批量处理
 */
@Service
@Transactional
public class BatchService {

    @PersistenceContext
    private EntityManager entityManager;

	//配置文件中每次批量提交的数量
    @Value("${.batch_size}")
    private long batchSize;

    /**
     * 批量插入
     *
     * @param list 实体类集合
     * @param <T>  表对应的实体类
     */
    public <T> void batchInsert(List<T> list) {
        if (!(list)){
            for (int i = 0; i < (); i++) {
                ((i));
                if (i % batchSize == 0) {
                    ();
                    ();
                }
            }
            ();
            ();
        }
    }

    /**
     * 批量更新
     *
     * @param list 实体类集合
     * @param <T>  表对应的实体类
     */
    public <T> void batchUpdate(List<T> list) {
        if (!(list)){
            for (int i = 0; i < (); i++) {
                ((i));
                if (i % batchSize == 0) {
                    ();
                    ();
                }
            }
            ();
            ();
        }
    }
}

实际运用

选择一个需要新增的table,将原有的saveAll(),改为调用自定义的batchInsert()方法

 public JSONObject sumbitPhone(MultipartFile phoneFile) {
        long timeIdStart = ();
        JSONObject result=new JSONObject();
        List<CheckPhone> phoneList=getPhoneListEn(phoneFile);
        (phoneList);
        ("code",200);
        ("msg","提交成功");
        ("提交预校验数据用时:"+(() - timeIdStart) / 1000.0+"秒");
        return result;
    }

注意

使用batch批量新增的时候,表的主键不能使用自增的形式,需要使用uuid来才能使用批量的新增

运行时间对比

使用saveall()方法耗时(提交1000个号码文件) 从sql监控中可以看到,新增1000个号码,执行了7.962秒,执行了1000次的事务,都是一个个提交的

使用batch批量新增(同为1000个号码)此时执行,提交了两次,执行了两次事务。因为我设置了batch_size为1000。此时用是196毫秒。看一看出他是按999条数据,然后统一提交的