我有一個使用 MYSQL 中的 like 運算子搜索特定行的查詢,它在 mysql 作業臺上完美運行,但我似乎無法使用 nativequery 在 Spring 的 JPA 上運行它。MYSQL 中的查詢是:
SELECT * FROM product where PRODUCT_ID LIKE '33__:modelId00000000';
當我在作業臺中測驗它時它有效:

其中我使用通配符查找在正確字符位置包含某個字串的所有產品。productId 以 33 開頭,然后是 0-100,然后是模型 id,所以 %:modelId% 在這里不起作用。這是我現在的 JPA 查詢,但它回傳 null。
@Query(value = "SELECT * FROM product where PRODUCT_ID LIKE '33__:modelId00000000'", nativeQuery = true)
List<Product> getProductByModelId(String modelId);
允許回傳什么,例如在這種情況下 modelId 為“99”:
- 3300 99 00000000
- 3301 99 00000000
- 3302 99 00000000
- 3303 99 00000000
- 3355 99 00000000
- 3389 99 00000000
- 3399 99 00000000
uj5u.com熱心網友回復:
也許,你可以嘗試這樣的事情:
SELECT * FROM product where PRODUCT_ID LIKE CONCAT('33%',:modelId,'00000000')
但由于我不熟悉 JPA,我不確定這個查詢是否安全不受 MySQL 注入的影響。
演示小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/352574.html
