我有一个名为Recipe的实体,并且创建了一个扩展JpaRepository的存储库,我想搜索包含DietLabelList搜索数组中每个元素的配方。
@Getter
@Setter
@Entity
@NoArgsConstructor
@RequiredArgsConstructor
@ToString
@Table(name = "recipe")
public class Recipe {
@Column(name = "id", nullable = false)
@Id
@GeneratedValue
private UUID rid;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "dietLabelList", nullable = false)
private UUID[] dietLabelList;
}
@Repository
public interface RecipeRepository extends JpaRepository<Recipe, UUID> {
List<Recipe> findByTitleContaining(String title);
List<Recipe> findByDietLabelList(UUID[] dietLabels);
}
例如我有一个食谱,例如DietBaelList [[Balanced],“ High-Fiber”,“ High-Protein”]和findByDietLabelList([“ Balanced”,“ High-Fiber”])应该可以找到它。 JpaRepository是否有可能?
最佳答案
您可以使用QueryParam
并指定自定义查询
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
@Repository
public interface RecipeRepository extends JpaRepository<Recipe, UUID> {
List<Recipe> findByTitleContaining(String title);
@Query(value = "SELECT r FROM Recipe r WHERE r.dietLabels in :dietLabels")
List<Recipe> findByDietLabelList(@Param("dietLabels") UUID[] dietLabels);
}