You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot读取以分号分隔的多个Location而非单个的实现方案

读取分号分隔的多个Location及查询逻辑适配

1. 读取分号分隔的Location内容

原getLocation方法仅处理单个Location,需修改为支持多值分割处理:

  • 通过getData获取单元格字符串内容
  • 以分号;为分隔符拆分字符串
  • 对每个拆分后的名称做去空处理,过滤无效空字符串
  • 通过批量查询提升效率,获取所有匹配的Location实例

修改后的方法示例:

private List<Location> getLocations(Row rowData, int index, String prefix) {
    String locationStr = this.excelReaderService.getData(rowData, index);
    
    if (locationStr == null) {
        return Collections.emptyList();
    }
    
    // 分割字符串并过滤空的无效名称
    List<String> validLocationNames = Arrays.stream(locationStr.split(";"))
            .map(StringUtils::trimToNull)
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
    
    return this.locationService.searchByNames(validLocationNames);
}

2. 是否需要修改searchByName函数?

原searchByName仅返回单个匹配的Location,针对多Location场景推荐以下处理方式:

保留原方法,新增批量查询方法(推荐)

原searchByName可继续用于单个Location的查询场景,无需删除。为提升多值查询的性能,新增批量查询方法:

  • 在LocationService中新增searchByNames方法:
@Override
public List<Location> searchByNames(Collection<String> names) {
    if (CollectionUtils.isEmpty(names)) {
        return Collections.emptyList();
    }
    return this.locationRepository.findByNameIn(names);
}
  • 对应的LocationRepository添加查询方法:
List<Location> findByNameIn(Collection<String> names);

该方式仅需一次数据库查询,大幅提升数据导入时的性能,适合批量场景。

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 11:17:29