如何通过Product ID列表使用JPA查询获取对应urlImg字段值列表
方案1:JPQL写法(推荐,跨数据库兼容性好)
补全后完整代码如下:
import org.springframework.data.repository.query.Param; // 方法需传入ID列表作为查询参数 @Query("select p.urlImg1 from Product p where p.id in :idList") List<String> findUrlImg1ByIdIn(@Param("idList") List<Long> idList);
说明:查询语句直接映射实体类和实体类字段名,:idList会自动绑定传入的ID列表参数,无需手动处理参数拼接,安全避免SQL注入问题。
方案2:原生SQL写法
如果有特殊需求要使用原生SQL查询,可使用以下写法:
import org.springframework.data.repository.query.Param; @Query(value = "select url_img1 from product where id in :idList", nativeQuery = true) List<String> findUrlImg1ByIdIn(@Param("idList") List<Long> idList);
说明:原生SQL写法中表名、字段名要和数据库实际命名完全一致,示例中默认表名为product,如果你在实体类上自定义了表名,需替换为实际的表名。
内容的提问来源于stack exchange,提问作者Steve Jobs
相关产品推荐
相关产品推荐

