【发布时间】:2021-10-23 16:06:58
【问题描述】:
我有本机查询 sql。我如何向这个 sql 添加动态“WHERE”。有时用户希望通过 idBran 过滤此结果。如果metod有参数!= null,我可以添加额外的sql代码吗?
@Query(value = "SELECT product.id_product as idProduct," +
" product.product_name as productName," +
" brand.id_brand as idBrand," +
" brand.name_brand as nameBrand," +
" type.name_type as nameType," +
" type.short_type as shortType," +
" product_image.url_image as img," +
" product_image.alt_image as alt," +
" NVL(AVG(product_review.score),0) as score," +
" COUNT(product_review.textreview) as countComments" +
" FROM product" +
" LEFT JOIN product_review ON product.id_product=product_review.id_product" +
" JOIN brand ON product.id_brand=brand.id_brand" +
" JOIN type ON product.id_type=type.id_type" +
" JOIN product_image ON product.id_product=product_image.id_product" +
" GROUP BY product.id_product",
countQuery = "SELECT count(*) FROM product",
nativeQuery = true)
Page<ProductsToListing> getAllProductsToListing(Pageable pageable);
【问题讨论】:
-
idBran 是你想在这个“动态”中传递的唯一参数吗?
标签: java sql spring-boot spring-data-jpa