Kzz xDさん:
入力としてタイプString []の2つのパラメータをとり、それが受信した内容に応じてフィルタリングない方法があります。
方法
@Override
public List<Product> filter(String[] brands, String[] cpus) {
List<Product> products;
List<Product> toReturn = new ArrayList<>();
int i = 0;
if(brands != null && cpus != null){
for(String brand: brands){
for(String cpu: cpus){
products = productRepo.findAllByBrandAndCpu(brand, cpu);
toReturn.addAll(products);
System.out.println(i++);
}
}
}else{
if (brands != null){
for (String brand: brands) {
products = productRepo.findAllByBrand(brand);
toReturn.addAll(products);
}
}else if(cpus != null){
for (String cpu: cpus) {
products = productRepo.findAllByCpu(cpu);
toReturn.addAll(products);
}
}else{
return productRepo.findAll();
}
}
return toReturn;
}
非常に多くの反復が存在しないようにどのように私はここにこのコードを変更することができます。
ここに
if(brands != null && cpus != null){
for(String brand: brands){
for(String cpu: cpus){
products = productRepo.findAllByBrandAndCpu(brand, cpu);
toReturn.addAll(products);
System.out.println(i++);
}
}
}
この方法findAllByBrandAndCpu
@Query(value = "SELECT p FROM Product p where p.brand.name = ?1 and p.cpu.name = ?2")
List<Product> findAllByBrandAndCpu(String brandName, String cpuName);
テリー:
あなたが使用してリポジトリのクエリを更新することにより、両方のネストされたループを削除することができ@KzzこんにちはIN
などのSQL演算子を:
@Query(value = "SELECT p FROM Product p where p.brand.name IN ?1 and p.cpu.name IN ?2")
List<Product> findAllByBrandAndCpu(List<String> brands, List<String> cpus);