gpt4 book ai didi

java - 在 Java Spring boot jpa 中将 List> 转换为对象

转载 作者:行者123 更新时间:2023-12-02 09:16:05 25 4
gpt4 key购买 nike

我有一个如何转换的问题 List<Map<String, String>>自定义类对象以将其保存到 JPA。如果我应用 branchRepository.save(content)它不起作用。

这是我的代码:

-- BranchService.java --

public List<Map<String, String>> uploadEmployee(MultipartFile multip) throws Exception {

String fileNames = multip.getOriginalFilename();

DataFormatter formatter = new DataFormatter();

File file = new File("./reports/" + fileNames);
Workbook workbook = WorkbookFactory.create(file);

FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();

Sheet sheet = workbook.getSheetAt(0);

int headerRowNum = sheet.getFirstRowNum();

Map<Integer, String> colHeaders = new HashMap<Integer, String>();
Row row = sheet.getRow(headerRowNum);
for (Cell cell : row) {
int colIdx = cell.getColumnIndex();
String value = formatter.formatCellValue(cell, evaluator);
colHeaders.put(colIdx, value);
}

List<Map<String, String>> content = new ArrayList<Map<String, String>>();
for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
if (row == null)
row = sheet.createRow(r);
Map<String, String> valuesToHeaders = new HashMap<String, String>();
for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
int colIdx = entry.getKey();
Cell cell = row.getCell(colIdx);
if (cell == null)
cell = row.createCell(colIdx);
String cellValue = formatter.formatCellValue(cell, evaluator);
valuesToHeaders.put(entry.getValue(), cellValue);
}
content.add(valuesToHeaders);
}

workbook.close();

System.out.println(content);

return content;
}

如何转换并应用到JPA?

最佳答案

不要生成像 MapList 这样通用的东西,而是直接返回您的 JPA 实体。

所以转动这个:

List<Map<String, String>> content = new ArrayList<Map<String, String>>();
for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
if (row == null)
row = sheet.createRow(r);
Map<String, String> valuesToHeaders = new HashMap<String, String>();
for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
int colIdx = entry.getKey();
Cell cell = row.getCell(colIdx);
if (cell == null)
cell = row.createCell(colIdx);
String cellValue = formatter.formatCellValue(cell, evaluator);
valuesToHeaders.put(entry.getValue(), cellValue);
}
content.add(valuesToHeaders);
}

更像这样:

List<Branch> content = new ArrayList<>();
for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
row = sheet.getRow(r);
if (row == null)
continue; //SKIP, don't bother creating empty stuff!
Branch branch = new Branch();
for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
int colIdx = entry.getKey();
Cell cell = row.getCell(colIdx);
if (cell != null) {
String cellValue = formatter.formatCellValue(cell, evaluator);
switch(entry.getValue()) {
case "Description": {
branch.setDescription(cellValue);
break;
}
case "name": //example with multiple headers mapping to same field
case "Label": {
branch.setLabel(cellValue);
break;
}
}
//alternatively use if-else block with regex matching or some other technique to map your headers to JPA entity fields
}
}
content.add(branch);
}

使用 switch 您可以映射多个不同的拼写或缩写(如果您的规范允许此类事情),但为了获得最终的灵 active ,您可以使用正则表达式执行 if/else。也可以完全取消 header 并使其基于索引(switch on colIdx)。

当然,您可以保持代码不变并将上述内容写入新的转换函数,例如:

List<Branch> convert(List<Map<String,String>> content) {
return content.stream().map(this::convert).collect(Collectors.toList());
}
Branch convert(Map<String,String> props) {
Branch branch = new Branch();
for(String key : props.keySet()) {
String value = props.get(key);
switch(key) {
case "Description": {
branch.setDescription(value);
break;
}
case "name": //example with multiple headers mapping to same field
case "Label": {
branch.setLabel(value);
break;
}
}

}
return branch;
}

这可能是最简洁的选项(尽管您仍然希望删除空行和单元格创建代码)。

关于java - 在 Java Spring boot jpa 中将 List<Map<String, String>> 转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58977673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com