Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JPA分页将数据从32k行Excel持久化到MySql数据库?_Java_Spring Boot_Jpa_Spring Data Jpa_Pagination - Fatal编程技术网

Java 如何使用JPA分页将数据从32k行Excel持久化到MySql数据库?

Java 如何使用JPA分页将数据从32k行Excel持久化到MySql数据库?,java,spring-boot,jpa,spring-data-jpa,pagination,Java,Spring Boot,Jpa,Spring Data Jpa,Pagination,我有一个很大的Excel文件,有32k行,还有一个Java Spring代码,它将Excel数据持久保存到mySQL数据库中。我的代码适用于大约6k行,但由于JPA的限制,不适用于整个Excel。我读到可以用JPA分页来完成,但到目前为止,我只找到了从DB收集数据(已经用数据持久化)并呈现到UI的信息。Excel文件包含32k个药品,这些行将被保存到数据库中 我使用以下方法创建此控制器层: public ResponseEntity<ResponseMessage> uplo

我有一个很大的Excel文件,有32k行,还有一个Java Spring代码,它将Excel数据持久保存到mySQL数据库中。我的代码适用于大约6k行,但由于JPA的限制,不适用于整个Excel。我读到可以用JPA分页来完成,但到目前为止,我只找到了从DB收集数据(已经用数据持久化)并呈现到UI的信息。Excel文件包含32k个药品,这些行将被保存到数据库中

我使用以下方法创建此控制器层:

    public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile file,
                                                    @RequestParam(defaultValue = "0") int page,
                                                    @RequestParam(defaultValue = "6000") int size) {

        String message = "";

        if (ExcelHelper.hasExcelFormat(file)) {
            try {
// the following 6 row are my patetic attempt to resolve with pagination
               List<Medicine> medicines = new ArrayList<>();
                Pageable paging = PageRequest.of(page, size);
                Page<Medicine> pageMedicamente = medicineRepositoryDao.save(paging);

                medicines = pageMedicamente.getContent();
                medicineService.save(file);

                message = "Uploaded the file successfully: " + file.getOriginalFilename();
                return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
            } catch (Exception e) {
                message = "Could not upload the file: " + file.getOriginalFilename() + "!";
                return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
            }
        }
public ResponseEntity上传文件(@RequestParam(“文件”)多部分文件,
@RequestParam(defaultValue=“0”)整型页面,
@RequestParam(defaultValue=“6000”)整数大小){
字符串消息=”;
if(ExcelHelper.hasExcelFormat(文件)){
试一试{
//下面的6行是我尝试用分页来解决的问题
列表药物=新的ArrayList();
Pageable paging=PageRequest.of(页面,大小);
Page pageMedicamente=medicineRepositoryDao.save(分页);
medicines=pageMedicamente.getContent();
medicineService.save(文件);
message=“已成功上载文件:”+file.getOriginalFilename();
返回ResponseEntity.status(HttpStatus.OK).body(新的ResponseMessage(message));
}捕获(例外e){
message=“无法上载文件:“+file.getOriginalFilename()+”!”;
返回ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(新的ResponseMessage(message));
}
}
以及存储库层:

@Repository
public interface MedicineRepositoryDao extends JpaRepository<Medicine, Long> {


    Page<Medicine> save( Pageable pageable);

}
@存储库
公共接口MedicineRepositoryDao扩展了JpaRepository{
页面保存(可分页可分页);
}
以及服务层:

        try {
            List<Medicine> medicines = ExcelHelper.excelToMedicine(file.getInputStream());
            medicineRepositoryDao.saveAll(medicines);
        } catch (IOException e) {
            throw new RuntimeException("fail to store excel data: " + e.getMessage());
        }
    }

试试看{
List medicines=ExcelHelper.excelToMedicine(文件.getInputStream());
药物储备dao.saveAll(药物);
}捕获(IOE异常){
抛出新的RuntimeException(“无法存储excel数据:+e.getMessage());
}
}

我想你把一些事情搞混了

  • 我不认为Spring对您可能在这里保留的行数有任何相关限制。但JPA确实如此。JPA不保留对您保存在其一级缓存中的任何实体的引用。因此,对于大量的行/实体,这会占用内存,并且会使某些操作变慢,因为实体会逐个查找或处理

  • 分页用于读取实体,而不是保存

  • 在这种情况下,你有几个选择

  • 不要使用JPA。对于简单地从文件中写入数据并将其写入数据库来说,JPA几乎没有任何好处。这几乎可以通过使用
    JdbcTemplate
    NamedParameterJdbcTemplate
    来实现,而且速度会快得多,因为JPA的开销被跳过,在这种情况下,您不会从中受益。如果您想使用ORM,您可能想看看Spring数据JDBC,它在概念上更简单,不保留对实体的引用,因此在这个场景中应该显示更好的特性。我建议不要在这里使用ORM,因为您似乎没有从拥有实体中获益,因此创建实体然后让ORM从中提取数据确实是浪费时间

  • 将导入内容分为多个批次。这意味着在继续处理下一个1000行之前,您可以一次保存1000行,将它们写入数据库并提交事务。对于JPA来说,出于上述原因,这几乎是必要的。对于JDBC(即,
    JdbcTemplate
    &Co),这对于32K行可能不是必需的,但可能会提高性能,并在插入失败时有助于恢复。SpringBatch将帮助您实现这一点

  • 虽然前面的一点讨论了批处理,即将导入分解成块,但您还应该研究JDBC端的批处理,在JDBC端,您可以一次向数据库发送多条语句,或者发送一条包含多组参数的语句,这同样可以提高性能

  • 最后,除了Javaverse之外,通常还有更适合这项工作的替代方案。有些数据库有工具可以非常高效地加载平面文件


  • 您的信息很受欢迎,谢谢!我使用Hibernate作为ORM。