我一直在尝试使用带有MySQL数据库的Spring Boots JPA启用批量插入
在我的 application.properties :
中spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.datasource.url=jdbc:mysql://localhost:3306/demo?reconnect=true&rewriteBatchedStatements=true
spring.jpa.hibernate.jdbc.batch_size=500
spring.jpa.hibernate.order_inserts=true
spring.datasource.username=demo_user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
我尝试过使用CrudRepository.save(Iterable s)
并尝试使用自定义存储库(我在另一个答案中看到过):
@Component
public class BulkRepository<T extends Identifiable> {// Identifiable just has a getId method
private static final int BATCH_SIZE = 500;
private final EntityManager entityManager;
@Autowired
public BulkRepository(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Transactional
public <S extends T> List<S> saveInBatch(Iterable<S> value) {
List<S> entities = Lists.newArrayList(value);
List<S> saved = new ArrayList<>(entities.size());
int i = 0;
for (S t : entities) {
saved.add(persistOrMerge(t));
i++;
if (i % BATCH_SIZE == 0) {
// Flush a batch of inserts and release memory.
entityManager.flush();
entityManager.clear();
}
}
return saved;
}
private <S extends T> S persistOrMerge(S t) {
if (t.getId() == null) {
entityManager.persist(t);
return t;
} else {
return entityManager.merge(t);
}
}
}
当我在MySql中启用一般查询日志时,它会将每个插入显示为单独的:
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';
SELECT
event_time,
user_Host,
CONVERT(argument USING utf8) as query
FROM mysql.general_log
order by event_time desc
;
2017-12-22 12:31:48.290334 demo_user [demo_user] @ localhost [127.0.0.1] insert 到demo.test_table(id,string)值(1,'value1')
2017-12-22 12:31:48.288328 demo_user [demo_user] @ localhost [127.0.0.1] insert into demo.test_table(id,string)values(2,'value2')
有什么我想念的吗?目前看起来我需要绕过Spring JPA并使用原始SQL。