Selaa lähdekoodia

feat: 完成了动态sql

DingXiaoYu 2 vuotta sitten
vanhempi
commit
4bb12e39bc

+ 9 - 3
src/main/java/com/seecoder/BlueWhale/serviceImpl/ProductServiceImpl.java

@@ -84,9 +84,15 @@ public class ProductServiceImpl implements ProductService {
             condition=condition.concat("AND price <= :price");
             condition=condition.concat("AND price <= :price");
         }
         }
         Query query=entityManager.createQuery(condition);
         Query query=entityManager.createQuery(condition);
-        query.setParameter("name",name);
-        query.setParameter("category",category);
-        query.setParameter("price",price);
+        if (name!=null && name.length()>0){
+            query.setParameter("name",name);
+        }
+        if (category!=null){
+            query.setParameter("category",category);
+        }
+        if (price!=null && price>0){
+            query.setParameter("price",price);
+        }
         List<Product> products=query.getResultList();
         List<Product> products=query.getResultList();
         return products.stream().map(Product::toVO).collect(Collectors.toList());
         return products.stream().map(Product::toVO).collect(Collectors.toList());
     }
     }