英文:
Spring Boot Java Read multiple Locations separated by ; instead of only one
问题
Sure, here are the translated parts:
如何从导入的文件中读取更多以分号分隔的位置?我需要同时更改searchByName函数吗?
@Override
public String getData(Row rowData, int index) {
final Cell cell = rowData.getCell(index);
if ((cell != null) && ((CellType.STRING == cell.getCellType()) || (CellType.FORMULA == cell.getCellType()))) {
return StringUtils.trimToNull(cell.getStringCellValue());
}
return null;
}
private Location getLocation(Row rowData, int index, String prefix) {
String nomLocation= this.excelReaderService.getData(rowData, index);
Location location= this.locationService.searchByName(nomLocation);
return location;
}
@Override
public Location searchByName(String name) {
return this.locationRepository.findFirstByName(name).orElse(null);
}
英文:
How can I read more Locations from the imported file separated by ; do I need to change the searchByName function as well?
@Override
public String getData(Row rowData, int index) {
final Cell cell = rowData.getCell(index);
if ((cell != null) && ((CellType.STRING == cell.getCellType()) || (CellType.FORMULA == cell.getCellType()))) {
return StringUtils.trimToNull(cell.getStringCellValue());
}
return null;
}
private Location getLocation(Row rowData, int index, String prefix) {
String nomLocation= this.excelReaderService.getData(rowData, index);
Location location= this.locationService.searchByName(nomLocation);
return location;
}
@Override
public Location searchByName(String name) {
return this.locationRepository.findFirstByName(name).orElse(null);
}
答案1
得分: 1
为了实现这个,我首先会创建一个名为getLocations
的新方法,返回一个List<Location>
。在这个方法中,我会使用分号进行拆分以获取位置的字符串数组,然后将该数组转换为List<String>
,因为在处理存储库方法时,使用字符串列表更容易:
private List<Location> getLocations(Row rowData, int index, String prefix) {
String locationData= this.excelReaderService.getData(rowData, index);
String[] nomLocations = locationData.split(";");
List<String> locationsList = new ArrayList<>(Arrays.asList(nomLocations));
List<Location> locations= this.locationService.searchByNames(locationsList);
return locations;
}
接下来,我会在Locations Service中创建一个名为searchByNames
的新方法。如何构建此方法取决于您底层数据库的类型。如果是关系型数据库,您可以使用JPA查询方法来处理IN
子句:
@Override
public List<Location> searchByNames(List<String> names) {
return this.locationRepository.findByNameIn(names);
}
当然,您还需要在Location Repository接口中定义该方法:
List<Location> findByNameIn(List<String> names);
另一方面,如果您使用的是NoSQL数据库,使用IN
子句进行查询通常是一个反模式。在这种情况下,您需要构建新的searchByNames
方法来迭代列表并多次调用存储库方法:
@Override
public List<Location> searchByNames(List<String> names) {
List<Location> locations = new ArrayList<>();
for (String name : names) {
Location result = this.locationRepository.findFirstByName(name).orElse(null);
if (result != null) {
locations.add(result);
}
}
return locations;
}
希望这有所帮助!
英文:
To accomplish this, I'd first create a new method called getLocations
to return a List<Location>
. Inside that, I'd split
on the semi-colon to get a String array of locations. I'd also convert that array to a List<String>
as those are easier with work with when dealing with repository methods :
private List<Location> getLocations(Row rowData, int index, String prefix) {
String locationData= this.excelReaderService.getData(rowData, index);
String[] nomLocations = locationData.split(";");
List<String> locationsList = new ArrayList<>(Arrays.asList(nomLocations));
List<Location> locations= this.locationService.searchByNames(locationsList);
return locations;
}
Then, I'd create a new method in the Locations Service called searchByNames
. How you build that method depends on what your underlying database is. If it's a relational database, you might be able to use the JPA query method for an IN
clause:
@Override
public List<Location> searchByNames(List<String> names) {
return this.locationRepository.findByNameIn(names);
}
You would, of course, also have to define that method in your Location Repository interface:
List<Location> findByNameIn(List<String> names);
On the other hand, if you're using a NoSQL database, querying with an IN
clause is usually an anti-pattern. In that case, you'd want to build the new searchByNames
method iterate through the list and make multiple calls to the repository method:
@Override
public List<Location> searchByNames(List<String> names) {
List<Location> locations = new ArrayList<>();
for (Location location : locations) {
Location result = this.locationRepository.findFirstByName(name).orElse(null);
if (result != null) {
locations.add(result);
}
}
return locations;
}
Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论