【问题标题】:Java Spring Import Excel to SQL ServerJava Spring 将 Excel 导入 SQL Server
【发布时间】:2022-01-14 23:10:59
【问题描述】:

我有使用 Java Spring 在 Excel 中导入 15.000 行的代码,在生产环境中大约需要 10 分钟,但在开发环境中只需要大约 5 分钟,我该如何提高性能?这是我的代码。

流程代码:

  1. 检查行 Excel 是否可以保存
  2. 按 1 保存到数据库 1

开始检查行 excel

                    Cell currentCell = cellsInRow.next();
                    String uuidAsString = uuid.toString();
                    Date today = Calendar.getInstance().getTime();
                    if(cellIndex==0) { 
                        ble.setA(currentCell.getStringCellValue());
                    } else if(cellIndex==1) {
                        ble.setB(currentCell.getStringCellValue());
                    } else if(cellIndex==2) { 
                        ble.setC(currentCell.getDateCellValue());
}

开始后

blacklistExternalRepository.saveAll(lstBlacklistExternal);

【问题讨论】:

    标签: java spring database import


    【解决方案1】:

    发布的代码不完整,所以这里是一个带有以下假设的想法:

    • 对于批量导入,变量 today 只能计算一次
    • excel 文档具有常规格式,即每一行在索引 0、1 和 2 处至少有三个单元格

    考虑到这一点,你可以做类似的事情

    LocalDate today = LocalDate.now();
    List<BLE> bleList = new ArrayList<>(); // a list of ble objects
    for (Row r : rows) {
        Iterator<Cell> cellsInRow = ... // get the cells in row r
        BLE ble = new BLE();
        ble.setA(cellsInRow.next().getStringCellValue());
        ble.setB(cellsInRow.next().getStringCellValue());
        ble.setC(cellsInRow.next().getDateCellValue());
        bleList.add(ble);
    }
    // do whatever you need to with the list of ble objects
    

    【讨论】:

    • 谢谢你的回答,我试试这个来提高我的代码性能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2014-04-05
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多