首页 > 解决方案 > 如何在 Jpa Hibernate Query 中将数字数组作为参数传递?

问题描述

例子 :

Select * from student where roll_no in (1,2,3);

在学生存储库(Spring Boot)中:

@Query(value="Select * from student where roll_no in (?)",native =true)
List selectStudents(我在这里给什么?)

或者还有其他方法可以实现吗?

标签: javamysqlspring-boothibernatejpa

解决方案


如果你想使用本机查询,你可以像下面那样做

@Query(value="select * from student where roll_no in (:rollNos)",native =true)
List<Object[]> selectStudents(@Param("rollNos") List<Integer> rollNos);

但我建议您使用如下所示的 JPA 命名查询来执行此操作,这很容易进一步处理,因为它以实体格式为您提供结果。

Student findByRollNo(List<Integer> rollNos);

推荐阅读