使用Java进行批量插入的最佳方法是什么?

使用Java进行批量插入的最佳方法是什么?,java,spring,hibernate,insert,bulk,Java,Spring,Hibernate,Insert,Bulk,我正在尝试制作一个可以在数据库中保存大量结构化对象的应用程序。 现在我正在使用SpringDataJPA(使用hibernate+MySQL(MyIsam表+外键)) 使用Spring数据存储库为数据服务编写代码既简单又愉快,但性能太慢 例如,我试图在一个表中插入100条记录,这需要8.5秒 我尝试直接在MySQL cmd中执行相同的插入操作(在过程中使用硬编码的“插入字符串”),结果显示时间为0,85秒。即使这样的结果对我来说也太慢了,但这是MySQL的问题 在阅读论坛时,我发现了这样一个问题

我正在尝试制作一个可以在数据库中保存大量结构化对象的应用程序。 现在我正在使用SpringDataJPA(使用hibernate+MySQL(MyIsam表+外键)) 使用Spring数据存储库为数据服务编写代码既简单又愉快,但性能太慢

例如,我试图在一个表中插入100条记录,这需要8.5秒

我尝试直接在MySQL cmd中执行相同的插入操作(在过程中使用硬编码的“插入字符串”),结果显示时间为0,85秒。即使这样的结果对我来说也太慢了,但这是MySQL的问题

在阅读论坛时,我发现了这样一个问题:SpringDataJPA不能正确地进行批量插入

如何使我的应用程序更快?我想使用大小为1000或更多的大容量插入操作

由于无法将对象存储在一个块中(尽管块大小不同),因此情况变得复杂

更清楚地说:

当然,要插入objectGame,需要执行几个步骤: 插入大量objectGame并获取其ID 让objectGame的ID插入大部分objectRound并获取其ID ... 等等

哪种方式更适合我


使用Spring数据JPA、jdbc、hibernate、myBatis?或者另一种方式?

您是对的,Spring数据JPA在默认情况下不能以有效的方式进行大容量插入。当我们想将25000多个对象批量写入一个表时,我们遇到了这个问题,我们有8个这样的表要写入。下面是我们是如何做到的

默认情况下,Spring数据JPA将把对象提取到EntityManager的缓存中,然后比较并决定是调用persist还是merge。我绕过了这个逻辑,因为我根据版本知道我是想持久化还是合并。技巧是根据
确定(entity.getVersion()==0){


你已经有了答案-MySQL和插入字符串。你只需要做一次。拿一把铲子,插入记录,然后继续。正如duffymo所指出的,直接db交互总是会更快。你考虑过使用Spring Batch吗?也许Spring Batch会帮到我。我不太熟悉它的功能。
The objectGame contains list of objectRound
The objectGame also contains list of objectGamePlayer
The objectRound contains list of objectRoundCard
The objectGamePlayer contains list of objectPlayerCard
The objectGamePlayer also contains list of objectPlayerBet
public List<T> update(Collection<T> entities) {
        List<T> ret =  new ArrayList<>();
        EntityManager em = getEntityManager();
        for (T entity : entities) {
            if (entity.getVersion() == 0) {
                em.persist(entity);
                ret.add(entity);
            } else {
                ret.add(em.merge(entity));
            }
        }
        return ret;
    }
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;

public class MyJpaRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

    private EntityManager entityManager;

    public MyJpaRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
        
        // Keep the EntityManager around to used from the newly introduced methods.
        this.entityManager = entityManager;
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }
}
@Configuration
@EnableJpaRepositories(basePackages = "au.com.xyz.infrastructure.repositories" , repositoryBaseClass = MyJpaRepositoryImpl.class)
@ComponentScan(basePackages = {"au.com.xyz.infrastructure.repositories.plugin", "au.com.xyz.infrastructure.repositories.external", ...})

public class InfrastructureSpringConfig {